fix: Replace asserts with None checks for graceful shutdown #1244
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are two code paths that race to set / read the value of
self._scheduler
variable of streaming_pull_managerCode path 1
future.cancel()
:python-pubsub/google/cloud/pubsub_v1/subscriber/futures.py
Lines 56 to 76 in 5402b62
calls
streaming_pull_manager.close()
:python-pubsub/google/cloud/pubsub_v1/subscriber/futures.py
Line 75 in 5402b62
which schedules the
_shutdown
method:python-pubsub/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py
Lines 896 to 902 in 5402b62
that sets the
self._scheduler
variable to None using a lockself._closing
:python-pubsub/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py
Lines 912 to 929 in 5402b62
Code Path 2
The streaming_pull_manager's
_on_response
method:python-pubsub/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py
Lines 1106 to 1129 in 5402b62
self._scheduler
variable is not None before proceeding to use it for processing further.Since the two code paths use different locks and are running on different threads, there are times when code path1 sets the
self._scheduler
value to None, while the_on_response
method is still executing. The assert in the_on_response
method fails causing an assertion error to be thrown. This happens when the library tries to process streaming pull responses when the library is also being shut down.This change replaces the asserts with a None check that would return early and stop processing(same behavior as an assert minus the error being thrown), resulting in a graceful shutdown of the library without red herring errors being logged to the user.
Fixes #997 🦕