From e1e27de2db54f915fa0305a29e03fd8b28cabdcd Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 18 Sep 2026 15:23:22 -0700 Subject: [PATCH] fix: stop a Full Disk Access blocked Warp read from freezing serve, and harden the Grok Bot quota reader (#1487) The Warp Full Disk Access probe ran synchronously on the serve request thread, so a denial froze the whole serve for about six seconds every sixty seconds; the recurring re-probe now returns the last known verdict instantly and refreshes off the request path. A probe child that cannot start on a Group Container path now fails closed, skipping Warp with the grant-access notice, instead of opening the file and re-wedging. The desktop Grok Bot quota reader opened Cursor's database with no busy timeout, so a Cursor write-lock could hang it; it now uses the same busy timeout the CLI uses and waits out the lock. The FIFO-based preflight test is skipped on Windows, where named pipes are not available. --- app/electron/quota/grokbot.ts | 7 +++ src/sqlite.ts | 68 +++++++++++++++++++++++++++--- tests/sqlite-tcc-preflight.test.ts | 35 ++++++++++++--- 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/app/electron/quota/grokbot.ts b/app/electron/quota/grokbot.ts index c656e491..b647285c 100644 --- a/app/electron/quota/grokbot.ts +++ b/app/electron/quota/grokbot.ts @@ -61,6 +61,13 @@ export async function cursorAccessTokenFromDatabase(databasePath: string): Promi if (!existsSync(databasePath)) return null const { DatabaseSync } = await import('node:sqlite') const db = new DatabaseSync(databasePath, { readOnly: true }) + try { + // Cursor is usually running and holding a write lock; without this a read + // gets SQLITE_BUSY at once. Wait it out the same 1s the CLI opener does. + db.exec?.('PRAGMA busy_timeout = 1000') + } catch { + // Best effort. Some Node sqlite builds may not expose exec on DatabaseSync. + } try { const rows = db.prepare('SELECT value FROM ItemTable WHERE key = ? LIMIT 1').all(ACCESS_TOKEN_KEY) const value = rows[0]?.['value'] diff --git a/src/sqlite.ts b/src/sqlite.ts index dbc0603d..b568581e 100644 --- a/src/sqlite.ts +++ b/src/sqlite.ts @@ -1,5 +1,5 @@ import { createRequire } from 'node:module' -import { spawnSync } from 'node:child_process' +import { spawn, spawnSync } from 'node:child_process' import { copyFileSync, existsSync, mkdirSync, readdirSync, renameSync, statSync, unlinkSync, utimesSync } from 'node:fs' import { createHash, randomBytes } from 'node:crypto' import { homedir } from 'node:os' @@ -422,20 +422,79 @@ const TCC_PROBE_TIMEOUT_MS = 3000 const TCC_PROBE_CACHE_MS = 60_000 const tccProbeCache = new Map() +const tccProbeRefreshing = new Set() let warnedTccBlocked = false export function isBlockedDatabaseError(err: unknown): boolean { return typeof err === 'object' && err !== null && 'codeburnBlocked' in err } +/// Turn one probe outcome into a verdict. A timeout kill (SIGKILL) is a real +/// hang: never open the file wherever it lives. A spawn that never ran (the +/// child could not start, e.g. execPath missing) taught us nothing about the +/// file, so fail CLOSED for Group Container paths - opening one with no consent +/// wedges the whole process - and open elsewhere, where a spawn failure is not +/// the file's fault. Otherwise 0 is readable, 2 is missing (let the real open +/// report that), any other exit means do not touch it. +function classifyProbe( + result: { signal: NodeJS.Signals | null; error?: unknown; status: number | null }, + isGroupContainer: boolean, +): boolean { + if (result.signal === 'SIGKILL') return true + if (result.error) return isGroupContainer + return result.status !== 0 && result.status !== 2 +} + +/// Re-probe a stale entry off the request path. spawnSync would freeze the +/// single-threaded serve queue for up to the timeout on every stale read; the +/// async spawn refreshes the cache without blocking the caller, which keeps +/// serving on the last known verdict until it lands. +function refreshProbeAsync(key: string, path: string, script: string, execPath: string, isGroupContainer: boolean): void { + if (tccProbeRefreshing.has(key)) return + tccProbeRefreshing.add(key) + const settle = (result: { signal: NodeJS.Signals | null; error?: unknown; status: number | null }): void => { + tccProbeRefreshing.delete(key) + tccProbeCache.set(key, { blocked: classifyProbe(result, isGroupContainer), at: Date.now() }) + } + let child: ReturnType + try { + child = spawn(execPath, ['-e', script, path], { + stdio: 'ignore', + // process.execPath is Electron's binary in the desktop app; without this + // it would launch a second app window instead of running the script. + env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }, + }) + } catch (error) { + settle({ signal: null, error, status: null }) + return + } + const timer = setTimeout(() => { + child.kill('SIGKILL') + settle({ signal: 'SIGKILL', status: null }) + }, TCC_PROBE_TIMEOUT_MS) + timer.unref?.() + child.once('error', (error) => { clearTimeout(timer); settle({ signal: null, error, status: null }) }) + child.once('exit', (status, signal) => { clearTimeout(timer); settle({ signal, status }) }) +} + /// `script` and `execPath` are only ever overridden by tests, which cannot make /// a real open() hang on demand nor break the spawn itself. export function probeDatabaseBlocked(path: string, script = TCC_PROBE, execPath = process.execPath): boolean { const now = Date.now() const key = `${script}\u0000${path}` + const isGroupContainer = path.startsWith(GROUP_CONTAINERS_PREFIX) const cached = tccProbeCache.get(key) - if (cached && now - cached.at < TCC_PROBE_CACHE_MS) return cached.blocked + if (cached) { + // Stale: refresh in the background so granting FDA later recovers, but never + // block this (possibly serve) request on spawnSync; answer with the last + // known verdict until the async re-probe lands. + if (now - cached.at >= TCC_PROBE_CACHE_MS) refreshProbeAsync(key, path, script, execPath, isGroupContainer) + return cached.blocked + } + // Cold: no cached verdict yet. A one-shot CLI run has no later request to wait + // for an async probe, so answer authoritatively now with a single spawnSync. + // serve warms this cache off the request path when it arms its root watchers. const probe = spawnSync(execPath, ['-e', script, path], { timeout: TCC_PROBE_TIMEOUT_MS, killSignal: 'SIGKILL', @@ -444,10 +503,7 @@ export function probeDatabaseBlocked(path: string, script = TCC_PROBE, execPath // would launch a second app window instead of running the script. env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }, }) - // 0 readable, 2 missing (let the real open report that); the timeout kill and - // any other exit mean we must not touch this file. A probe we could not run - // at all says nothing about the file, so it must not block it. - const blocked = probe.signal === 'SIGKILL' || (!probe.error && probe.status !== 0 && probe.status !== 2) + const blocked = classifyProbe(probe, isGroupContainer) tccProbeCache.set(key, { blocked, at: now }) return blocked } diff --git a/tests/sqlite-tcc-preflight.test.ts b/tests/sqlite-tcc-preflight.test.ts index 7dc68f27..48ed0f0b 100644 --- a/tests/sqlite-tcc-preflight.test.ts +++ b/tests/sqlite-tcc-preflight.test.ts @@ -1,3 +1,4 @@ +import { execFileSync } from 'node:child_process' import { mkdtempSync, writeFileSync } from 'node:fs' import { homedir, tmpdir } from 'node:os' import { join } from 'node:path' @@ -11,23 +12,43 @@ import { assertDatabaseReadable, isBlockedDatabaseError, probeDatabaseBlocked } const SLEEPS_FOREVER = 'setTimeout(() => {}, 1e9)' const groupContainerPath = (name: string) => join(homedir(), 'Library', 'Group Containers', '2BBY89MBSN.dev.warp.test', name) +const normalDir = () => mkdtempSync(join(tmpdir(), 'codeburn-tcc-')) describe('Group Container preflight', () => { - it('gives up on a child that never exits', () => { + // A readable file exits 0 whatever directory it lives in, so its verdict does + // not depend on the Group Container prefix; the real protected dir cannot be + // written to in a test, so a normal readable file stands in for both. + it('passes a readable file', () => { + const file = join(normalDir(), 'warp.sqlite') + writeFileSync(file, 'x') + expect(probeDatabaseBlocked(file)).toBe(false) + }) + + it('blocks a hang on a Group Container path', () => { const started = Date.now() expect(probeDatabaseBlocked(groupContainerPath('hang.sqlite'), SLEEPS_FOREVER)).toBe(true) expect(Date.now() - started).toBeLessThan(10_000) }) - it('passes a readable file', () => { - const file = join(mkdtempSync(join(tmpdir(), 'codeburn-tcc-')), 'warp.sqlite') - writeFileSync(file, 'x') - expect(probeDatabaseBlocked(file)).toBe(false) + // mkfifo has no Windows equivalent, so this hang can only be reproduced on POSIX. + it.skipIf(process.platform === 'win32')('blocks a hanging open (FIFO) on a normal path', () => { + const fifo = join(normalDir(), 'warp.fifo') + execFileSync('mkfifo', [fifo]) + const started = Date.now() + // The default probe opens argv[1] for read; a FIFO with no writer blocks it + // exactly like a TCC-wedged open, and the timeout kill classifies as blocked. + expect(probeDatabaseBlocked(fifo)).toBe(true) + expect(Date.now() - started).toBeLessThan(10_000) }) - it('does not blame the file when the probe itself cannot run', () => { + it('fails closed on a Group Container path the probe could not run for', () => { const missingNode = join(tmpdir(), 'codeburn-no-such-node') - expect(probeDatabaseBlocked(groupContainerPath('spawn-fail.sqlite'), SLEEPS_FOREVER, missingNode)).toBe(false) + expect(probeDatabaseBlocked(groupContainerPath('spawn-fail.sqlite'), SLEEPS_FOREVER, missingNode)).toBe(true) + }) + + it('does not blame a normal-path file when the probe itself cannot run', () => { + const missingNode = join(tmpdir(), 'codeburn-no-such-node') + expect(probeDatabaseBlocked(join(normalDir(), 'spawn-fail.sqlite'), SLEEPS_FOREVER, missingNode)).toBe(false) }) it('warns once per run and leaves the error marked as blocked', () => {