Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): bound close() when the db worker never opened

Alfredo Lopez2583974f89e1
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index c35c000dc..93b1def5a 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.582",
+ "version": "0.1.584",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.582",
+ "version": "0.1.584",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 730381a73..e971567df 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.582",
+ "version": "0.1.584",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts b/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts
index 68aa11084..fa12cb396 100644
--- a/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts
+++ b/mcp-host/src/core/conversation/persistence/__tests__/persistQueue.test.ts
@@ -713,6 +713,78 @@ describe('PersistQueue progress bound: slow vs wedged, in service and at close()
expect(failures[0].message).toMatch(/no progress for 100ms/)
})
+ it('fails close() within the bound when the worker never answers the open ping', async () => {
+ const worker = new FakeWorker()
+ const failures: Error[] = []
+ const q = new PersistQueue(worker, {
+ syncTimeoutMs: 5000,
+ asyncTimeoutMs: 5000,
+ stallTimeoutMs: 100,
+ onFailure: err => failures.push(err),
+ })
+ const open = q.awaitOpen().catch((err: unknown) => err)
+ q.enqueueAsync('s1', { kind: 'ping' })
+ await Promise.resolve()
+ // Both are posted before close(), so nothing sent after it can arm the bound.
+ expect(worker.sent).toHaveLength(2)
+ const closing = q.close()
+ let settled = false
+ const outcome = closing.then(
+ () => ({ ok: true as const }),
+ (err: Error) => ({ ok: false as const, err })
+ )
+ void outcome.then(() => {
+ settled = true
+ })
+ const started = Date.now()
+ // Bounded wait, so a hang fails the assertion instead of the test timeout.
+ await Promise.race([outcome, sleep(500)])
+ // Liveness witnesses: the open ping was posted, and close() started.
+ expect(worker.sent[0].op.kind).toBe('ping')
+ expect(() => q.enqueueAsync('s2', { kind: 'ping' })).toThrow()
+ expect(settled).toBe(true)
+ const result = await outcome
+ expect(result.ok).toBe(false)
+ expect(result.ok ? null : result.err.message).toMatch(/db worker made no progress for 100ms/)
+ expect(Date.now() - started).toBeLessThan(500)
+ expect(failures).toHaveLength(1)
+ expect(await open).toBe(failures[0])
+ })
+
+ it('lets a slow but healthy open finish during close()', async () => {
+ const worker = new FakeWorker()
+ const failures: Error[] = []
+ let terminateCalls = 0
+ worker.terminate = () => {
+ terminateCalls += 1
+ worker.emit('exit', 0)
+ }
+ const q = new PersistQueue(worker, {
+ syncTimeoutMs: 5000,
+ asyncTimeoutMs: 5000,
+ stallTimeoutMs: 300,
+ onFailure: err => failures.push(err),
+ })
+ const open = q.awaitOpen()
+ q.enqueueAsync('s1', { kind: 'ping' })
+ // The open takes longer than the bound before close() starts; the bound
+ // counts from close(), not from the ping.
+ await sleep(400)
+ expect(worker.sent).toHaveLength(2)
+ const closing = q.close()
+ await sleep(30)
+ ack(worker, worker.sent[0])
+ await open
+ ack(worker, worker.sent[1])
+ await vi.waitFor(() => expect(worker.sent.map(m => m.op.kind)).toContain('shutdown'))
+ // Liveness witness: the async write was acknowledged and shutdown posted.
+ expect(worker.sent.map(m => m.op.kind)).toEqual(['ping', 'ping', 'shutdown'])
+ ack(worker, worker.sent[2])
+ await expect(closing).resolves.toBeUndefined()
+ expect(failures).toHaveLength(0)
+ expect(terminateCalls).toBe(1)
+ })
+
it('stays failed when the stalled operation is acknowledged late', async () => {
const { worker, q, failures } = await openQueue({ stallTimeoutMs: 100 })
const stalled = q.enqueueSync({ kind: 'ping' }).catch((err: unknown) => err)
diff --git a/mcp-host/src/core/conversation/persistence/persistQueue.ts b/mcp-host/src/core/conversation/persistence/persistQueue.ts
index aabe7d6f1..1a1fb0b26 100644
--- a/mcp-host/src/core/conversation/persistence/persistQueue.ts
+++ b/mcp-host/src/core/conversation/persistence/persistQueue.ts
@@ -12,9 +12,10 @@
* with that failure and `onFailure` fires once, so the Host stops instead
* of accepting writes it can no longer persist. The same holds after an
* unexpected worker exit.
- * - Progress bound: once the database is open, while the worker has
- * operations in flight it must complete one within `stallTimeoutMs` of the
- * previous completion (or of the moment work became in flight). A slow
+ * - Progress bound: once the database is open, or once `close()` has started,
+ * while the worker has operations in flight it must complete one within
+ * `stallTimeoutMs` of the previous completion (or of the moment work became
+ * in flight, or of the start of `close()` before open). A slow
* worker keeps completing operations, however long its backlog; a wedged
* one stops, and the queue fails the same way as after a worker exit. The
* bound measures worker progress, never how long a write has waited.
@@ -48,8 +49,10 @@ export interface PersistQueueOptions {
asyncTimeoutMs: number
/**
* Longest time the worker may go without completing an operation while it
- * has operations in flight, in service and during `close()`. Not applied
- * before the `awaitOpen()` ping is answered.
+ * has operations in flight, in service and during `close()`. In service it
+ * applies only once the `awaitOpen()` ping is answered; once `close()` has
+ * started it applies even if the database never opened, counted from the
+ * start of `close()`.
*/
stallTimeoutMs: number
/** Called whenever a heartbeat reply is observed (id === HEARTBEAT_ID). */
@@ -211,8 +214,11 @@ export class PersistQueue {
* message handler only after it has opened and migrated the database, so
* this resolves once the database is open. No timeout: opening (spawn,
* migrations, recovery of a large WAL) can outlast any write budget, so
- * the progress bound applies only once this ping is answered.
- * Rejects on a worker error reply, a worker exit, or `close()`.
+ * the progress bound applies only once this ping is answered, or once
+ * `close()` has started.
+ * Rejects on a worker error reply, a worker exit, or when the worker makes
+ * no progress within `stallTimeoutMs` after `close()` started; a `close()`
+ * that finds the ping answered in time lets it resolve.
*/
awaitOpen(): Promise<void> {
try {
@@ -274,7 +280,10 @@ export class PersistQueue {
* without a `sessionKey` included), wait for the worker to ACK `shutdown`
* (it closes the database before replying), then terminate it. There is no
* total deadline: a slow worker drains its whole backlog, and only the
- * progress bound (`stallTimeoutMs`) ends the wait for a wedged one.
+ * progress bound (`stallTimeoutMs`) ends the wait for a wedged one. If the
+ * database has not opened yet, the bound starts with this call, so a worker
+ * that never answers the open ping fails within `stallTimeoutMs` while a
+ * slow but healthy open can still finish.
* Rejects with the first failure, so the caller sees that the durable
* trail is incomplete.
*/
@@ -286,6 +295,12 @@ export class PersistQueue {
private async shutdown(): Promise<void> {
this.closed = true
+ if (!this.opened) {
+ // The open ping may still be in flight; the bound starts now, not when
+ // the ping was posted, so a slow but healthy open can still finish.
+ this.lastProgressAt = performance.now()
+ this.armStallTimer()
+ }
// Admission is closed, so no chain starts after this snapshot.
await Promise.allSettled(Array.from(this.writeChain.values()))
await this.inFlightSettled()
@@ -337,7 +352,15 @@ export class PersistQueue {
private armStallTimer(): void {
// Still armed after another failure: close() waits for in-flight work.
- if (!this.opened || this.stalled || this.stallTimer || this.inFlight.size === 0) return
+ // Before open the bound applies only once close() has started.
+ if (
+ (!this.opened && !this.closed) ||
+ this.stalled ||
+ this.stallTimer ||
+ this.inFlight.size === 0
+ ) {
+ return
+ }
const remaining = this.opts.stallTimeoutMs - (performance.now() - this.lastProgressAt)
this.stallTimer = setTimeout(
() => {