Skip to content

fix: Auto-cancelled Sinks.Many would continue to accept emissions after cancellation #4037

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

georgebanasios
Copy link
Contributor

Fixes #3715

Ensure auto-cancelled Sinks.Many reject emissions.

1.Fixed the WIP Leak in drain()
The core issue was that the drain() method had several premature return statements inside its main loop that were triggered on termination. These exits bypassed the logic that decrements the wip counter, effectively deadlocking the sink and preventing queue cleanup.
Change: All early return statements inside the for(; ; ) loop in drain() have been removed.
Effect: This guarantees the drain loop always completes its lifecycle, the wip counter is managed correctly, and the sink's serialization is always preserved.

2.Adjusted tryEmitNext() Against Races
Change: tryEmitNext() now performs a final isCancelled() check after offering an item to the queue and triggering a drain.
Effect: If a cancellation occurs concurrently with an emission, drain() cleans up the offered item, and tryEmitNext() correctly returns FAIL_CANCELLED. This prevents both item leaks and misleading OK results.

3.Corrected Auto-Cancellation Logic in remove()
Change: When the last subscriber is removed, the remove() method now simply sets the cancelled state and delegates all cleanup work to the drain() method.
Effect: This centralizes all state-change side effects (like clearing the queue) into the main serialized drain loop.

@georgebanasios georgebanasios requested a review from a team as a code owner June 15, 2025 12:49
@georgebanasios georgebanasios force-pushed the fix-sink-accepts-emissions-after-cancellation branch from 0a85f9b to 5d86d11 Compare June 16, 2025 12:42
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.

Auto-cancelled Sink still accepts emissions
1 participant