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

fix: allow for recursive block-on calls #799

Merged
merged 4 commits into from
Jun 4, 2020
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ futures-timer = { version = "3.0.2", optional = true }
surf = { version = "1.0.3", optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
smol = { version = "0.1.10", optional = true }
smol = { version = "0.1.11", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
futures-timer = { version = "3.0.2", optional = true, features = ["wasm-bindgen"] }
Expand Down Expand Up @@ -103,3 +103,4 @@ required-features = ["unstable"]
[[example]]
name = "surf-web"
required-features = ["surf"]

26 changes: 25 additions & 1 deletion src/task/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
Expand Down Expand Up @@ -150,8 +151,31 @@ impl Builder {
parent_task_id: TaskLocalsWrapper::get_current(|t| t.id().0).unwrap_or(0),
});

thread_local! {
/// Tracks the number of nested block_on calls.
static NUM_NESTED_BLOCKING: Cell<usize> = Cell::new(0);
}

// Run the future as a task.
unsafe { TaskLocalsWrapper::set_current(&wrapped.tag, || smol::run(wrapped)) }
NUM_NESTED_BLOCKING.with(|num_nested_blocking| {
let count = num_nested_blocking.get();
let should_run = count == 0;
// increase the count
num_nested_blocking.replace(count + 1);

unsafe {
TaskLocalsWrapper::set_current(&wrapped.tag, || {
let res = if should_run {
// The first call should use run.
smol::run(wrapped)
} else {
smol::block_on(wrapped)
};
num_nested_blocking.replace(num_nested_blocking.get() - 1);
res
})
}
})
}
}

Expand Down
51 changes: 48 additions & 3 deletions tests/block_on.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
#![cfg(not(target_os = "unknown"))]

use async_std::task;
use async_std::{future::ready, task::block_on};

#[test]
fn smoke() {
let res = task::block_on(async { 1 + 2 });
let res = block_on(async { 1 + 2 });
assert_eq!(res, 3);
}

#[test]
#[should_panic = "boom"]
fn panic() {
task::block_on(async {
block_on(async {
// This panic should get propagated into the parent thread.
panic!("boom");
});
}

#[cfg(feature = "unstable")]
#[test]
fn nested_block_on_local() {
use async_std::task::spawn_local;

let x = block_on(async {
let a = block_on(async { block_on(async { ready(3).await }) });
let b = spawn_local(async { block_on(async { ready(2).await }) }).await;
let c = block_on(async { block_on(async { ready(1).await }) });
a + b + c
});

assert_eq!(x, 3 + 2 + 1);

let y = block_on(async {
let a = block_on(async { block_on(async { ready(3).await }) });
let b = spawn_local(async { block_on(async { ready(2).await }) }).await;
let c = block_on(async { block_on(async { ready(1).await }) });
a + b + c
});

assert_eq!(y, 3 + 2 + 1);
}

#[test]
fn nested_block_on() {
let x = block_on(async {
let a = block_on(async { block_on(async { ready(3).await }) });
let b = block_on(async { block_on(async { ready(2).await }) });
let c = block_on(async { block_on(async { ready(1).await }) });
a + b + c
});

assert_eq!(x, 3 + 2 + 1);

let y = block_on(async {
let a = block_on(async { block_on(async { ready(3).await }) });
let b = block_on(async { block_on(async { ready(2).await }) });
let c = block_on(async { block_on(async { ready(1).await }) });
a + b + c
});

assert_eq!(y, 3 + 2 + 1);
}