@@ -879,8 +879,11 @@ def __init__(
879879 # This will be used later if we fork.
880880 MongoClient ._clients [self ._topology ._topology_id ] = self
881881
882- def _init_background (self ) -> None :
882+ def _init_background (self , old_pid : Optional [ int ] = None ) -> None :
883883 self ._topology = Topology (self ._topology_settings )
884+ # Seed the topology with the old one's pid so we can detect clients
885+ # that are opened before a fork and used after.
886+ self ._topology ._pid = old_pid
884887
885888 def target () -> bool :
886889 client = self_ref ()
@@ -903,7 +906,7 @@ def target() -> bool:
903906
904907 def _after_fork (self ) -> None :
905908 """Resets topology in a child after successfully forking."""
906- self ._init_background ()
909+ self ._init_background (self . _topology . _pid )
907910
908911 def _duplicate (self , ** kwargs : Any ) -> MongoClient :
909912 args = self .__init_kwargs .copy ()
@@ -921,7 +924,7 @@ def _server_property(self, attr_name: str) -> Any:
921924 the server may change. In such cases, store a local reference to a
922925 ServerDescription first, then use its properties.
923926 """
924- server = self ._topology .select_server (writable_server_selector , _Op .TEST )
927+ server = self ._get_topology () .select_server (writable_server_selector , _Op .TEST )
925928
926929 return getattr (server .description , attr_name )
927930
0 commit comments