Repository navigation
Fix unbounded growth of the waiting queue in async RecvFut (#182) - #189
Open
jaideeppyne wants to merge 1 commit into
Open
jaideeppyne wants to merge 1 commit into
jaideeppyne wants to merge 1 commit into
Conversation
When an async `RecvFut` is polled after its hook has been fired but it fails to actually receive a message (e.g. the message was stolen by another receiver on a shared channel), the hook must be re-inserted into the `waiting` queue. However, the hook's `woken` flag was never cleared on re-insertion, so every subsequent poll that failed to receive would observe `woken` still set and push yet another duplicate clone of the same hook onto `waiting`, growing it without bound and leaking memory. Clear the `woken` flag while re-arming and re-inserting the hook, under the channel lock. `fire` also runs under this lock and the hook is not in `waiting` during this window, so the reset is race-free. Add a deterministic regression test.
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.
Fixes #182.
When an async
RecvFutis polled after its hook has been fired but it fails to actually receive a message (e.g. the message was stolen by another receiver on a shared channel), the hook is re-inserted intoChan::waiting. The hook'swokenflag was never cleared on re-insertion, so every subsequent poll that fails to receive re-observeswoken == trueand pushes yet another duplicate clone of the same hook, growingwaitingwithout bound (a memory leak, and it corrupts the waiting-receiver accounting).Interleaving, with two receivers sharing one channel:
recv_async— hook H1 is registered inwaiting,woken = false.fire(), settingwoken = true; the message goes to the queue.try_recv) steals that message first.recv_syncfails, so H1 is re-inserted — butwokenstaystrue.woken == trueand pushes another copy of H1.This clears the
wokenflag while re-arming and re-inserting the hook, under the channel lock.firealso runs under that lock and the hook is not inwaitingduring this window, so the reset is race-free. It only triggers on the path whererecv_synchas already failed (queue empty), so no message is dropped. This is orthogonal to #177's missed-wakeup fix.Includes a deterministic regression test: before the fix, 10 spurious polls leave 10 duplicate hooks in
waiting; after, exactly 1. Full test suite passes across all features; fmt and clippy are clean.