Skip to content

Commit c00b491

Browse files
committed
Reject provider success with unfinished stdin.
1 parent 27f1974 commit c00b491

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎packages/void/src/cli/provision.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,13 @@ export function createDefaultRunWrangler(
13081308
child.stdin?.on('error', handleProcessError);
13091309
child.on('close', (code, signal) => {
13101310
closed = true;
1311+
// Node destroys stdin on child exit without necessarily emitting an
1312+
// error. Exit 0 cannot confirm success while input remains unwritten.
1313+
if (code === 0 && child.stdin && !child.stdin.writableFinished) {
1314+
handleProcessError(
1315+
new Error('the Cloudflare operation process closed stdin before all input was written'),
1316+
);
1317+
}
13111318
if (timedOut || postSpawnError !== undefined) {
13121319
// That path settles once the group is also gone; nudge its poll.
13131320
nudgeConfirmPolls();

‎packages/void/test/unit/provision-runner-signals.test.ts‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ describe('createDefaultRunWrangler — post-spawn child `error` events confirm/q
697697
}
698698
}, 15_000);
699699

700-
it('a provider exiting after reporting success rejects unread stdin through its real pipe error', async () => {
700+
it('a provider exiting after reporting success rejects unread stdin', async () => {
701701
const script = join(POST_SPAWN_TMP, 'exited-stdin.cjs');
702702
const pidFile = join(POST_SPAWN_TMP, 'exited-stdin.pid');
703703
writeFileSync(
@@ -706,8 +706,8 @@ describe('createDefaultRunWrangler — post-spawn child `error` events confirm/q
706706
`fs.writeFileSync(${JSON.stringify(pidFile)}, String(process.pid));\n` +
707707
`fs.writeSync(1, JSON.stringify({ operationResult: 1, data: { id: 'provider-id' } }));\n` +
708708
// Exit closes every inherited handle on Windows as well as POSIX. The
709-
// oversized unread input is still pending, so the parent gets a real
710-
// closed-pipe error (write EOF on Windows, EPIPE on POSIX).
709+
// oversized unread input is still pending. Node may report a pipe
710+
// error or silently destroy stdin; neither can count as success.
711711
`process.exit(0);\n`,
712712
);
713713
let observedOutput = '';
@@ -748,9 +748,7 @@ describe('createDefaultRunWrangler — post-spawn child `error` events confirm/q
748748
expect(result!.status).toBe(1);
749749
// The stdin error owns termination even if exit 0 is concurrently observed.
750750
expect(result!.signal).toBe('SIGKILL');
751-
expect(result!.stderr).toMatch(
752-
process.platform === 'win32' ? /write EOF|EPIPE|broken pipe/i : /EPIPE|broken pipe/i,
753-
);
751+
expect(result!.stderr).toMatch(/write EOF|EPIPE|broken pipe|closed stdin before/i);
754752
expect(result!.stdout).toContain('"operationResult":1');
755753
expect(result!.data).toBeUndefined();
756754
}
@@ -760,6 +758,60 @@ describe('createDefaultRunWrangler — post-spawn child `error` events confirm/q
760758
expect(() => process.kill(pid, 0)).toThrow();
761759
}, 15_000);
762760

761+
it('rejects success when stdin closes without finishing or emitting an error', async () => {
762+
const script = join(POST_SPAWN_TMP, 'unfinished-stdin.cjs');
763+
writeFileSync(
764+
script,
765+
`process.stdout.write(JSON.stringify({ operationResult: 1, data: { id: 'provider-id' } }));\n`,
766+
);
767+
let stdinErrors = 0;
768+
const run = createRunner({
769+
launcher: [process.execPath, script],
770+
onSpawnedChild: (child) => {
771+
child.stdin!.on('error', () => stdinErrors++);
772+
// Node destroys stdin when the child exits. A destroyed Writable need
773+
// not emit an error, even when its input was never flushed.
774+
child.stdin!.destroy();
775+
},
776+
});
777+
const result = await run(
778+
{
779+
type: 'hyperdrive.create',
780+
name: 'unfinished-stdin',
781+
binding: 'HYPERDRIVE',
782+
configPath: 'config.jsonc',
783+
},
784+
{ accountId: 'acct', input: 'unread input' },
785+
);
786+
expect(stdinErrors).toBe(0);
787+
expect(result.status).toBe(1);
788+
expect(result.data).toBeUndefined();
789+
expect(result.stderr).toContain('stdin');
790+
});
791+
792+
it('accepts success after the provider reads all stdin', async () => {
793+
const script = join(POST_SPAWN_TMP, 'finished-stdin.cjs');
794+
writeFileSync(
795+
script,
796+
`let length = 0;\n` +
797+
`process.stdin.on('data', chunk => { length += chunk.length; });\n` +
798+
`process.stdin.on('end', () => process.stdout.write(JSON.stringify({ operationResult: 1, data: { length } })));\n`,
799+
);
800+
const run = createRunner({ launcher: [process.execPath, script] });
801+
const input = 'x'.repeat(2 * 1024 * 1024);
802+
const result = await run(
803+
{
804+
type: 'hyperdrive.create',
805+
name: 'finished-stdin',
806+
binding: 'HYPERDRIVE',
807+
configPath: 'config.jsonc',
808+
},
809+
{ accountId: 'acct', input },
810+
);
811+
expect(result.status).toBe(0);
812+
expect(result.data).toEqual({ length: input.length });
813+
});
814+
763815
it('a stdin Writable error rejects buffered success only after the child is confirmed dead', async () => {
764816
const script = join(POST_SPAWN_TMP, 'stdin-error.cjs');
765817
const pidFile = join(POST_SPAWN_TMP, 'stdin-error.pid');

0 commit comments

Comments
 (0)