Skip to content

Commit ee78a03

Browse files
fix: Watch thread deadlock on exit (#1014)
* fix: fix thread cleanup when destroying Watch instance * added test for on_snapshot while closing
1 parent 44bdc3f commit ee78a03

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

packages/google-cloud-firestore/google/cloud/firestore_v1/watch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ def on_snapshot(self, proto):
440440
proto(`google.cloud.firestore_v1.types.ListenResponse`):
441441
Callback method that receives a object to
442442
"""
443+
if self._closing.locked():
444+
# don't process on_snapshot responses while spinning down, to prevent deadlock
445+
return
443446
if proto is None:
444447
self.close()
445448
return

packages/google-cloud-firestore/tests/unit/v1/test_watch.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ def test_watch_on_snapshot_target_w_none():
400400
assert inst._rpc is None
401401

402402

403+
def test_watch_on_snapshot_while_closing():
404+
inst = _make_watch()
405+
inst.close = mock.Mock()
406+
with inst._closing:
407+
inst.on_snapshot(mock.Mock())
408+
# close should not be called again when already closing
409+
inst.close.assert_not_called()
410+
411+
403412
def test_watch_on_snapshot_target_no_change_no_target_ids_not_current():
404413
inst = _make_watch()
405414
proto = _make_listen_response()

0 commit comments

Comments
 (0)