fix: proper way to block tokio thread

This commit is contained in:
Myriad-Dreamin 2024-03-11 01:29:06 +08:00
parent fc573db375
commit 08be5e6592
3 changed files with 6 additions and 10 deletions

7
Cargo.lock generated
View file

@ -2514,12 +2514,6 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "pollster"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
[[package]]
name = "portable-atomic"
version = "1.6.0"
@ -3617,7 +3611,6 @@ dependencies = [
"once_cell",
"parking_lot",
"paste",
"pollster",
"serde",
"serde_json",
"tinymist-query",

View file

@ -44,7 +44,6 @@ typst-ts-core = { version = "0.4.2-rc6", default-features = false, features = [
"vector-bbox",
"no-content-hint",
] }
pollster = "0.3.0"
codespan-reporting = "0.11"
typst-ts-compiler.workspace = true
typst-preview.workspace = true

View file

@ -492,10 +492,14 @@ impl<Ctx> CompileClient<Ctx> {
f: impl FnOnce(&mut Ctx) -> Ret + Send + 'static,
) -> ZResult<Ret> {
// get current async handle
if tokio::runtime::Handle::try_current().is_ok() {
if let Ok(e) = tokio::runtime::Handle::try_current() {
let fut = self.steal_inner(f)?;
// todo: remove blocking
return pollster::block_on(fut).map_err(map_string_err("failed to steal"));
return std::thread::spawn(move || {
e.block_on(fut).map_err(map_string_err("failed to steal"))
})
.join()
.unwrap();
}
self.steal_inner(f)?