Skip to content

Fix deadlock when all receivers are dropped #474

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

Merged
3 commits merged into from Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ jobs:
- name: Docs
run: cargo doc --features docs

clippy_check:
name: Clippy check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Install rust
run: rustup update beta && rustup default beta
- name: Install clippy
run: rustup component add clippy
- name: clippy
run: cargo clippy --all --features unstable
# clippy_check:
# name: Clippy check
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v1
# - name: Install rust
# run: rustup update beta && rustup default beta
# - name: Install clippy
# run: rustup component add clippy
# - name: clippy
# run: cargo clippy --all --features unstable
14 changes: 11 additions & 3 deletions src/sync/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ impl<T> Channel<T> {
let mut tail = self.tail.load(Ordering::Relaxed);

loop {
// Extract mark bit from the tail and unset it.
//
// If the mark bit was set (which means all receivers have been dropped), we will still
// send the message into the channel if there is enough capacity. The message will get
// dropped when the channel is dropped (which means when all senders are also dropped).
let mark_bit = tail & self.mark_bit;
tail ^= mark_bit;

// Deconstruct the tail.
let index = tail & (self.mark_bit - 1);
let lap = tail & !(self.one_lap - 1);
Expand All @@ -699,8 +707,8 @@ impl<T> Channel<T> {

// Try moving the tail.
match self.tail.compare_exchange_weak(
tail,
new_tail,
tail | mark_bit,
new_tail | mark_bit,
Ordering::SeqCst,
Ordering::Relaxed,
) {
Expand Down Expand Up @@ -732,7 +740,7 @@ impl<T> Channel<T> {
// ...then the channel is full.

// Check if the channel is disconnected.
if tail & self.mark_bit != 0 {
if mark_bit != 0 {
return Err(TrySendError::Disconnected(msg));
} else {
return Err(TrySendError::Full(msg));
Expand Down
8 changes: 7 additions & 1 deletion tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ fn smoke() {

drop(s);
assert_eq!(r.recv().await, None);
})
});

task::block_on(async {
let (s, r) = channel(10);
drop(r);
s.send(1).await;
});
}

#[test]
Expand Down