Skip to content

Commit bf74b4a

Browse files
committed
fix: add stop() call to BackgroundConsumer failures due to exceptions
1 parent 225bf75 commit bf74b4a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

google/api_core/bidi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ def _thread_main(self, ready):
684684
exc,
685685
)
686686

687+
self.stop()
687688
_LOGGER.info("%s exiting", _BIDIRECTIONAL_CONSUMER_NAME)
688689

689690
def start(self):

tests/unit/test_bidi.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,23 @@ def test_stop_error_logs(self, caplog):
918918
error_logs = [r.message for r in caplog.records if r.levelname == "ERROR"]
919919
assert not error_logs, f"Found unexpected ERROR logs: {error_logs}"
920920
bidi_rpc.is_active = False
921+
922+
def test_fatal_exceptions_will_shutdown_consumer(self, caplog):
923+
"""
924+
https://github.com/googleapis/python-api-core/issues/820
925+
Exceptions thrown in the BackgroundConsumer that
926+
lead to the consumer halting should also stop the thread and rpc.
927+
"""
928+
caplog.set_level(logging.DEBUG)
929+
bidi_rpc = mock.create_autospec(bidi.BidiRpc, instance=True)
930+
bidi_rpc.is_active = True
931+
on_response = mock.Mock(spec=["__call__"])
932+
933+
bidi_rpc.open.side_effect = ValueError()
934+
935+
consumer = bidi.BackgroundConsumer(bidi_rpc, on_response)
936+
937+
consumer.start()
938+
939+
# We want to make sure that close is called, which will surface the error to the caller.
940+
bidi_rpc.close.assert_called_once()

0 commit comments

Comments
 (0)