Memory leak on Windows: every new fiber leaks its start closure

未关闭 适合新手
#15,455 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, wasm

调研方向

阅读 crates/fiber/src/windows.rs,重点关注 fiber_start 以及它如何处理 initial_closure;将其与 #12928 中的 Unix 修复进行比较。在 Windows 上分别于更改前后运行提供的分配计数测试。反复调用 call_async 后几乎不留下堆块,即为完成。

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

描述

bug

Hi! I embed Wasmtime in a long-running Windows app that calls into wasm modules with call_async all day, and I noticed its memory growing by about 30–50 MB an hour. I tracked it down to crates/fiber/src/windows.rs.

In fiber_start, the closure is taken out of its box like this:

let func = Box::from_raw((*state).initial_closure.get().cast::<F>());
// ...
let suspend = super::Suspend::<A, B, C>::execute(suspend, initial, *func);

*func moves the closure out, but the Box itself is only freed when func goes out of scope, and fiber_start never gets there, because it ends by switching back to the parent fiber. So each fiber leaks one small allocation (24–40 bytes for us). Since call_async creates a new fiber for every call, this adds up quickly.

I think this is the same problem #12928 fixed for Unix, where fiber_start was changed to return. The Windows version still has it on main.

Test Case

No wasm file needed; the module is just one empty exported function, inlined below.

use std::alloc::{GlobalAlloc, Layout, System};
use std::pin::pin;
use std::sync::atomic::{AtomicIsize, Ordering};
use std::task::{Context, Poll, Waker};

struct Counting;
static LIVE_BLOCKS: AtomicIsize = AtomicIsize::new(0);

unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        LIVE_BLOCKS.fetch_add(1, Ordering::Relaxed);
        unsafe { System.alloc(layout) }
    }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        LIVE_BLOCKS.fetch_sub(1, Ordering::Relaxed);
        unsafe { System.dealloc(ptr, layout) }
    }
}

#[global_allocator]
static ALLOCATOR: Counting = Counting;

/// `(module (func (export "f")))`
const EMPTY_EXPORT: &[u8] = &[
    0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
    0x03, 0x02, 0x01, 0x00, 0x07, 0x05, 0x01, 0x01, b'f', 0x00, 0x00, 0x0a, 0x04, 0x01,
    0x02, 0x00, 0x0b,
];

fn finish<F: Future>(future: F) -> F::Output {
    let mut future = pin!(future);
    let mut context = Context::from_waker(Waker::noop());
    loop {
        if let Poll::Ready(output) = future.as_mut().poll(&mut context) {
            return output;
        }
    }
}

#[test]
fn calls_leave_no_heap_blocks_behind() {
    const CALLS: isize = 10_000;
    let engine = wasmtime::Engine::default();
    let module = wasmtime::Module::new(&engine, EMPTY_EXPORT).unwrap();
    let mut store = wasmtime::Store::new(&engine, ());
    let linker = wasmtime::Linker::new(&engine);
    let instance = finish(linker.instantiate_async(&mut store, &module)).unwrap();
    let f = instance.get_typed_func::<(), ()>(&mut store, "f").unwrap();
    let mut call = |times: isize| {
        for _ in 0..times {
            finish(f.call_async(&mut store, ())).unwrap();
        }
    };
    call(100); // warm up caches
    let before = LIVE_BLOCKS.load(Ordering::Relaxed);
    call(CALLS);
    let left = LIVE_BLOCKS.load(Ordering::Relaxed) - before;
    assert!(left < CALLS / 100, "{CALLS} calls left {left} heap blocks behind");
}
Steps to Reproduce
  • Put the test above in a crate that depends on wasmtime with the async feature.
  • Run cargo test on Windows.
Expected Results

No heap blocks left behind after the calls (or close to none).

Actual Results

About 10,000 blocks are left behind on Windows, one per call. On Linux and macOS, none are.

Versions and Environment

Wasmtime version or commit: seen on 48.0.5 through 48.0.10; the same code is still on main

Operating system: Windows 11

Architecture: x86_64

Extra Info

Moving the closure out of the box right away frees the box before the fiber runs:

let func = *Box::from_raw((*state).initial_closure.get().cast::<F>());

I've been running that change as a local patch on 48.0.x and the leak is gone. Happy to send a PR.

主要语言
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 摘要。