[ServiceBus] Fix async sender AttributeError race in _open (#35618)#46683
Open
SAY-5 wants to merge 1 commit intoAzure:mainfrom
Open
[ServiceBus] Fix async sender AttributeError race in _open (#35618)#46683SAY-5 wants to merge 1 commit intoAzure:mainfrom
SAY-5 wants to merge 1 commit intoAzure:mainfrom
Conversation
Capture self._handler into a local before awaiting open_async/client_ready_async so a concurrent coroutine cannot null it out mid-flight, addressing the AttributeError reported in Azure#35618. Signed-off-by: SAY-5 <saiasish.cnp@gmail.com>
|
Thank you for your contribution @SAY-5! We will review the pull request and get back to you soon. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to fix a concurrency bug in the async Service Bus sender by making _open() use a local handler reference, so shared ServiceBusSender instances are less likely to hit an AttributeError during concurrent sends. It fits into the azure-servicebus package's async connection lifecycle, specifically around sender startup and readiness checks.
Changes:
- Updated
ServiceBusSender._open()to captureself._handlerin a local variable before awaited startup calls. - Switched handler-dependent readiness and max-message-size logic in
_open()to use the captured local handler. - Added a changelog entry describing the async sender race-condition fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
sdk/servicebus/azure-servicebus/azure/servicebus/aio/_servicebus_sender_async.py |
Adjusts async sender open logic to use a local handler reference during awaited startup. |
sdk/servicebus/azure-servicebus/CHANGELOG.md |
Documents the intended bug fix for the upcoming unreleased version. |
Comment on lines
+212
to
+219
| handler = self._handler | ||
| try: | ||
| await self._handler.open_async(connection=self._connection) | ||
| while not await self._handler.client_ready_async(): | ||
| await handler.open_async(connection=self._connection) | ||
| while not await handler.client_ready_async(): | ||
| await asyncio.sleep(0.05) | ||
| self._running = True | ||
| self._max_message_size_on_link = ( | ||
| self._amqp_transport.get_remote_max_message_size(self._handler) or MAX_MESSAGE_LENGTH_BYTES | ||
| self._amqp_transport.get_remote_max_message_size(handler) or MAX_MESSAGE_LENGTH_BYTES |
Comment on lines
203
to
+214
| async def _open(self): | ||
| if self._running: | ||
| return | ||
| if self._handler: | ||
| await self._handler.close_async() | ||
| auth = None if self._connection else (await create_authentication(self)) | ||
| self._create_handler(auth) | ||
| # Capture a local reference to the handler to guard against concurrent | ||
| # coroutines mutating self._handler across awaits (see issue #35618). | ||
| handler = self._handler | ||
| try: | ||
| await self._handler.open_async(connection=self._connection) | ||
| while not await self._handler.client_ready_async(): | ||
| await handler.open_async(connection=self._connection) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #35618.
When multiple coroutines concurrently call
ServiceBusSender.send_messageson a shared async sender, a race in_servicebus_sender_async.py:_opencan null outself._handlerbetweenawait self._handler.open_async(...)andawait self._handler.client_ready_async(), producing:This patch captures
self._handlerinto a local variable immediately after_create_handler(auth)and uses that local for the subsequent awaits within_open. Theexcept/_close_handlercleanup path still operates onself._handler, preserving existing teardown semantics.All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines