Memory leak on Windows: every new fiber leaks its start closure
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- rust, wasm
- Lĩnh vực
- backend, performance
Hướng nghiên cứu
Đọc crates/fiber/src/windows.rs, tập trung vào fiber_start và cách nó xử lý initial_closure; so sánh với bản sửa lỗi Unix trong #12928. Chạy bài kiểm thử đếm số lần cấp phát được cung cấp trên Windows trước và sau khi thay đổi. Hoàn thành khi các lần gọi call_async lặp lại hầu như không để lại khối nào trên heap.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
wasmtimewith theasyncfeature. - Run
cargo teston 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.
- Ngôn ngữ chính
- Rust
- Star
- 18.9k
- Fork
- 2k
- Merge trung bình
- 1 ngày 6 giờ
- Pull request đã merge (30 ngày)
- 238
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của bytecodealliance/wasmtime
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
bytecodealliance/wasmtime#15536 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
wasi-preview1-component-adapter: fd_write does not invalidate cached InputStream, causing stale readsCó thể đã có người làm @dpfens đã nhận 13 ngày trước. Đang mởwasi
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
bytecodealliance/wasmtime#15296 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug wasm-proposal:exceptions wasmtime:debugging
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
bytecodealliance/wasmtime#14102 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
wasm-proposal:gc
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
bytecodealliance/wasmtime#13808 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`Func::call` aborts on OOM when Wasm traps (with name section) or throwsCó thể đã có người làm @cfallin đã nhận hôm nay. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
bytecodealliance/wasmtime#15529 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của bytecodealliance/wasmtime
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
valkey-io/valkey-glide#7975 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
marklauter/tsong#110 ·
-
docs enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
wasm-bindgen/wasm-bindgen#5503 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
vortex-data/vortex#12633 ·
Maintainer thường phản hồi trong vòng 1 ngày