fix(storage): await store-file writes instead of polling (#1412) (#1413)

* fix(storage): await store-file writes instead of polling (#1412)

OAuth persistence is Zustand `persist` → file-storage adapter →
writeStoreFile, which the persist middleware invokes fire-and-forget:
saveTokens updates the in-memory store synchronously while the file
write lags. Tests verified persistence by polling the state file with a
2s/50ms `waitForStateFile()` budget, which flaked under full-suite load
(the file was written, the poll just didn't observe it in time).

Make the write completion awaitable instead:
- Track in-flight writeStoreFile() promises per resolved path in
  store-io and add flushStoreFileWrites(filePath?) to await them (also
  useful for graceful shutdown). Writes to the same path are chained so a
  flush awaits every queued write.
- Convert the OAuth E2E, node storage, and file-storage-adapter tests
  from poll loops (waitForStateFile / vi.waitFor on existsSync /
  setTimeout) to `await flushStoreFileWrites(path)` + a single read.
- Remove the now-unused waitForStateFile() test helper.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore(storage): doc load-bearing sync registration; rename stale debug env

Address @claude review on #1413:
- Document that writeStoreFile()'s synchronous pendingWrites registration is
  load-bearing for flushStoreFileWrites(), in both the storage adapter's
  setItem callsite and the pendingWrites docblock, so future refactors don't
  introduce an await before registration and silently re-flake the tests.
- Rename the stale DEBUG_WAIT_FOR_STATE_FILE test env var to DEBUG_PERSIST_PATH
  (waitForStateFile was removed in this PR).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cliff Hall 2026-06-02 11:55:24 -04:00 • committed by GitHub
commit e1c8f71924
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,7 +12,7 @@ import type {
import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as os from "node:os";
import { waitForStateFile } from "@modelcontextprotocol/inspector-test-server";
import { flushStoreFileWrites } from "@inspector/core/storage/store-io.js";
// Unique path per process so parallel test files don't share the same state file
const testStatePath = path.join(
@ -457,7 +457,7 @@ describe("OAuth Store (Zustand)", () => {
`mcp-inspector-oauth-persist-${Date.now()}-${Math.random().toString(36).slice(2)}.json`,
);
try {
if (process.env.DEBUG_WAIT_FOR_STATE_FILE === "1") {
if (process.env.DEBUG_PERSIST_PATH === "1") {
console.error("[storage-node.test] state file path:", persistTestPath);
}
const store = getOAuthStore(persistTestPath);
@ -478,14 +478,11 @@ describe("OAuth Store (Zustand)", () => {
>;
};
};
const parsed = await waitForStateFile<StateShape>(
persistTestPath,
(p) => {
const s = (p as StateShape)?.state?.servers?.[serverUrl];
return !!s?.clientInformation;
},
{ timeout: 2000, interval: 50 },
);
// Persistence is fire-and-forget; await the write rather than polling.
await flushStoreFileWrites(persistTestPath);
const parsed = JSON.parse(
await fs.readFile(persistTestPath, "utf-8"),
) as StateShape;
expect(parsed.state.servers[serverUrl]?.clientInformation).toEqual(
clientInfo,
);
@ -522,14 +519,11 @@ describe("NodeOAuthStorage with custom storagePath", () => {
servers: Record<string, { tokens?: { access_token?: string } }>;
};
};
const parsed = await waitForStateFile<StateShape>(
customPath,
(p) => {
const t = (p as StateShape)?.state?.servers?.[testServerUrl]?.tokens;
return t?.access_token === tokens.access_token;
},
{ timeout: 2000, interval: 50 },
);
// Persistence is fire-and-forget; await the write rather than polling.
await flushStoreFileWrites(customPath);
const parsed = JSON.parse(
await fs.readFile(customPath, "utf-8"),
) as StateShape;
expect(parsed.state.servers[testServerUrl]?.tokens?.access_token).toBe(
tokens.access_token,