Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(turnrewind): reject externally killed git subprocesses

yingtianlan04bc244c4478
diff --git a/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js b/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js
index bb0625f0..4f4887a0 100644
--- a/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js
+++ b/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js
@@ -34,7 +34,7 @@ const GIT_SUBPROCESS_TIMEOUT_MS = 5 * 60 * 1000
* used to be synchronous, which froze the whole host — every session, the web
* UI and the health check — for the duration of `git add` on big workspaces.
*/
-function runGit(repoDir, workspaceDir, args, extraEnv = {}, maxBytes = MAX_OUTPUT_BYTES) {
+export function runGit(repoDir, workspaceDir, args, extraEnv = {}, maxBytes = MAX_OUTPUT_BYTES) {
return new Promise((resolvePromise, rejectPromise) => {
const child = spawn('git', ['-c', 'core.quotepath=false', '--git-dir', repoDir, '--work-tree', workspaceDir, ...args], {
cwd: workspaceDir,
@@ -70,16 +70,17 @@ function runGit(repoDir, workspaceDir, args, extraEnv = {}, maxBytes = MAX_OUTPU
errors.push(chunk)
})
child.on('error', error => fail(new Error(`TURNREWIND_GIT_EXEC: ${error.message}`)))
- child.on('close', (code) => {
+ child.on('close', (code, signal) => {
if (settled)
return
settled = true
clearTimeout(timeout)
const stdout = Buffer.concat(chunks)
- // code null = killed by the timeout's SIGKILL; the timeout already
- // rejected the promise, this only guards the normal exit path.
- if (code !== null && code !== 0) {
- const detail = Buffer.concat(errors).toString('utf8').trim() || stdout.toString('utf8').trim() || `exit ${code}`
+ // A clean exit is the only path that resolves (see gitExitIsClean).
+ // Our own timeout kill already settled above, so anything unclean
+ // here is an external kill with truncated output.
+ if (!gitExitIsClean(code, signal)) {
+ const detail = Buffer.concat(errors).toString('utf8').trim() || stdout.toString('utf8').trim() || (signal ? `killed by ${signal}` : `exit ${code}`)
rejectPromise(new Error(`TURNREWIND_GIT_FAILED: ${detail}`))
return
}
@@ -88,6 +89,20 @@ function runGit(repoDir, workspaceDir, args, extraEnv = {}, maxBytes = MAX_OUTPU
})
}
+/**
+ * Pure verdict for a git subprocess exit: only a clean exit (code 0, no
+ * signal) may resolve. code null or a signal means the process died
+ * externally (OOM killer, taskkill, antivirus, host suspension) and any
+ * buffered output is a truncated half-stream that must reject - resolving
+ * would e.g. commit a half-finished 'git add' as the turn snapshot.
+ */
+export function gitExitIsClean(code, signal) {
+ // Node passes signal as null (and older versions may pass undefined) on
+ // a clean exit; treat both as no-signal.
+ const noSignal = signal === null || signal === undefined
+ return code === 0 && noSignal
+}
+
async function runGitText(repoDir, workspaceDir, args, extraEnv = {}) {
const output = await runGit(repoDir, workspaceDir, args, extraEnv)
return output.toString('utf8')
@@ -99,6 +114,9 @@ let gitProbeFailedAt = 0
/** Re-probe a failed availability check after this long (git may be installed while the host runs). */
const GIT_PROBE_RETRY_MS = 5 * 60 * 1000
+/** The availability probe feeds the pre-step barrier: a hung `git --version` must not park a turn forever. */
+const GIT_PROBE_TIMEOUT_MS = 30 * 1000
+
/**
* Whether a usable git executable is on PATH. Successful probes are cached for
* the host process; failed probes expire after GIT_PROBE_RETRY_MS so a git
@@ -113,18 +131,33 @@ export function gitAvailable() {
gitProbeResult ??= new Promise((resolvePromise) => {
const child = spawn('git', ['--version'])
let settled = false
+ // The probe feeds the pre-step barrier: without a budget a hung
+ // `git --version` would park the barrier forever. A timed-out or
+ // externally-killed probe counts as unavailable (retried after TTL).
+ let probeTimeout
+ const settle = (value) => {
+ if (settled)
+ return undefined
+ settled = true
+ clearTimeout(probeTimeout)
+ resolvePromise(value)
+ }
const markFailure = () => {
gitProbeFailedAt = Date.now()
}
- const settle = value => (settled ? undefined : (settled = true, resolvePromise(value)))
+ probeTimeout = setTimeout(() => {
+ markFailure()
+ child.kill('SIGKILL')
+ settle(false)
+ }, GIT_PROBE_TIMEOUT_MS)
child.on('error', () => {
markFailure()
settle(false)
})
- child.on('close', (code) => {
- if (code !== 0)
+ child.on('close', (code, signal) => {
+ if (code !== 0 || signal !== null)
markFailure()
- settle(code === 0)
+ settle(code === 0 && signal === null)
})
})
return gitProbeResult
@@ -154,13 +187,14 @@ async function ensureRepository(store) {
clearTimeout(timeout)
rejectPromise(new Error(`TURNREWIND_GIT_INIT: ${error.message}`))
})
- child.on('close', (code) => {
+ child.on('close', (code, signal) => {
if (settled)
return
settled = true
clearTimeout(timeout)
- if (code !== 0) {
- rejectPromise(new Error(`TURNREWIND_GIT_INIT: ${Buffer.concat(errors).toString('utf8').trim() || 'git init failed'}`))
+ if (!gitExitIsClean(code, signal)) {
+ const detail = Buffer.concat(errors).toString('utf8').trim() || (signal ? `killed by ${signal}` : `exit ${code}`)
+ rejectPromise(new Error(`TURNREWIND_GIT_FAILED: ${detail}`))
return
}
resolvePromise()
@@ -398,15 +432,18 @@ function runGitStdin(repoDir, workspaceDir, args, input) {
child.stdout.on('data', chunk => chunks.push(chunk))
child.stderr.on('data', chunk => errors.push(chunk))
child.on('error', error => fail(new Error(`TURNREWIND_GIT_EXEC: ${error.message}`)))
- child.on('close', (code) => {
+ child.on('close', (code, signal) => {
if (settled)
return
- settled = true
- clearTimeout(timeout)
- if (code !== null && code !== 0) {
- fail(new Error(`TURNREWIND_GIT_FAILED: ${Buffer.concat(errors).toString('utf8').trim() || `exit ${code}`}`))
+ // Unclean exit = external kill with truncated output (see
+ // gitExitIsClean); our own timeout settled above. Leave settled=false
+ // here so fail() can reject the promise and guard the race.
+ if (!gitExitIsClean(code, signal)) {
+ fail(new Error(`TURNREWIND_GIT_FAILED: ${Buffer.concat(errors).toString('utf8').trim() || (signal ? `killed by ${signal}` : `exit ${code}`)}`))
return
}
+ settled = true
+ clearTimeout(timeout)
resolvePromise(Buffer.concat(chunks))
})
// Ignore EPIPE if git exits before consuming all input.
diff --git a/plugins/dsh-tauri-turnrewind/lib/core/git-workspace.js b/plugins/dsh-tauri-turnrewind/lib/core/git-workspace.js
index 35d846c3..7b92e161 100644
--- a/plugins/dsh-tauri-turnrewind/lib/core/git-workspace.js
+++ b/plugins/dsh-tauri-turnrewind/lib/core/git-workspace.js
@@ -5,8 +5,10 @@ import process from 'node:process'
const MAX_OUTPUT_BYTES = 1024 * 1024
// spawnSync blocks the event loop; a hard timeout keeps a wedged git from
-// freezing the host forever (same wall clock as the async spawn budget).
-const SYNC_GIT_TIMEOUT_MS = 5 * 60 * 1000
+// freezing the host forever. These are local metadata rev-parse calls only,
+// so 15s is already generous - a workload slow enough to hit it is wedged,
+// not busy (the heavy async captures keep their own 5-minute budget).
+const SYNC_GIT_TIMEOUT_MS = 15 * 1000
function runGitSync(workspaceDir, args) {
const result = spawnSync('git', ['-c', 'core.quotepath=false', ...args], {
diff --git a/plugins/dsh-tauri-turnrewind/test/git-kill.test.js b/plugins/dsh-tauri-turnrewind/test/git-kill.test.js
new file mode 100644
index 00000000..8c677b1d
--- /dev/null
+++ b/plugins/dsh-tauri-turnrewind/test/git-kill.test.js
@@ -0,0 +1,47 @@
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { mkdtemp, rm, writeFile } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { it } from 'vitest'
+import { captureSnapshot, createSnapshotStore, gitExitIsClean, runGit } from '../lib/core/git-snapshot.js'
+import { initGitWorkspace } from './git-test-utils.js'
+
+/**
+ * External kills deliver either close(null, signal) or, on Windows, a
+ * nonzero exit with no signal. Neither is a clean exit: partial stdout must
+ * never be accepted as a complete Git result.
+ */
+it('gitExitIsClean only accepts a successful unsignalled exit', () => {
+ assert.equal(gitExitIsClean(0, null), true)
+ assert.equal(gitExitIsClean(0, undefined), true)
+ assert.equal(gitExitIsClean(null, 'SIGKILL'), false)
+ assert.equal(gitExitIsClean(null, 'SIGTERM'), false)
+ assert.equal(gitExitIsClean(1, null), false)
+ assert.equal(gitExitIsClean(null, null), false)
+})
+
+it('an abruptly terminated child is classified as unclean on every platform', async () => {
+ const child = spawn(process.execPath, ['-e', 'setTimeout(() => process.kill(process.pid, "SIGKILL"), 50)'])
+ const result = await new Promise((resolvePromise) => {
+ child.on('close', (code, signal) => resolvePromise({ code, signal }))
+ })
+ assert.equal(gitExitIsClean(result.code, result.signal), false)
+})
+
+it('runGit keeps succeeding on the normal exit path after the kill guard', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-kill-test-'))
+ const workspace = join(root, 'workspace')
+ try {
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, 'a.txt'), 'a\n')
+ const store = createSnapshotStore(join(root, 'data'), workspace)
+ const snapshot = await captureSnapshot(store, 'refs/turnrewind/kill-test', 'kill test')
+ const output = await runGit(store.repoDir, store.workspaceDir, ['rev-parse', '--verify', 'refs/turnrewind/kill-test'])
+ assert.ok(output.length > 0)
+ assert.ok(snapshot.commit)
+ }
+ finally {
+ await rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 300 })
+ }
+})