Skip to content
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

threadpool: fix semaphore deadlock #795

Merged
merged 1 commit into from Dec 12, 2018
Merged
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
27 changes: 19 additions & 8 deletions tokio-threadpool/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ impl Worker {
{
struct Guard<'a> {
worker: &'a Worker,
allocated_at_run: bool
}

impl<'a> Drop for Guard<'a> {
Expand All @@ -537,24 +536,27 @@ impl Worker {
// This capacity is "use it or lose it", so if the thread is not
// transitioned to blocking in this call, then another task has
// to be notified.
if self.allocated_at_run && !self.worker.is_blocking.get() {
self.worker.pool.notify_blocking_task(&self.worker.pool);
//
// If the task has consumed its blocking allocation but hasn't
// used it, it must be given to some other task instead.
if !self.worker.is_blocking.get() {
let can_block = self.worker.current_task.can_block();
if can_block == CanBlock::Allocated {
self.worker.pool.notify_blocking_task(&self.worker.pool);
}
}

self.worker.current_task.clear();
}
}

let can_block = task.consume_blocking_allocation();

// Set `current_task`
self.current_task.set(task, can_block);
self.current_task.set(task, CanBlock::CanRequest);

// Create the guard, this ensures that `current_task` is unset when the
// function returns, even if the return is caused by a panic.
let _g = Guard {
worker: self,
allocated_at_run: can_block == CanBlock::Allocated
};

task.run(notify)
Expand Down Expand Up @@ -774,7 +776,16 @@ impl CurrentTask {
}

fn can_block(&self) -> CanBlock {
self.can_block.get()
use self::CanBlock::*;

match self.can_block.get() {
Allocated => Allocated,
CanRequest | NoCapacity => {
let can_block = self.get_ref().consume_blocking_allocation();
self.can_block.set(can_block);
can_block
}
}
}

fn set_can_block(&self, can_block: CanBlock) {
Expand Down