Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/tui/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import { createTuiAttention } from "./attention"
import * as TuiAudio from "./audio"
import { win32DisableProcessedInput, win32FlushInputBuffer } from "./terminal-win32"
import { destroyRenderer } from "./util/renderer"
import { createSigcontResume } from "./util/sigcont-resume"
import { cliErrorMessage, errorFormat } from "./util/error"

registerOpencodeSpinner()
Expand Down Expand Up @@ -441,9 +442,11 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi
},
{ priority: 1 },
)
const sigcont = createSigcontResume(renderer)
onCleanup(() => {
offSelectionKeys()
attention.dispose()
sigcont.dispose()
})

// Wire up console copy-to-clipboard via opentui's onCopySelection callback
Expand Down Expand Up @@ -947,9 +950,10 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi
// attention signals fire then. See docs/tui/background.md.
// The terminal is only handed over once suspend() has restored it.
await renderer.suspend()
process.once("SIGCONT", () => {
void renderer.resume().catch(() => {})
})
// A second suspend before SIGCONT would stack another once-listener
// and resume twice; arm() drops the pending one first. The renderer
// can also be destroyed while stopped, so the resume is guarded (#426).
sigcont.arm()
process.kill(0, "SIGTSTP")
},
},
Expand Down
26 changes: 26 additions & 0 deletions packages/tui/src/util/sigcont-resume.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Arms a one-shot SIGCONT listener that resumes a suspended renderer.
// Re-arming drops a still-pending listener so two suspends before SIGCONT
// cannot stack listeners and resume twice, and a renderer destroyed while
// stopped is never resumed (#426).
export function createSigcontResume(renderer: { isDestroyed: boolean; resume(): Promise<void> }) {
let pending: (() => void) | undefined

function dispose() {
if (pending) process.removeListener("SIGCONT", pending)
pending = undefined
}

return {
arm() {
dispose()
const onSigcont = () => {
pending = undefined
if (renderer.isDestroyed) return
void renderer.resume().catch(() => {})
}
pending = onSigcont
process.once("SIGCONT", onSigcont)
},
dispose,
}
}
39 changes: 39 additions & 0 deletions packages/tui/test/util/sigcont-resume.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// #426: SIGCONT resume dedupe and cleanup.
import { expect, mock, test } from "bun:test"
import { createSigcontResume } from "../../src/util/sigcont-resume"

function fakeRenderer() {
return { isDestroyed: false, resume: mock(() => Promise.resolve()) }
}

test("re-arming dedupes the pending SIGCONT listener", () => {
const baseline = process.listenerCount("SIGCONT")
const renderer = fakeRenderer()
const sigcont = createSigcontResume(renderer)
sigcont.arm()
sigcont.arm()
expect(process.listenerCount("SIGCONT")).toBe(baseline + 1)
process.emit("SIGCONT")
expect(renderer.resume).toHaveBeenCalledTimes(1)
expect(process.listenerCount("SIGCONT")).toBe(baseline)
})

test("dispose drops the pending listener so a later SIGCONT is a no-op", () => {
const baseline = process.listenerCount("SIGCONT")
const renderer = fakeRenderer()
const sigcont = createSigcontResume(renderer)
sigcont.arm()
sigcont.dispose()
expect(process.listenerCount("SIGCONT")).toBe(baseline)
process.emit("SIGCONT")
expect(renderer.resume).not.toHaveBeenCalled()
})

test("a renderer destroyed while stopped is not resumed", () => {
const renderer = fakeRenderer()
const sigcont = createSigcontResume(renderer)
sigcont.arm()
renderer.isDestroyed = true
process.emit("SIGCONT")
expect(renderer.resume).not.toHaveBeenCalled()
})
Loading