Skip to content

Commit d43c2e3

Browse files
fix: Catch and surface BaseException()
Currently, the behavior of the library is to catch Exception() when encountered in the user provided callback, surface it to the calling code and shut down the client. However, for BaseException() encountered in the user provided callback, the BaseException() is not surfaced to the calling code and the client is not shut down. Make the behavior of the client when BaseException() is encountered consistent with the behavior of the client when Exception() is encountered.
1 parent cdb94a8 commit d43c2e3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _wrap_as_exception(maybe_exception: Any) -> BaseException:
112112

113113
def _wrap_callback_errors(
114114
callback: Callable[["google.cloud.pubsub_v1.subscriber.message.Message"], Any],
115-
on_callback_error: Callable[[Exception], Any],
115+
on_callback_error: Callable[[BaseException], Any],
116116
message: "google.cloud.pubsub_v1.subscriber.message.Message",
117117
):
118118
"""Wraps a user callback so that if an exception occurs the message is
@@ -124,7 +124,7 @@ def _wrap_callback_errors(
124124
"""
125125
try:
126126
callback(message)
127-
except Exception as exc:
127+
except BaseException as exc:
128128
# Note: the likelihood of this failing is extremely low. This just adds
129129
# a message to a queue, so if this doesn't work the world is in an
130130
# unrecoverable state and this thread should just bail.

tests/unit/pubsub_v1/subscriber/test_streaming_pull_manager.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ def test__wrap_callback_errors_no_error():
7777
on_callback_error.assert_not_called()
7878

7979

80-
def test__wrap_callback_errors_error():
81-
callback_error = ValueError("meep")
80+
@pytest.mark.parametrize(
81+
"callback_error",
82+
[
83+
(ValueError("ValueError")),
84+
(BaseException("BaseException")),
85+
],
86+
)
87+
def test__wrap_callback_errors_error(callback_error):
8288

8389
msg = mock.create_autospec(message.Message, instance=True)
8490
callback = mock.Mock(side_effect=callback_error)

0 commit comments

Comments
 (0)