Skip to content

sync: forget mpsc Permit before sending value - #8560

Merged
Darksonn merged 1 commit into
tokio-rs:tokio-1.51.xfrom
Darksonn:f12
Oct 2, 2026
Merged

Darksonn merged 1 commit into
tokio-rs:tokio-1.51.xfrom
Darksonn:f12

Conversation

@Darksonn

@Darksonn Darksonn commented Oct 2, 2026

Copy link
Copy Markdown
Member

Currently, mpsc::Permit::send calls self.chan.send(value) before
calling mem::forget(self). Because chan.send(value) enqueues the
message and then wakes the receiver waker, a panic in the receiver's
Waker::wake implementation causes unwinding to drop Permit after the
value has already been enqueued.

Dropping Permit returns a permit to the semaphore, and receiving the
enqueued message returns the permit a second time, permanently inflating
the channel's capacity.

Fix this by copying the self.chan reference and forgetting self
before calling chan.send(value).

Fixes: #8559

Currently, `mpsc::Permit::send` calls `self.chan.send(value)` before
calling `mem::forget(self)`. Because `chan.send(value)` enqueues the
message and then wakes the receiver waker, a panic in the receiver's
`Waker::wake` implementation causes unwinding to drop `Permit` after the
value has already been enqueued.

Dropping `Permit` returns a permit to the semaphore, and receiving the
enqueued message returns the permit a second time, permanently inflating
the channel's capacity.

Fix this by copying the `self.chan` reference and forgetting `self`
before calling `chan.send(value)`.

Fixes: tokio-rs#8559
@Darksonn Darksonn added C-enhancement Category: A PR with an enhancement or bugfix. A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Oct 2, 2026
@Darksonn
Darksonn requested a review from mattiapitossi October 2, 2026 13:20
@github-actions github-actions Bot added the R-loom-sync Run loom sync tests on this PR label Oct 2, 2026
@Darksonn Darksonn added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 2, 2026

@ADD-SP ADD-SP left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Darksonn Darksonn removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 2, 2026
@Darksonn
Darksonn merged commit 5b758f5 into tokio-rs:tokio-1.51.x Oct 2, 2026
104 checks passed
@Darksonn
Darksonn deleted the f12 branch October 2, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants