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

オープン
#110 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@brxs がすでに取り組んでいます。

2026年11月27日 から。

  • #112 @brxs による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
470
フォーク
11
平均マージ
4時間 10分
マージ済み PR(30日)
89

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

anomalyco/psychoscript のほかの issue

anomalyco/psychoscript の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。