@@ -272,8 +272,15 @@ def __init__(self, rsc, event_class):
272272 self .rsc = weakref .proxy (rsc , self .shutdown )
273273 self .event = event_class ()
274274 self .refreshed = event_class ()
275+ self .started_event = event_class ()
275276 self .stopped = False
276277
278+ def start_sync (self ):
279+ """Start the Monitor and block until it's really started.
280+ """
281+ self .start () # Implemented in subclasses.
282+ self .started_event .wait (5 )
283+
277284 def shutdown (self , dummy = None ):
278285 """Signal the monitor to shutdown.
279286 """
@@ -299,6 +306,7 @@ def monitor(self):
299306 """Run until the RSC is collected or an
300307 unexpected error occurs.
301308 """
309+ self .started_event .set ()
302310 while True :
303311 self .event .wait (Monitor ._refresh_interval )
304312 if self .stopped :
@@ -747,7 +755,11 @@ def __init__(self, hosts_or_uri=None, max_pool_size=10,
747755 register_monitor (self .__monitor )
748756
749757 if _connect :
750- self .__monitor .start ()
758+ # Wait for the monitor to really start. Otherwise if we return to
759+ # caller and caller forks immediately, the monitor could think it's
760+ # still alive in the child process when it really isn't.
761+ # See http://bugs.python.org/issue18418.
762+ self .__monitor .start_sync ()
751763
752764 def _cached (self , dbname , coll , index ):
753765 """Test if `index` is cached.
0 commit comments