Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-1274

Reset libmongoc client after forking to avoid interacting with parent resources in child processes

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.7.0
    • Affects Version/s: None
    • Component/s: None
    • None

      CDRIVER-2857 is going to introduce a mongoc_client_reset() function that can be called after forking. This function will allow libmongoc to ensure a few things:

      • Existing connections are dropped, while SDAM topology is preserved
      • Existing cursors will no longer interact with the server (e.g. getMore or killCursors will not be invoked in the child). We'll rely on libmongoc to raise errors if a child attempts to interact the server via a parent's cursor; however, there is probably no harm in allowing iteration of results already stored in the local batch.
      • Existing sessions IDs from the parent won't be reused in the client, and endSessions will not be invoked for those lsids. We'll rely on libmongoc to raise errors if a mongoc_client_session_t is re-used in a child process.

      To leverage mongoc_client_reset(), the PHP driver will need a few changes:

      • Record the current PID when a Manager, Session, or Client object is created
      • During each free_object handler (i.e. destructor), check whether the current PID differs. If so, call mongoc_client_reset() on the associated mongoc_client_t before proceeding to call the appropriate libmongoc "destroy" function.
      • In order to avoid calling mongoc_client_reset() redundantly, the PHP driver should include some logic to determine if it has already been invoked for a given PID.

      In a previous version of this issue (when it only pertained to children not killing cursors created in the parent), we highlighted differences from PHPC-912. With this change, the PHP driver may actually allow children to destroy clients provided we check PIDs and reset accordingly before doing so. This would allow the driver to no longer leak memory, even though that was never a serious concern due to client destruction only happening when the process is shutting down.

            Assignee:
            jmikola@mongodb.com Jeremy Mikola
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: