Skip to content

Commit

Permalink
Merge pull request wasmerio#397 from wasmerio/sdk-73-fix-python-threa…
Browse files Browse the repository at this point in the history
…ding-in-the-browser

Running `import asyncio` with `wasmer/python@3.12` in the browser deadlocks
  • Loading branch information
Michael Bryan authored Jan 5, 2024
2 parents a381115 + 3517716 commit dbc77c2
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 41 deletions.
107 changes: 86 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ tracing = { version = "0.1", features = ["log", "release_max_level_debug"] }
tracing-futures = { version = "0.2" }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
url = "2.4.0"
virtual-fs = { version = "0.10.0", default-features = false }
virtual-fs = { version = "0.11.0", default-features = false }
virtual-net = { version = "0.6.0", default-features = false, features = ["remote"] }
wasm-bindgen = { version = "0.2" }
wasm-bindgen-derive = "0.2.1"
wasm-bindgen-downcast = "0.1"
wasm-bindgen-futures = "0.4"
wasm-bindgen-test = "0.3.37"
wasmer = { version = "4.2.4", default-features = false, features = ["js", "js-default", "tracing", "wasm-types-polyfill", "enable-serde"] }
wasmer-wasix = { version = "0.17", default-features = false, features = ["js", "js-default"] }
wasmer = { version = "4.2.5", default-features = false, features = ["js", "js-default", "tracing", "wasm-types-polyfill", "enable-serde"] }
wasmer-wasix = { version = "0.18", default-features = false, features = ["js", "js-default"] }
webc = "5.3.0"

[dependencies.web-sys]
Expand Down
11 changes: 8 additions & 3 deletions examples/wasmer.sh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { Terminal } from "xterm";
import { FitAddon } from "xterm-addon-fit";

const encoder = new TextEncoder();
const params = new URLSearchParams(window.location.search);

const packageName = params.get("package") || "sharrattj/bash";
const uses = params.getAll("use");
const args = params.getAll("arg");
const logFilter = params.get("log") || "warn";

async function main() {
// Note: We dynamically import the Wasmer SDK to make sure the bundler puts
Expand All @@ -18,7 +23,7 @@ async function main() {
const { Wasmer, init, initializeLogger } = await import("@wasmer/sdk");

await init(wasmerSDKUrl);
// initializeLogger("debug");
initializeLogger(logFilter);

const term = new Terminal({ cursorBlink: true, convertEol: true });
const fit = new FitAddon();
Expand All @@ -27,9 +32,9 @@ async function main() {
fit.fit();

term.writeln("Starting...");
const pkg = await Wasmer.fromRegistry("sharrattj/bash");
const pkg = await Wasmer.fromRegistry(packageName);
term.reset();
const instance = await pkg.entrypoint!.run();
const instance = await pkg.entrypoint!.run({ args, uses });
connectStreams(instance, term);
}

Expand Down
13 changes: 5 additions & 8 deletions src/tasks/post_message_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,9 @@ impl PostMessagePayload {

#[cfg(test)]
mod tests {
use std::{
num::NonZeroUsize,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};

use futures::channel::oneshot;
Expand Down Expand Up @@ -279,7 +276,7 @@ mod tests {
let engine = wasmer::Engine::default();
let module = wasmer::Module::new(&engine, wasm).unwrap();
let msg = PostMessagePayload::Notification(Notification::CacheModule {
hash: ModuleHash::sha256(wasm),
hash: ModuleHash::hash(wasm),
module: module.into(),
});

Expand All @@ -288,7 +285,7 @@ mod tests {

match round_tripped {
PostMessagePayload::Notification(Notification::CacheModule { hash, module: _ }) => {
assert_eq!(hash, ModuleHash::sha256(wasm));
assert_eq!(hash, ModuleHash::hash(wasm));
}
_ => unreachable!(),
};
Expand Down
10 changes: 9 additions & 1 deletion src/tasks/task_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use wasm_bindgen::{JsCast, JsValue};
use wasmer::{AsJs, AsStoreRef, Memory, MemoryType, Module, Store};
use wasmer_wasix::{
runtime::{
task_manager::{TaskWasm, TaskWasmRun, TaskWasmRunProperties, WasmResumeTrigger},
task_manager::{
TaskWasm, TaskWasmRecycle, TaskWasmRun, TaskWasmRunProperties, WasmResumeTrigger,
},
SpawnMemoryType,
},
wasmer_wasix_types::wasi::ExitCode,
Expand All @@ -30,6 +32,7 @@ pub(crate) fn to_scheduler_message(
spawn_type,
trigger,
update_layout,
recycle,
} = task;

let module_bytes = module.serialize().unwrap();
Expand Down Expand Up @@ -94,6 +97,7 @@ pub(crate) fn to_scheduler_message(
snapshot,
update_layout,
result: None,
recycle,
};

Ok(SchedulerMessage::SpawnWithModuleAndMemory {
Expand Down Expand Up @@ -195,6 +199,8 @@ pub(crate) struct SpawnWasm {
update_layout: bool,
/// The result of running the trigger.
result: Option<Result<Bytes, ExitCode>>,
#[derivative(Debug(format_with = "crate::utils::hidden"))]
recycle: Option<Box<TaskWasmRecycle>>,
}

impl SpawnWasm {
Expand Down Expand Up @@ -240,6 +246,7 @@ impl ReadySpawnWasm {
update_layout,
result,
trigger: _,
recycle,
}) = self;

// Invoke the callback which will run the web assembly module
Expand All @@ -258,6 +265,7 @@ impl ReadySpawnWasm {
ctx,
store,
trigger_result: result,
recycle,
};
run(properties);

Expand Down
Loading

0 comments on commit dbc77c2

Please sign in to comment.