Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EventHub] Fix race condition when buffered mode is enabled #34712

Merged
merged 3 commits into from Mar 21, 2024

Conversation

falcaopetri
Copy link
Contributor

Description

This PR implements a fix to the race condition described in #34711.

The approach here is twofold:

  1. Require the lock while trying to add new events to internal EventBatch buffer
  2. Remove unnecessary clean up code

Implementing either of these would be enough to fix the related issue, but I would argue that both are necessary to make thinks consistent:

  • Change 1. helps preventing future race condition issues, since it properly protects the shared resource self._cur_batch. It is especially important to synchronous BufferedProducer, since it relies on multi-threading, which can be even more unpredictable than an event loop.
  • Change 2. improves the consistency of self._cur_batch and self._cur_buffered_len:

A last note regarding Change 1.:

  • it seems that the azure-sdk-for-net also updates the self._cur_batch with a synchronization tool, see RunPublishingAsync

I also have another small set of changes that could make sync and async BufferedProducer codebase more consistent. Since they do not have an impact on functionality, I thought it would be better to submit them in follow up PR.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

It would indeed be interesting to have some test code here, though I think it's hard to make determinist tests check this potential race condition. We would think something based on this test, though it is currently skipped:

@pytest.mark.skip('not testing correctly + flaky, fix during MQ')
@pytest.mark.liveTest
@pytest.mark.asyncio
async def test_long_wait_small_buffer(connection_str, uamqp_transport):

@github-actions github-actions bot added the Community Contribution Community members are working on the issue label Mar 9, 2024
Copy link

github-actions bot commented Mar 9, 2024

Thank you for your contribution @falcaopetri! We will review the pull request and get back to you soon.

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. Event Hubs labels Mar 9, 2024
@falcaopetri
Copy link
Contributor Author

@microsoft-github-policy-service agree

@kashifkhan
Copy link
Member

/azp run python - eventhub - tests

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@annatisch
Copy link
Member

Thank you so much for taking the time to add this @falcaopetri! Much appreciated :)

@kashifkhan kashifkhan merged commit fc939b3 into Azure:main Mar 21, 2024
23 checks passed
@falcaopetri falcaopetri deleted the fix/eventhub-send-race-condition branch March 29, 2024 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. Event Hubs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants