Skip to content

fix(mcp): memory leak — WebSocket frame payload not freed in handleConnection loop#208

Merged
gHashTag merged 1 commit into
mainfrom
feat/issue-205
Mar 11, 2026
Merged

fix(mcp): memory leak — WebSocket frame payload not freed in handleConnection loop#208
gHashTag merged 1 commit into
mainfrom
feat/issue-205

Conversation

@gHashTag
Copy link
Copy Markdown
Owner

Summary

  • Added allocated: bool field to Frame struct to track when payload memory was allocated during unmasking of masked WebSocket frames
  • Added defer statement in handleConnection() loop to free the payload when it was allocated, preventing memory leak

Problem

tools/mcp/trinity_mcp/websocket_transport.zig had a memory leak in the handleConnection() loop. When parseFrame() processed a masked WebSocket frame, it allocated memory for the unmasked payload. But after processing the frame in the switch statement, the payload was never freed.

Fix

The fix adds an allocated boolean to the Frame struct that is set to true when memory is allocated for unmasking, and then a defer statement in handleConnection() frees the payload when needed:

defer if (frame.allocated) self.allocator.free(frame.payload);

Test Plan

  • zig build compiles successfully
  • zig ast-check passes
  • Frame payload is freed after each iteration of the handleConnection loop
  • No memory leak on masked WebSocket frames

Closes #205

🤖 Generated with Claude Code

…nnection loop (#205)

Added `allocated` flag to Frame struct to track when payload memory was
allocated during unmasking. Added defer in handleConnection to free the
payload when it was allocated, preventing memory leak on masked frames.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gHashTag gHashTag merged commit 4bef593 into main Mar 11, 2026
4 of 6 checks passed
gHashTag added a commit that referenced this pull request Mar 18, 2026
fix(mcp): memory leak — WebSocket frame payload not freed in handleConnection loop
@gHashTag gHashTag deleted the feat/issue-205 branch April 3, 2026 11:01
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.

fix(mcp): memory leak — WebSocket frame payload not freed in handleConnection loop

1 participant