-
-
Notifications
You must be signed in to change notification settings - Fork 20
fix: destroy piped streams on child exit to prevent grandchild deadlock #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf59661
test: verify exec completes when grandchild holds piped stdout
Mearman 9657bb5
fix: destroy piped streams on child exit to prevent grandchild deadlock
Mearman 07f9240
test: add grandchild fixture scripts for pipe inheritance tests
Mearman a253d7f
test: restructure grandchild tests to use static fixtures
Mearman 5537d3b
fix: use setImmediate instead of setTimeout for stream cleanup
Mearman 56af2d9
chore: clarify comment
43081j 1ee0b9f
test: move tests into main
43081j 99da935
fix: account for piped streams
43081j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
|
Mearman marked this conversation as resolved.
Outdated
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import {describe, test, expect} from 'vitest'; | ||
| import os from 'node:os'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import {spawnSync} from 'node:child_process'; | ||
|
|
||
| const isWindows = os.platform() === 'win32'; | ||
|
|
||
| function runInSubprocess( | ||
| childScript: string, | ||
| runnerBody: string | ||
| ): {status: number | null; signal: string | null; stdout: string} { | ||
| const dir = path.dirname(childScript); | ||
| const runnerScript = path.join(dir, 'runner.mjs'); | ||
| const distPath = JSON.stringify( | ||
| path.join(process.cwd(), 'dist', 'main.mjs') | ||
| ); | ||
| const childPath = JSON.stringify(childScript); | ||
|
|
||
| fs.writeFileSync( | ||
| runnerScript, | ||
| `import { x } from ${distPath}\n${runnerBody.replace(/CHILD_SCRIPT/g, childPath)}` | ||
| ); | ||
|
|
||
| try { | ||
| const proc = spawnSync('node', [runnerScript], { | ||
| timeout: 10000, | ||
| encoding: 'utf8', | ||
| killSignal: 'SIGKILL' | ||
| }); | ||
|
|
||
| return { | ||
| status: proc.status, | ||
| signal: proc.signal, | ||
| stdout: proc.stdout ?? '' | ||
| }; | ||
| } finally { | ||
| try { | ||
| spawnSync('pkill', ['-f', dir]); | ||
| } catch {} | ||
| fs.rmSync(dir, {recursive: true, force: true}); | ||
| } | ||
| } | ||
|
|
||
| describe.skipIf(isWindows)('exec (grandchild pipe inheritance)', () => { | ||
| test('await completes when grandchild holds piped stdout open', async () => { | ||
| const dir = fs.mkdtempSync( | ||
| path.join(os.tmpdir(), 'tinyexec-grandchild-') | ||
| ); | ||
| const childScript = path.join(dir, 'child.mjs'); | ||
|
|
||
| fs.writeFileSync( | ||
| childScript, | ||
| `import { spawn } from 'node:child_process' | ||
| console.log('output') | ||
| spawn('node', ['-e', 'setTimeout(() => void 0, 30000)'], { | ||
| stdio: ['ignore', 1, 'ignore'], | ||
| }) | ||
| process.exit(0) | ||
| ` | ||
| ); | ||
|
|
||
| const result = runInSubprocess( | ||
| childScript, | ||
| ` | ||
| const result = await Promise.race([ | ||
| x('node', [CHILD_SCRIPT]).then(() => 'completed'), | ||
| new Promise((resolve) => setTimeout(() => resolve('hung'), 5000)), | ||
| ]) | ||
| process.stdout.write(result) | ||
| process.exit(result === 'completed' ? 0 : 1) | ||
| ` | ||
| ); | ||
|
|
||
| if (result.signal === 'SIGKILL') { | ||
| expect.unreachable( | ||
| 'exec hung for 10s (grandchild held pipe open)' | ||
| ); | ||
| } | ||
|
|
||
| expect(result.status).toBe(0); | ||
| expect(result.stdout.trim()).toBe('completed'); | ||
| }); | ||
|
|
||
| test('async iterator completes when grandchild holds piped stdout open', async () => { | ||
|
Mearman marked this conversation as resolved.
Outdated
|
||
| const dir = fs.mkdtempSync( | ||
| path.join(os.tmpdir(), 'tinyexec-grandchild-') | ||
| ); | ||
| const childScript = path.join(dir, 'child.mjs'); | ||
|
|
||
| fs.writeFileSync( | ||
| childScript, | ||
| `import { spawn } from 'node:child_process' | ||
| console.log('line1') | ||
| console.log('line2') | ||
| spawn('node', ['-e', 'setTimeout(() => void 0, 30000)'], { | ||
| stdio: ['ignore', 1, 'ignore'], | ||
| }) | ||
| process.exit(0) | ||
| ` | ||
| ); | ||
|
|
||
| const result = runInSubprocess( | ||
| childScript, | ||
| ` | ||
| const lines = [] | ||
| const result = await Promise.race([ | ||
| (async () => { | ||
| for await (const line of x('node', [CHILD_SCRIPT])) { | ||
| lines.push(line) | ||
| } | ||
| return 'completed' | ||
| })(), | ||
| new Promise((resolve) => setTimeout(() => resolve('hung'), 5000)), | ||
| ]) | ||
| process.stdout.write(JSON.stringify({ result, lines })) | ||
| process.exit(result === 'completed' ? 0 : 1) | ||
| ` | ||
| ); | ||
|
|
||
| if (result.signal === 'SIGKILL') { | ||
| expect.unreachable( | ||
| 'async iterator hung for 10s (grandchild held pipe open)' | ||
| ); | ||
| } | ||
|
|
||
| expect(result.status).toBe(0); | ||
| const parsed = JSON.parse(result.stdout.trim()); | ||
| expect(parsed.result).toBe('completed'); | ||
| expect(parsed.lines).toEqual(['line1', 'line2']); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.