Skip to content

Commit

Permalink
Handle UNPARKING state in Receiver::drop() (#60)
Browse files Browse the repository at this point in the history
Fixes panic that could occur due to not handling this (short lived) channel state
  • Loading branch information
sandersaares authored Feb 22, 2025
1 parent e7acf4e commit 208cf17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- Handle the UNPARKING state correctly in `Receiver::drop()`.


## [0.1.10] - 2025-02-04
### Added
Expand Down
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,31 @@ impl<T> Drop for Receiver<T> {
// SAFETY: see safety comment at top of function
unsafe { dealloc(self.channel_ptr) };
}
// The sender has observed the RECEIVING state and is currently reading the waker from
// a poll. We need to loop here until we observe the MESSAGE or DISCONNECTED state.
// We busy loop here since we know the sender is done very soon.
#[cfg(any(feature = "std", feature = "async"))]
UNPARKING => loop {
hint::spin_loop();
// ORDERING: The load above has already synchronized with the write of the message.
match channel.state.load(Relaxed) {
MESSAGE => {
// SAFETY: we are in the message state so the message is initialized
unsafe { channel.drop_message() };

// SAFETY: see safety comment at top of function
unsafe { dealloc(self.channel_ptr) };
break;
}
DISCONNECTED => {
// SAFETY: see safety comment at top of function
unsafe { dealloc(self.channel_ptr) };
break;
}
UNPARKING => (),
_ => unreachable!(),
}
},
_ => unreachable!(),
}
}
Expand Down

0 comments on commit 208cf17

Please sign in to comment.