Several factors may prevent TopologyClosedEvents from being observed.
In php_phongo_pclient_destroy, which is used to free both persistent and non-persistent clients belonging to the same process, we clear the APM callbacks on a client before calling mongoc_client_destroy. As noted in the comments, this has no effect for global, persistent clients since the subscribers would already have been freed by the time those clients are destroyed; however, it would prevent events being dispatched for non-persistent (i.e. request-scoped) clients. The reasoning for this logic is to avoid dispatching command monitoring events issued during client destruction (e.g. killing cursors or sessions) and references PHPC-1666; however, it's possible that this actually depends on PHPC-1741 (removing the Manager reference entirely from command monitoring event objects).
In php_phongo_manager_free_object, php_phongo_manager_unregister is called before php_phongo_client_unregister. The Manager registry was introduced in PHPC-1645 with non-persistent clients. When a Manager is removed from the registry, phongo_apm_get_subscribers_to_notify will no longer be able to find its subscribers when invoked through the topology_closed callback. This is despite the fact that the Manager's subscribers aren't actually freed until later in php_phongo_manager_free_object. Swapping the order of php_phongo_manager_unregister and php_phongo_client_unregister would address this; however, we'll need to ensure that doesn't cause some other conflict (e.g. null deference attempting to fetch a Manager for a dispatched event).
- is related to
-
PHPC-1741 Remove getServer API and manager reference from APM event classes
- Closed
- related to
-
PHPC-2029 Memory leak when saving Server object from command monitoring event subscriber
- Closed
-
PHPC-2461 Allow observation of ServerClosedEvents during client shutdown
- Blocked
-
PHPC-2449 Improve docs for when SDAM Monitoring events are dispatched
- Closed
-
PHPC-2030 Allow observation of commands issued during mongoc_client_destroy()
- Closed