Error contexts sent through futures/streams aren't reference-counted

未关闭
#14,674 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, wasm
领域
backend

调研方向

Start in crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs, especially lower_error_context_to_index, and compare its reference-count handling with error_context_transfer. Reproduce the supplied WAST case with component-model-error-context enabled, then trace the error-context lifetime through future and stream payloads. Done means the receiver can drop its copy and the sender can still drop its handle without a bug or stale slot reuse.

由索引模型根据 Issue 内容生成。

描述

bug wasm-proposal:component-model-error-context

This input:

;;! component_model_async = true
;;! component_model_error_context = true

;; `$B` sends an error-context to `$A` through a future, and `$A` drops the
;; copy it received. `$B` still owns its own handle, so dropping it should
;; succeed.
(component
  (component $A
    (core module $libc (memory (export "memory") 1))
    (core instance $libc (instantiate $libc))
    (type $FT (future error-context))
    (core func $future.read
      (canon future.read $FT async (memory (core memory $libc "memory"))))
    (core func $error-context.drop (canon error-context.drop))
    (core module $m
      (import "libc" "memory" (memory 1))
      (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
      (import "" "error-context.drop" (func $error-context.drop (param i32)))
      (func (export "take") (param $f i32)
        (if (i32.ne (call $future.read (local.get $f) (i32.const 0))
              (i32.const 0 (; COMPLETED ;)))
          (then unreachable))
        (call $error-context.drop (i32.load (i32.const 0))))
    )
    (core instance $i (instantiate $m
      (with "libc" (instance $libc))
      (with "" (instance
        (export "future.read" (func $future.read))
        (export "error-context.drop" (func $error-context.drop))))))
    (func (export "take") (param "f" $FT) (canon lift (core func $i "take"))))
  (instance $a (instantiate $A))

  (component $B
    (import "take" (func $take (param "f" (future error-context))))
    (core module $libc (memory (export "memory") 1))
    (core instance $libc (instantiate $libc))
    (type $FT (future error-context))
    (core func $take (canon lower (func $take)))
    (core func $future.new (canon future.new $FT))
    (core func $future.write
      (canon future.write $FT async (memory (core memory $libc "memory"))))
    (core func $error-context.new
      (canon error-context.new (memory (core memory $libc "memory"))))
    (core func $error-context.drop (canon error-context.drop))
    (core module $m
      (import "libc" "memory" (memory 1))
      (import "" "take" (func $take (param i32)))
      (import "" "future.new" (func $future.new (result i64)))
      (import "" "future.write" (func $future.write (param i32 i32) (result i32)))
      (import "" "error-context.new" (func $error-context.new (param i32 i32) (result i32)))
      (import "" "error-context.drop" (func $error-context.drop (param i32)))
      (func (export "run")
        (local $futures i64) (local $e i32)
        (local.set $e (call $error-context.new (i32.const 0) (i32.const 0)))
        (local.set $futures (call $future.new))
        (i32.store (i32.const 0) (local.get $e))
        (if (i32.ne (call $future.write
                      (i32.wrap_i64 (i64.shr_u (local.get $futures) (i64.const 32)))
                      (i32.const 0))
              (i32.const -1 (; BLOCKED ;)))
          (then unreachable))
        (call $take (i32.wrap_i64 (local.get $futures)))
        (call $error-context.drop (local.get $e)))
    )
    (core instance $i (instantiate $m
      (with "libc" (instance $libc))
      (with "" (instance
        (export "take" (func $take))
        (export "future.new" (func $future.new))
        (export "future.write" (func $future.write))
        (export "error-context.new" (func $error-context.new))
        (export "error-context.drop" (func $error-context.drop))))))
    (func (export "run") async (canon lift (core func $i "run"))))
  (instance $b (instantiate $B (with "take" (func $a "take"))))
  (export "run" (func $b "run"))
)

(assert_return (invoke "run"))

fails with

$ wasmtime wast -W component-model-error-context=y repro.wast
Error: failed to run script file 'repro.wast'
    0: failed directive on repro.wast:78
    1: error while executing at wasm backtrace:
    2: BUG: retrieve concurrent state for error context during drop

An LLM summary, if useful, is:

When an error-context is lowered as part of a future or stream payload,
lower_error_context_to_index
(crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs)
inserts it into the receiving component's table without incrementing
global_error_context_ref_counts. The direct transfer path,
error_context_transfer, does increment it.

So if a component sends an error-context through a future and the receiver
drops its copy, the global count goes from 1 to 0 and the shared state is
deleted while the sender still has a handle. The sender dropping its handle
then hits a bail_bug!. If another error-context is created first, it
reuses the freed slot, and the sender's handle refers to that one instead,
which may belong to a different component.

This requires component-model-error-context, which is off by default.

主要语言
Rust
星标
18.9k
派生
2k
平均合并
1 天 6 小时
30 天内合并 PR
238

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bytecodealliance/wasmtime 的其他 Issue

查看 bytecodealliance/wasmtime 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。