Skip to content

perf(terminal): defer MessagePort ACK to xterm write callback#4862

Merged
gregpriday merged 4 commits intodevelopfrom
perf/issue-4849-messageport-ack-gates
Apr 2, 2026
Merged

perf(terminal): defer MessagePort ACK to xterm write callback#4862
gregpriday merged 4 commits intodevelopfrom
perf/issue-4849-messageport-ack-gates

Conversation

@gregpriday
Copy link
Copy Markdown
Collaborator

Summary

  • The MessagePort path was ACK-ing data on transport receipt, before xterm had parsed it. This let the pty-host keep sending data at wire speed regardless of how fast xterm was consuming it, making the renderer-side 128KB/32KB watermarks ineffective.
  • ACK is now deferred to the terminal.write() callback, matching the IPC path's behaviour and making the watermark system the actual flow control gate as intended.
  • The byte count used for ACK is the original pty-host byte count from the message, not a re-encoded length, to keep the pty-host's accounting consistent.

Resolves #4849

Changes

  • src/clients/terminalClient.ts: removed immediate ACK in the MessagePort message event handler; callbacks now receive id and byteCount so callers can defer ACK to after xterm consumption
  • src/services/terminal/TerminalInstanceService.ts: passes id and byteCount through to the existing terminal.write() callback alongside the IPC path's ACK call
  • src/clients/__tests__/terminalClient.test.ts: updated and extended tests covering deferred ACK behaviour, byte count passthrough, and the multi-callback fan-out

Testing

Unit tests updated and passing. The change aligns MessagePort backpressure semantics with the existing IPC path, which has been stable in production.

- Split ACK in installPortDataHandler: defer when live callbacks exist,
  immediate for early-buffered data (no xterm involved yet)
- Add acknowledgePortData() method routing ACK via active MessagePort
- Call acknowledgePortData in writeToTerminal for all three paths:
  hibernated, deferred-restore, and normal xterm write callback
- Update tests: verify no immediate ACK on live-callback path,
  immediate ACK preserved for early-buffer, deferred ACK works
- Add pendingPortAckBytes FIFO queue preserving msg.bytes from pty-host
- acknowledgePortData pops from queue to avoid UTF-16 vs UTF-8 mismatch
- Skip port ACK when queue is empty (IPC data or early-buffer flush),
  preventing double-ACK for early-buffered data
- Add tests for byte count preservation and empty-queue no-op behavior
…ce tests

- Add acknowledgePortData: vi.fn() to terminalClient mock in 9 test files
  that mock the terminalClient module
@gregpriday gregpriday merged commit b3dfc2b into develop Apr 2, 2026
6 of 8 checks passed
@gregpriday gregpriday deleted the perf/issue-4849-messageport-ack-gates branch April 2, 2026 13:15
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.

MessagePort ACK gates transport receipt, not xterm consumption

1 participant