{{ message }}
Repository navigation
fix(types): ExecProcess streams are undefined, not null, when absent - #7847
Open
PedroRosalba wants to merge 1 commit into
Open
PedroRosalba wants to merge 1 commit into
PedroRosalba wants to merge 1 commit into
Conversation
`ExecProcess.stdin`, `.stdout` and `.stderr` are `jsg::Optional` getters (`ExecProcess::getStdin/getStdout/getStderr`), and JSG wraps an empty `jsg::Optional` as `undefined`; only `kj::Maybe` becomes `null`. The hand-written `JSG_TS_OVERRIDE` declared all three as `X | null`, so `proc.stdout === null` never holds, and `if (proc.stderr !== null) proc.stderr.getReader()` compiles and throws when the stream was not provided. Absent is the common case: stdin unless `stdin: "pipe"` (or a PTY), stdout with `stdout: "ignore"`, stderr with `stderr: "ignore"` or `"combined"` (so also with a PTY). The Containers docs already describe the three fields as `| undefined`. Declare them optional (`readonly stdin?: WritableStream`, ...), which is what the generator emits for `jsg::Optional` properties elsewhere (`DurableObjectState.container?`, `ContainerSnapshot.name?`) and what the `DurableObjectStub.name?` override already does for a `jsg::Optional` getter. Types only; no runtime change. Verified on the built binary against a Docker container: in the default, `stdout: "ignore"` + `stderr: "ignore"`, `stderr: "combined"`, `stdin: "pipe"` and `pty: true` configurations every absent stream reads `typeof undefined` and `=== null` is false. Tests: //types:test/types/container (new; fails against the previous snapshot), //src/workerd/server/tests/container-client:container-client@ (existing runtime coverage; needs Docker). Precedent for an optional prototype getter in an override: Request.cf (jsg::Optional getCf, overridden as `readonly cf?: Cf` in http.h).
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.

Problem
ExecProcess(returned byctx.container.exec()) declaresreadonly stdin: WritableStream | null,readonly stdout: ReadableStream | null,readonly stderr: ReadableStream | null. The getters returnjsg::Optional<...>, and JSG wraps an emptyjsg::Optionalasundefined; onlykj::Maybebecomesnull. Absent is the common case:stdinunlessstdin: "pipe"(or a PTY),stdoutwithstdout: "ignore",stderrwithstderr: "ignore"or"combined"(so also with a PTY). As a resultproc.stdin === nullis never true, andif (proc.stdout !== null) proc.stdout.getReader()compiles and throwsTypeError: Cannot read properties of undefined.The Containers docs already say: "A stream that the process does not provide is undefined." and "The Workers TypeScript types declare stdin, stdout, and stderr as | null. Test for a stream, such as if (process.stderr), instead of comparing with null."
Fix
Declare the three as optional (
readonly stdin?: WritableStream, ...) in theJSG_TS_OVERRIDEinsrc/workerd/api/container.h, which is what the generator emits forjsg::Optionalproperties elsewhere (DurableObjectState.container?,ContainerSnapshot.name?) and whatRequest.cfdoes for ajsg::Optionalgetter in an override. Types only.Type-level breaking change: under
strictNullChecks,=== nullchecks on these fields become a compile error, which is the point: they were never true.Tests
bazel test //types:test/types/container(new; fails on the previous snapshot)//src/workerd/server/tests/container-client:container-client@(existing runtime coverage; needs Docker)stdout: "ignore"+stderr: "ignore",stderr: "combined",stdin: "pipe"andpty: trueconfigurations: every absent stream istypeof "undefined"and=== nullis false.just generate-typesreproduces the snapshot edits byte for byte.Tested on macOS (Apple Silicon) only.