Skip to content

Commit ba65678

Browse files
committed
fix(hub): keep restart from closing the new run's stream
1 parent f9ef820 commit ba65678

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

‎packages/hub/src/node/__tests__/host-terminals.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
44
import { tmpdir } from 'node:os'
55
import { join } from 'node:path'
66
import process from 'node:process'
7+
import { exec } from 'tinyexec'
78
import { describe, expect, it, vi } from 'vitest'
89
import { hasNative } from 'zigpty'
910
import { DevframeTerminalsHost } from '../host-terminals'
@@ -14,6 +15,8 @@ const zigptyModuleMock = vi.hoisted(() => ({
1415
spawn: vi.fn(),
1516
}))
1617

18+
vi.mock('tinyexec', { spy: true })
19+
1720
vi.mock('zigpty', async (importOriginal) => {
1821
const originalModule = await importOriginal<typeof import('zigpty')>()
1922
zigptyModuleMock.spawn.mockImplementation(originalModule.spawn)
@@ -337,6 +340,37 @@ describe('devframeTerminalHost stream lifecycle', () => {
337340
}
338341
})
339342

343+
it.skipIf(process.platform === 'win32')('keeps streaming when the old command exits during restart', async () => {
344+
const { host, sinks } = createTerminalHost()
345+
const session = await host.startChildProcess({
346+
command: NODE,
347+
args: ['-e', 'console.log(String(process.pid)); process.stdin.resume()'],
348+
}, { id: 'child', title: 'Child' })
349+
const previous = session.getResult()
350+
const child = session.getChildProcess()!
351+
const snapshot = vi.mocked(exec)
352+
try {
353+
snapshot.mockImplementationOnce((command) => {
354+
expect(command).toBe('ps')
355+
// Hold the snapshot open until the old command closes.
356+
const ps = exec(NODE, ['-e', 'process.stdin.resume()'])
357+
child.once('close', () => ps.process?.stdin?.end())
358+
child.stdin!.end()
359+
return ps
360+
})
361+
await session.restart()
362+
const pid = session.getResult().pid
363+
await waitUntil(() => expect(sinks.get('child')?.write).toHaveBeenCalledWith(`${pid}\n`), 5000)
364+
expect(session.status).toBe('running')
365+
expect((await previous).exitCode).toBeUndefined()
366+
await session.restart()
367+
}
368+
finally {
369+
snapshot.mockReset()
370+
await session.terminate()
371+
}
372+
})
373+
340374
it('getResult() tracks the new run after restart()', async () => {
341375
const { host } = createTerminalHost()
342376

‎packages/hub/src/node/host-terminals.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export class DevframeTerminalsHost implements DevframeTerminalsHostType {
343343
})
344344

345345
const settle = (exitCode: number | undefined) => {
346-
if (settled || currentRun !== runId)
346+
if (settled)
347347
return
348348
settled = true
349349
resolveOutput({
@@ -354,19 +354,15 @@ export class DevframeTerminalsHost implements DevframeTerminalsHostType {
354354
}
355355

356356
cp.process?.stdout?.on('data', (chunk: Buffer | string) => {
357-
if (currentRun !== runId)
358-
return
359357
const text = chunk.toString()
360358
stdoutChunks.push(text)
361-
if (!state.streamClosed)
359+
if (!state.streamClosed && currentRun === runId)
362360
state.controller?.enqueue(text)
363361
})
364362
cp.process?.stderr?.on('data', (chunk: Buffer | string) => {
365-
if (currentRun !== runId)
366-
return
367363
const text = chunk.toString()
368364
stderrChunks.push(text)
369-
if (!state.streamClosed)
365+
if (!state.streamClosed && currentRun === runId)
370366
state.controller?.enqueue(text)
371367
})
372368
cp.process?.once('error', (error) => {
@@ -419,7 +415,8 @@ export class DevframeTerminalsHost implements DevframeTerminalsHostType {
419415
const restart = async () => {
420416
if (state.streamClosed)
421417
throw diagnostics.DF8206({ id: terminal.id })
422-
// Wait for the old tree to go away so the new run can reclaim its ports.
418+
// Keep the old run from closing the stream while its process tree stops.
419+
runId++
423420
await killRun(cp)
424421
cp = createChildProcess()
425422
markStatus('running')

0 commit comments

Comments
 (0)