azure-sdk-for-python: AIO version of `EventHubConsumerClient` has significant memory leak
- Package Name: azure-eventhub
- Package Version: ^5.11.2 (maybe older as well)
- Operating System: Linux
- Python Version: 3.9, 3.12
Describe the bug
AIO version of EventHubConsumerClient has significant memory leak, when async code is used inside on_event
To Reproduce Steps to reproduce the behavior:
- Use reference implementation from https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/eventhub/azure-eventhub/samples/async_samples/recv_async.py
- Consume some 1.3-5k events per minute for 10-15 min
- Observe the memory grows only
Expected behavior Long running events processing to not leak memory.
Screenshots
AIO version memory usage over 12h:
(note the spike downs are POD restarts due to OOM killer)
Sync version memory usage over 12h:
Additional context For example the sync implementation has no issues at all https://github.dev/Azure/azure-sdk-for-python/tree/main/sdk/eventhub/azure-eventhub/samples/sync_samples/recv.py
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 19 (7 by maintainers)
I packaged a new .whl for azure-eventhub from the Main branch (commit ef2521b) and duplicated @smoke’s test condition (https://github.com/Azure/azure-sdk-for-python/issues/36398#issuecomment-2228490156).
Memory usage on the latest build is now stable:
I would appreciate it if we could cut a release with this patch included- I can’t import arbitrary commits into my environment.
Hi @smoke and @bretttegart, the fix is out in azure-eventhub 5.12.2. Thanks!
@l0lawrence I can not do that right now, sorry. Can you link the PR or commit that fixes the issue here, also make sure the issue is properly closed as resolved / fixed? This way avoid
Hi @smoke update: we have a fixed merged into main, if you are able to test out those changes by building a whl from there that would help to confirm that your issue has been resolved. If you are unable to, we will update this thread when the fix is released 😃
Hi @smoke we were able to reproduce your issue and are looking into a fix, will update when we have more information.
httpx.AsyncClient()on_eventis forcefully executed in parallel from the consumer, thus creating a back pressure of messages read but unprocessed. In my opinion parallel execution should be left for users to utilize, so they can properly handle the back pressure.Please find the log file with debug logger debug.log.tar.gz
Here a graph of the memory usage that clearly shows the memory leak:
Notice that to have realistic scenario I have used the following
on_eventhandler:Update: I have tested with code that does not have any additional code and there is no memory leak, apparently the problem is when there is some async code inside the
on_event.Here the pristine code log debug2.log.tar.gz
and the graph:
This memory leak is also impacting my application (forwarding large JSON audit events from Microsoft Entra to Azure Event Hub).
I reproduced the memory leak issue in a scratch environment. To do so, I wrote 200MB of dummy JSON data to a new event hub and downloaded it using the async reference implementation that Smoke linked in the issue. Debug logs from the receiver are attached. debug.log
Unfortunately, the Sync implementation doesn’t work in my environment due to a different issue with our dual-stack TCP outgoing proxy.
Please let me know if I can provide any additional context or logging!