Skip to content

Commit 94fe41e

Browse files
Robust thread termination in test_watch and test_watch_requires_lock_to_run (#6788)
1 parent 4b89e26 commit 94fe41e

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

distributed/tests/test_profile.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,15 @@ def stop():
198198
stop_called.set()
199199
return time() > start + 0.500
200200

201-
log = watch(interval="10ms", cycle="50ms", stop=stop)
201+
try:
202+
log = watch(interval="10ms", cycle="50ms", stop=stop)
202203

203-
stop_called.wait(2)
204-
sleep(0.5)
205-
assert 1 < len(log) < 10
206-
watch_thread.join(2)
204+
stop_called.wait(2)
205+
sleep(0.5)
206+
assert 1 < len(log) < 10
207+
finally:
208+
stop_called.wait()
209+
watch_thread.join()
207210

208211

209212
def test_watch_requires_lock_to_run():
@@ -232,21 +235,25 @@ def block_lock():
232235
# Block the lock over the entire duration of watch
233236
blocking_thread = threading.Thread(target=block_lock, name="Block Lock")
234237
blocking_thread.daemon = True
235-
blocking_thread.start()
236238

237-
log = watch(interval="10ms", cycle="50ms", stop=stop_profiling)
239+
try:
240+
blocking_thread.start()
238241

239-
start = time() # wait until thread starts up
240-
while threading.active_count() < start_threads + 2:
241-
assert time() < start + 2
242-
sleep(0.01)
242+
log = watch(interval="10ms", cycle="50ms", stop=stop_profiling)
243243

244-
sleep(0.5)
245-
assert len(log) == 0
246-
release_lock.set()
244+
start = time() # wait until thread starts up
245+
while threading.active_count() < start_threads + 2:
246+
assert time() < start + 2
247+
sleep(0.01)
247248

248-
profiling_thread.join(2)
249-
blocking_thread.join(2)
249+
sleep(0.5)
250+
assert len(log) == 0
251+
release_lock.set()
252+
finally:
253+
release_lock.set()
254+
stop_profiling_called.wait()
255+
blocking_thread.join()
256+
profiling_thread.join()
250257

251258

252259
@dataclasses.dataclass(frozen=True)

0 commit comments

Comments
 (0)