Skip to content

fix: add end-to-end flush semantics for flush(cb)#198

Merged
mcollina merged 2 commits intomainfrom
fix/197-cross-thread-flush
Mar 5, 2026
Merged

fix: add end-to-end flush semantics for flush(cb)#198
mcollina merged 2 commits intomainfrom
fix/197-cross-thread-flush

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented Mar 4, 2026

Summary

Fixes #197 by making ThreadStream#flush(cb) wait for end-to-end worker destination flush, not only shared-buffer drain.

Changes

  • Added cross-thread flush protocol:
    • main thread sends { code: 'FLUSH', context: 'thread-stream', id }
    • worker replies { code: 'FLUSHED', context: 'thread-stream', id }
  • Added context validation on both sides to prevent message collisions with other libraries using worker message channels.
  • flush(cb) now:
    1. waits for local/shared buffer drain
    2. waits for worker flush acknowledgment
  • Added tracking for pending flush callbacks and failure propagation on worker exit/destroy.
  • Worker flush behavior now supports:
    • destination.flush(cb) (including promise/sync variants)
    • fallback to destination.flushSync()
    • fallback wait for drain when backpressured
    • immediate ack when no flush primitive is available
  • Added tests for all requested flush semantics and worker-exit failure path.
  • Added missing flush(cb?) declaration in index.d.ts.
  • Updated README to describe stronger async flush semantics.

Validation

  • npm run lint
  • npm run test:ci
  • npm run build

Fixes #197

mcollina added 2 commits March 4, 2026 20:13
Implement cross-thread FLUSH/FLUSHED protocol so ThreadStream#flush(cb) waits for worker destination flush.

- add context-scoped FLUSH request/ack handling between main thread and worker
- queue and resolve pending flush callbacks by id
- fail pending flush callbacks on worker exit/destroy
- support worker destination flush via flush(cb)/flushSync()/drain fallback
- add tests for flush behavior and worker-exit failure path
- add missing flush(cb?) type signature and document stronger flush semantics

Fixes #197
Register FLUSH control listener only after worker setup completes and guard flush requests until ready.

This prevents the internal FLUSH channel from racing with userland parentPort.once('message') handlers (e.g. post-message test), while preserving context-scoped control messages.
@mcollina mcollina merged commit 52c002a into main Mar 5, 2026
29 of 33 checks passed
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.

Add end-to-end cross-thread flush semantics for ThreadStream#flush(cb)

2 participants