Skip to content

fix(aio): use get_running_loop() instead of get_event_loop() in AIOConsumer#2212

Open
AlexCai26 wants to merge 1 commit intoconfluentinc:masterfrom
AlexCai26:fix/aio-event-loop-uvicorn
Open

fix(aio): use get_running_loop() instead of get_event_loop() in AIOConsumer#2212
AlexCai26 wants to merge 1 commit intoconfluentinc:masterfrom
AlexCai26:fix/aio-event-loop-uvicorn

Conversation

@AlexCai26
Copy link

Description

This PR replaces calls to the deprecated asyncio.get_event_loop() with asyncio.get_running_loop() inside AIOConsumer initialization and the subscribe method.

Motivation and Context

When integrating confluent_kafka.aio.AIOConsumer into applications running on complex async frameworks like FastAPI / Uvicorn, asyncio.get_event_loop() can return a different event loop than the one currently executing the request handlers.

Because _AIOConsumer uses get_event_loop() to wrap Kafka callbacks (e.g., on_assign, on_revoke, and internal configuration callbacks), these callbacks end up bound to the wrong loop. When AIOConsumer performs background operations via ThreadPoolExecutor and attempts to dispatch callbacks using loop.call_soon_threadsafe(...), the callbacks are scheduled on an inactive or incorrect event loop. This leads to silent deadlocks where poll() waits indefinitely for events that are never properly propagated to the application's running loop.

Using asyncio.get_running_loop() ensures that all thread-safe callbacks are explicitly scheduled on the exact event loop that instantiate and run the consumer. This perfectly aligns AIOConsumer with the existing correct implementation in AIOProducer, which already utilizes asyncio.get_running_loop().

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@AlexCai26 AlexCai26 requested review from a team and Matthew Seal (MSeal) as code owners March 10, 2026 00:57
@confluent-cla-assistant
Copy link

confluent-cla-assistant bot commented Mar 10, 2026

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ alex-ibb
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant