Skip to content

fix: free WASM guest request allocation in host functions#369

Merged
nicklasl merged 3 commits intomainfrom
nicklasl/wasm-memory-leak
Apr 14, 2026
Merged

fix: free WASM guest request allocation in host functions#369
nicklasl merged 3 commits intomainfrom
nicklasl/wasm-memory-leak

Conversation

@nicklasl
Copy link
Copy Markdown
Member

Summary

  • Fix memory leak where wasm_msg_host_current_time host function never frees the request pointer allocated by the WASM guest
  • Each resolve_flags/apply_flags call leaked ~20 bytes of WASM linear memory (which can only grow, never shrink)
  • Over days of continuous operation this caused unbounded memory growth (observed 50MB → 300MB over a week)
  • Go, JS, and Python host functions now free the incoming request pointer. Java was already correct.
  • Add WASM memory regression tests for all four providers

Test plan

  • Go: TestWasmMemoryStableOnRepeatedResolveCalls — 50k resolves, zero memory growth
  • JS: wasm memory stability — 50k resolves, zero memory growth
  • Python: test_multiple_resolves_dont_leak_memory — 50k resolves, zero memory growth
  • Java: wasmMemoryStableOnRepeatedResolveCalls — 50k resolves, zero memory growth

🤖 Generated with Claude Code

The wasm_msg_host_current_time host function receives a request pointer
from the WASM guest but never frees it, leaking ~20 bytes per
resolve_flags/apply_flags call. Over days this causes unbounded WASM
linear memory growth.

Fix by consuming the request pointer in Go, JS, and Python host
functions. Java was already correct.

Add WASM memory regression tests for all four providers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
nicklasl and others added 2 commits April 14, 2026 12:01
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Align Go, JS, and Python host function plumbing with Java's pattern:
- consumeRequest: read + decode + free the wasm-msg Request envelope
- transferResponseSuccess: wrap data in a wasm-msg Response and transfer
- transferResponseError: wrap error in a wasm-msg Response and transfer

Host functions now use the same consume→handle→transfer pattern across
all providers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nicklasl nicklasl marked this pull request as ready for review April 14, 2026 12:05
Copy link
Copy Markdown
Contributor

@andreas-karlsson andreas-karlsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@nicklasl nicklasl merged commit dd7ddac into main Apr 14, 2026
10 checks passed
@nicklasl nicklasl deleted the nicklasl/wasm-memory-leak branch April 14, 2026 12:42
@github-actions github-actions bot mentioned this pull request Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants