[CDRIVER-2049] Do not shutdown() sockets created by other processes Created: 13/Feb/17  Updated: 03/May/17  Resolved: 29/Mar/17

Status: Closed
Project: C Driver
Component/s: libmongoc, network
Affects Version/s: 1.6.0
Fix Version/s: 1.7.0

Type: Improvement Priority: Major - P3
Reporter: Jeremy Mikola Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to PHPC-912 Child processes should not destroy cl... Closed
is related to PHPC-913 Child process should not re-use mongo... Closed

 Description   

In PHPC-912, a user encountered a situation where a forked child process closed a server connection that was still in use by the parent process. Since the PHP driver persists mongoc_client_t objects in a hash table, this table was copied by the child process and then destroyed when the child process exited while the parent was still alive. Upon destruction, the PHP driver attempts to free any persisted clients with mongoc_client_destroy(), which ultimately calls mongoc_socket_close() for each socket. Therein, shutdown() is called on each file descriptor. I propose that shutdown() only be called if the socket was created by the same PID. close() can always be called, since the operating system will handle reference counting on any shared file descriptors.

We already plan to associate mongoc_client_t objects with a file descriptor in the PHP driver, to avoid a child process utilizing an existing mongoc_client_t created by a parent process (PHPC-913); however, we have no mechanism to destroy a mongoc_client_t and free memory without also unconditionally triggering shutdown() for each of its sockets. AFAICT, the only alternative would be for the PHP driver to leak mongoc_client_t objects created by other processes, which seems sloppy but may be excusable since this cleanup is happening as the PHP process terminates.



 Comments   
Comment by Githook User [ 03/Apr/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-2049 don't shutdown sockets created by parent

Store the current process id when creating a socket, and check the pid
in mongoc_socket_close to prevent closing a socket created by the parent
process. Useful for forking environments like PHP.

test_mongoc_socket_check_closed proves we still shut down sockets in the
common case.
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/13f91bcc880676a26524ab7783b4b8a8d6b8f6a1

Generated at Wed Feb 07 21:14:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.