fix(rust): Returned::Block deadlocks single-threaded async executors under execute_async

Aperta
#110 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@brxs ci sta già lavorando.

Dal 27/11/2026.

  • #112 di @brxs — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

Start with rust/crates/psychoscript/src/host.rs around host::block and the Returned::Block handling described in the issue. Use the minimal #[tokio::test] reproduction and the Runtime::execute_async / Program::run_async entry points to trace where exec::block_on blocks the executor. The work is done when Returned::Block futures are polled asynchronously with the driving executor context, remain cancellable, and the reproduction completes with Ok("done") on a single-threaded Tokio runtime.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment
  • Crate: psychoscript 0.1.3 (dev @ f6b504e)
  • OS/arch: macOS arm64 (rustc 1.99.1), single-threaded Tokio (#[tokio::test], default flavor = "current_thread")
Summary

When driving a program asynchronously via Runtime::execute_async / Program::run_async on a single-threaded async executor (such as #[tokio::test]'s default current_thread runtime), calling a synchronous HostFunction that returns Returned::Block(future) deadlocks the executor until timeout_ms fires (or hangs forever if timeout_ms is None).

Root Cause

host::block (rust/crates/psychoscript/src/host.rs:1048) waits for Returned::Block futures via wait(...) -> exec::block_on(...), which parks the driving executor thread in Condvar::wait:

  1. Before OFFLOAD_AFTER (< 1 ms, home_of(vm) == None): block calls wait(...) directly on the executor thread.
  2. After OFFLOAD_AFTER (>= 1 ms, home_of(vm) == Some(home)): block hands wait(...) back to the executor thread via home.run(...), which still runs exec::block_on(...) on the executor thread inside Worker::poll.

Because the single-threaded executor is parked in Condvar::wait, its timer/I/O reactor (or sibling tasks) can never run to wake the future.

Minimal Reproduction
use std::time::Duration;
use psychoscript::{HostFunction, HostValue, Limits, ProgramOptions, Returned, Runtime};

#[tokio::test] // default current_thread runtime
async fn returned_block_deadlocks_current_thread_tokio() {
    let rt = Runtime::make()
        .limits(Limits {
            timeout_ms: Some(1_000),
            ..Default::default()
        })
        .globals(|_| {
            let sleep = HostFunction::new(|_, _| {
                Returned::Block(Box::pin(async {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                    Ok(HostValue::from("done"))
                }))
            });
            [("sleepSync", HostValue::from(sleep))]
        });

    let out = rt
        .execute_async("return sleepSync();", &ProgramOptions::new())
        .await
        .unwrap();

    // Actual:   Err(Diagnostic { kind: TimeoutExceeded, message: "Execution timed out after 1000ms." })
    // Expected: Ok("done") in ~10ms
    assert_eq!(out.json().unwrap(), serde_json::json!("done"));
}
Expected Behavior

In execute_async / run_async, Returned::Block futures should be polled asynchronously with the driving executor's Context (returning Poll::Pending when pending, both before and after offload) so single-threaded executors remain free to drive timers, I/O, and sibling tasks — and dropping the outer future cancels the in-flight Returned::Block future on the executor thread.

Lingua principale
Rust
Stelle
470
Fork
11
Merge medio
4h 10m
PR unite (30g)
89

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di anomalyco/psychoscript

Tutte le issue di anomalyco/psychoscript

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.