Skip to content

Commit

Permalink
fix: ExecutionCtx in sdk make mutex acquire recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed Aug 24, 2024
1 parent bccd0af commit dca3787
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
19 changes: 10 additions & 9 deletions lib/sdk/src/execution_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,22 @@ impl Inner {
return false;
}*/

pub fn execute(&mut self) {
// get runnable asyncio task
pub fn execute(&mut self) -> Option<WaitUntilHandler> {
let (handle, is_wait) = asyncio::select();
if !is_wait {
return;
return None;
}
// no handle to run, but is-wait=true, do wait
if handle.is_none() {
asyncio::ready();
// after ready, select runnable when next time
return;
return None;
}
let handle = handle.unwrap();
let handler = self.handlers.remove(&handle);
if let Some(handler) = handler {
// call callback function
handler();
}
self.handlers.remove(&handle)
}

pub fn is_pending(&self) -> bool {
!self.handlers.is_empty()
}
Expand Down Expand Up @@ -150,7 +148,10 @@ impl ExecutionCtx {
/// after execute, it will be removed from asyncio task list
/// then it should check is_pending to check if there is any asyncio task pending
pub fn execute(&mut self) {
self.inner.lock().unwrap().execute();
let handler = self.inner.lock().unwrap().execute();
if let Some(handler) = handler {
handler();
}
}
/// `is_pending` check if there is any asyncio task pending
pub fn is_pending(&self) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions lib/wasm-host/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ impl Worker {
.land_http_incoming()
.call_handle_request(&mut store, &req)
.await?;
let body = store.data_mut().take_body(resp.body.unwrap()).unwrap();

let body_handle = resp.body.unwrap();
let body = store.data_mut().take_body(body_handle).unwrap();
debug!("response is ready, body:{}", body_handle);
// check async task is pending
let is_pending = exports
.land_asyncio_context()
Expand Down Expand Up @@ -154,7 +155,6 @@ impl Worker {
// println!("async task is done, cost:{:.2?}", now.elapsed());
});
}

Ok((resp, body))
}
}
Expand Down
8 changes: 3 additions & 5 deletions tests/wait-until/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use land_sdk::{http_main, ExecutionCtx};

#[http_main]
pub fn handle_request(req: Request, mut ctx: ExecutionCtx) -> Result<Response, Error> {
// read uri and method from request
let url = req.uri().clone();
let method = req.method().to_string().to_uppercase();

let seq_id = ctx.sleep(1500);
// this function is called in host with tokio::spawn
ctx.sleep_callback(seq_id, || {
println!("sleep 1.5s done!");
});

// this function is called in guest.
// std::thread::sleep will block main thread(wasm runtime is single-thread currently)
ctx.wait_until(|| {
println!("sleep 1s...");
std::thread::sleep(std::time::Duration::from_secs(1));
Expand All @@ -27,8 +27,6 @@ pub fn handle_request(req: Request, mut ctx: ExecutionCtx) -> Result<Response, E
// build response
Ok(http::Response::builder()
.status(200)
.header("X-Request-Url", url.to_string())
.header("X-Request-Method", method)
.body(Body::from("Hello Runtime.land!!"))
.unwrap())
}

0 comments on commit dca3787

Please sign in to comment.