Skip to content

Commit 775bf92

Browse files
authored
PYTHON-2699 Emit PoolReadyEvent before resuming the background thread (mongodb#683)
1 parent 7acb58b commit 775bf92

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

pymongo/pool.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,10 +1199,12 @@ def __init__(self, address, options, handshake=True):
11991199
self.ntxns = 0
12001200

12011201
def ready(self):
1202-
old_state, self.state = self.state, PoolState.READY
1203-
if old_state != PoolState.READY:
1204-
if self.enabled_for_cmap:
1205-
self.opts.event_listeners.publish_pool_ready(self.address)
1202+
# Take the lock to avoid the race condition described in PYTHON-2699.
1203+
with self.lock:
1204+
if self.state != PoolState.READY:
1205+
self.state = PoolState.READY
1206+
if self.enabled_for_cmap:
1207+
self.opts.event_listeners.publish_pool_ready(self.address)
12061208

12071209
@property
12081210
def closed(self):
@@ -1284,8 +1286,10 @@ def remove_stale_sockets(self, reference_generation, all_credentials):
12841286
`generation` at the point in time this operation was requested on the
12851287
pool.
12861288
"""
1287-
if self.state != PoolState.READY:
1288-
return
1289+
# Take the lock to avoid the race condition described in PYTHON-2699.
1290+
with self.lock:
1291+
if self.state != PoolState.READY:
1292+
return
12891293

12901294
if self.opts.max_idle_time_seconds is not None:
12911295
with self.lock:

0 commit comments

Comments
 (0)