Skip to content

Conversation

@zijiren233
Copy link

Fixed a critical race condition in Authenticated::wait() where notifications
could be lost if auth.set() was called between checking is_authenticated and
calling notify.notified().await, causing permanent blocking of request handlers.

Changes:

  • Create notified future before double-checking auth status to ensure no notifications are missed
  • Add is_authenticated() helper method for non-blocking authentication checks
  • Optimize auth.wait() call sites with fast-path checks to skip select! when already authenticated
  • Apply optimization to uni stream, bi stream, and datagram handlers

The fix leverages Tokio's guarantee that Notify::notified() futures created
before notify_waiters() will resolve immediately, preventing notification

   Fixed a critical race condition in Authenticated::wait() where notifications
   could be lost if auth.set() was called between checking is_authenticated and
   calling notify.notified().await, causing permanent blocking of request handlers.

   Changes:
   - Create notified future before double-checking auth status to ensure no notifications are missed
   - Add is_authenticated() helper method for non-blocking authentication checks
   - Optimize auth.wait() call sites with fast-path checks to skip select! when already authenticated
   - Apply optimization to uni stream, bi stream, and datagram handlers

   The fix leverages Tokio's guarantee that Notify::notified() futures created
   before notify_waiters() will resolve immediately, preventing notification
@Itsusinn
Copy link
Owner

Itsusinn commented Nov 2, 2025

I thought about it when I wrote those code, it should hard to occur...

@Itsusinn
Copy link
Owner

Itsusinn commented Nov 2, 2025

I'm wondering if there's some kind of test that can cover this race condition

@Itsusinn Itsusinn merged commit 2764fe6 into Itsusinn:main Nov 2, 2025
19 checks passed
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.

2 participants