Skip to content

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

TypeScript

fix(mcp-host): make GFS store close() wait for a stable mutation queue

Alfredo Lopezd9cdd7869a8e
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 3c22560db..3e0282f71 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.550",
+ "version": "0.1.551",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.550",
+ "version": "0.1.551",
"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 61132844a..724f7aa7b 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.550",
+ "version": "0.1.551",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/internalTools/gfsDownloadStore.ts b/mcp-host/src/internalTools/gfsDownloadStore.ts
index de910a039..3a3b942e0 100644
--- a/mcp-host/src/internalTools/gfsDownloadStore.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStore.ts
@@ -1004,8 +1004,9 @@ export class GfsDownloadStore {
}
/**
- * Stops admitting, waits for active transfers until the deadline and then
- * closes regardless. Transfers still active afterwards get download_busy
+ * Stops admitting, waits for active transfers and then for the mutation
+ * queue to be empty (including work accepted during the wait) until the
+ * deadline, and then closes regardless. Transfers still active afterwards get download_busy
* from publish/fail; their directories are removed by the next start.
*/
async close(drainTimeoutMs = 5_000): Promise<void> {
@@ -1016,16 +1017,26 @@ export class GfsDownloadStore {
}
this.closing = true
let timer: NodeJS.Timeout | undefined
+ let deadlineReached = false
const deadline = new Promise<void>(resolve => {
- timer = setTimeout(resolve, drainTimeoutMs)
+ timer = setTimeout(() => {
+ deadlineReached = true
+ resolve()
+ }, drainTimeoutMs)
})
const drained = new Promise<void>(resolve => {
if (this.active.size === 0) resolve()
else this.drainWaiters.push(resolve)
})
- // Active transfers first, then whatever mutation is still in flight; both
- // bounded by the same deadline.
- await Promise.race([drained.then(() => this.mutationTail), deadline])
+ // Active transfers first, then the mutation queue until it is stable: a
+ // mutation accepted while close() waits replaces the tail, so wait again.
+ // Both phases are bounded by the same deadline.
+ await Promise.race([drained, deadline])
+ while (!deadlineReached) {
+ const tail = this.mutationTail
+ await Promise.race([tail, deadline])
+ if (tail === this.mutationTail) break
+ }
clearTimeout(timer)
if (this.active.size > 0)
logger.warn(
diff --git a/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts b/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts
index 5dec6e8a2..86bf26ee8 100644
--- a/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts
+++ b/mcp-host/src/internalTools/gfsDownloadStoreBoundaries.test.ts
@@ -643,6 +643,69 @@ describe('GFS download store boundaries: in-process concurrency', () => {
expect(syncFs.readFileSync(path.join(rootA, receipt.path))).toEqual(bytes)
})
+ // R3-M1: work accepted while close() waits replaces the mutation tail; close
+ // must wait for the new tail too, and still return at its deadline.
+ function blockSweeps(target: GfsDownloadStore) {
+ const internals = target as unknown as { sweep: (now: number) => Promise<unknown> }
+ const realSweep = internals.sweep.bind(target)
+ const started: number[] = []
+ const releases: Array<() => void> = []
+ const finished: number[] = []
+ vi.spyOn(internals, 'sweep').mockImplementation(async (now: number) => {
+ const index = started.length
+ started.push(index)
+ await new Promise<void>(resolve => releases.push(resolve))
+ const result = await realSweep(now)
+ finished.push(index)
+ return result
+ })
+ return { started, releases, finished }
+ }
+
+ it('C5b: close waits for a sweep accepted while it was already waiting', async () => {
+ const sweeps = blockSweeps(store)
+ const first = store.cleanupExpired()
+ await vi.waitFor(() => expect(sweeps.started).toEqual([0]))
+ let closed = false
+ const closing = store.close(5_000).then(() => {
+ closed = true
+ })
+ // close() is already waiting on the first sweep's tail when this arrives.
+ await new Promise(resolve => setTimeout(resolve, 10))
+ const second = store.cleanupExpired()
+
+ sweeps.releases[0]!()
+ await first
+ // Witness: the second sweep started, so the first tail had resolved.
+ await vi.waitFor(() => expect(sweeps.started).toEqual([0, 1]))
+ await new Promise(resolve => setTimeout(resolve, 20))
+ expect(closed).toBe(false)
+
+ sweeps.releases[1]!()
+ await second
+ await closing
+ expect(sweeps.finished).toEqual([0, 1])
+ expect(store.isAvailable()).toBe(false)
+ })
+
+ it('C5c: close still returns at its deadline when an accepted sweep never finishes', async () => {
+ const sweeps = blockSweeps(store)
+ const stuck = store.cleanupExpired()
+ await vi.waitFor(() => expect(sweeps.started).toEqual([0]))
+ const startedAt = performance.now()
+
+ await store.close(100)
+
+ const elapsed = performance.now() - startedAt
+ expect(elapsed).toBeGreaterThanOrEqual(90)
+ expect(elapsed).toBeLessThan(2_000)
+ // Witness: the sweep was still blocked when close returned.
+ expect(sweeps.finished).toEqual([])
+ expect(store.isAvailable()).toBe(false)
+ sweeps.releases[0]!()
+ await expect(stuck).resolves.toMatchObject({ removedExpired: 0 })
+ })
+
it('C7: fail() after a partial write removes the directory and releases the reservation', async () => {
const { transfer } = await startTransfer(store, rootA, A, 355, 12)
const directory = downloadDirectory(rootA, transfer.id)
diff --git a/scripts/ci/pinned-vitest-cases/mcp-host.json b/scripts/ci/pinned-vitest-cases/mcp-host.json
index ae90ba0b1..1574f67e3 100644
--- a/scripts/ci/pinned-vitest-cases/mcp-host.json
+++ b/scripts/ci/pinned-vitest-cases/mcp-host.json
@@ -331,6 +331,14 @@
"suite": "gfsDownloadStoreBoundaries.test.ts",
"fullName": "GFS download store boundaries: in-process concurrency C5: close requested during publication waits for it"
},
+ {
+ "suite": "gfsDownloadStoreBoundaries.test.ts",
+ "fullName": "GFS download store boundaries: in-process concurrency C5b: close waits for a sweep accepted while it was already waiting"
+ },
+ {
+ "suite": "gfsDownloadStoreBoundaries.test.ts",
+ "fullName": "GFS download store boundaries: in-process concurrency C5c: close still returns at its deadline when an accepted sweep never finishes"
+ },
{
"suite": "gfsDownloadStoreBoundaries.test.ts",
"fullName": "GFS download store boundaries: in-process concurrency C7: fail() after a partial write removes the directory and releases the reservation"