feat(worker_threads): worker.stdin Writable (parent→worker process.stdin bridge) (#1076)#1251
Merged
Conversation
…din bridge) (#1076) Follow-up to #1003, closing the last stdio gap of epic #996. With `stdin: true`, `worker.stdin` is now a Writable on the parent whose writes are bridged into the worker's `process.stdin` Readable (the reverse of #1003's worker→parent stdout). The worker's `process.stdin` previously resolved to the process-global `SharpTSStdin.Instance`, which reads the real `Console.In` — so a worker would consume the host terminal. Fix: a `[ThreadStatic] WorkerThreads.WorkerStdin` (mirroring `ClusterContext`) installed on the worker thread, consulted by `ProcessBuiltIns.GetOwnMember`. It is always isolated (even without `stdin: true`) so a worker never touches the terminal, and only fed when the parent opts in. The bridge reuses the existing `WorkerMessageHandler` poll timer (new `PumpStdin`) draining a dedicated queue — no new cross-thread marshaling primitive. The parent `_stdin` Writable's write/final callbacks mirror the child_process stdin bridge. Dual-mode tests: write→'data' + end→'end' roundtrip, multi-write ordering, and un-piped stdin is absent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1076. Follow-up to #1003, closing the last stdio gap of epic #996.
What
With
stdin: true,worker.stdinis now a Writable on the parent whose writes are bridged into the worker'sprocess.stdinReadable — the parent→worker mirror of #1003's worker→parentstdout/stderr.worker.stdin.write(chunk)surfaces as'data'inside the worker;worker.stdin.end()surfaces as'end'.The wrinkle (and a latent bug fixed)
The worker's
process.stdinresolved to the process-globalSharpTSStdin.Instance, which spawns a background thread reading the realConsole.In— so a worker would consume the host terminal.Fix: a
[ThreadStatic] WorkerThreads.WorkerStdin(mirroring the existingClusterContextpattern), installed on the worker's dedicated thread inRunWorkerScriptand consulted byProcessBuiltIns.GetOwnMember("stdin"). It is always isolated to a per-workerSharpTSReadable(even withoutstdin: true) so a worker never touches the terminal, and is only fed from the parent whenstdin: trueis passed.Bridge
worker.stdinis aSharpTSWritablecreated only onstdin: true(elseGetMemberreturns null → reads asundefined, consistent withworker.stdout/stderr).child_processstdin bridge) enqueue chunks / a null-EOF sentinel onto a dedicatedBlockingCollection.WorkerMessageHandlerpoll timer drains it each tick via newPumpStdin→PushFromHostinto the worker'sprocess.stdin(null ⇒'end'). No new cross-thread marshaling primitive; same lifecycle/guards as the message queue.A dedicated queue (not the shared
ClonedMessagerecord) is used because stdin andpostMessageare separate channels in Node.Scope note
A worker started without
stdin: truegets an isolated stdin that stays open (never emits'end') rather than ending immediately as Node does — strictly better than the prior behavior and not worth the edge-case complexity here.Tests (dual-mode)
worker.stdin.write(...)→ readable in the worker viaprocess.stdin'data';end()→'end'.'end').worker.stdinis absent (falsy).Verification