Repository navigation
fix: fully close MCP audit SQLite connections on shutdown - #1349
Merged
diegosouzapw merged 2 commits intoApr 16, 2026
Merged
diegosouzapw merged 2 commits into
diegosouzapw merged 2 commits into
Conversation
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an incomplete shutdown path where the MCP audit logger held a separate SQLite connection that wasn’t being closed, which could leave WAL sidecar files behind after container stop (especially in Next.js standalone chunking scenarios).
Changes:
- Add
closeAuditDb()to checkpoint/close the MCP audit SQLite handle and cache it viaglobalThisfor cross-chunk reuse. - Invoke audit DB shutdown from both the Next.js graceful shutdown cleanup and the MCP stdio server shutdown path.
- Add Vitest regression coverage for audit DB close behavior and increase Docker Compose stop grace period.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib/gracefulShutdown.ts | Adds shutdown-time import + close for MCP audit DB alongside main DB close. |
| open-sse/mcp-server/server.ts | Ensures the MCP stdio server attempts to close the audit DB on exit. |
| open-sse/mcp-server/audit.ts | Moves audit DB handle to globalThis and adds closeAuditDb() with WAL checkpoint + close. |
| open-sse/mcp-server/tests/audit.test.ts | Adds tests validating closeAuditDb() checkpoints/closes and still closes on checkpoint failure. |
| docker-compose.yml | Extends stop grace period to allow shutdown cleanup to complete in Docker. |
Comment on lines
+99
to
108
| const [{ closeAuditDb }, { closeDbInstance }] = await Promise.all([ | ||
| import("@omniroute/open-sse/mcp-server/audit.ts"), | ||
| import("@/lib/db/core"), | ||
| ]); | ||
| if (closeAuditDb()) { | ||
| console.log("[Shutdown] MCP audit database checkpointed and closed."); | ||
| } | ||
| if (closeDbInstance()) { | ||
| console.log("[Shutdown] SQLite database checkpointed and closed."); | ||
| } |
Comment on lines
+23
to
+30
| let dbFile: string; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| globalThis.__omnirouteMcpAuditDb = undefined; | ||
| dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-mcp-audit-")); | ||
| dbFile = path.join(dataDir, "storage.sqlite"); | ||
| fs.writeFileSync(dbFile, ""); |
Comment on lines
97
to
108
| async function cleanup(): Promise<void> { | ||
| try { | ||
| const { closeDbInstance } = await import("@/lib/db/core"); | ||
| const [{ closeAuditDb }, { closeDbInstance }] = await Promise.all([ | ||
| import("@omniroute/open-sse/mcp-server/audit.ts"), | ||
| import("@/lib/db/core"), | ||
| ]); | ||
| if (closeAuditDb()) { | ||
| console.log("[Shutdown] MCP audit database checkpointed and closed."); | ||
| } | ||
| if (closeDbInstance()) { | ||
| console.log("[Shutdown] SQLite database checkpointed and closed."); | ||
| } |
Owner
|
Thanks @rdself for this great contribution! 🎉 The fix has been integrated into the release/v3.6.8 branch and will be part of the next release. We appreciate your effort in closing the MCP DB shutdown gap for standalone builds! |
This was referenced Apr 17, 2026
Merged
Poid-ZA
pushed a commit
to Poid-ZA/OmniRoute
that referenced
this pull request
Aug 5, 2026
Poid-ZA
pushed a commit
to Poid-ZA/OmniRoute
that referenced
this pull request
Aug 5, 2026
…pw#1349) Integrated into release/v3.6.8
muhamadgalihsaputra
pushed a commit
to niyatna/NiyatnaRoute
that referenced
this pull request
Sep 27, 2026
muhamadgalihsaputra
pushed a commit
to niyatna/NiyatnaRoute
that referenced
this pull request
Sep 27, 2026
…pw#1349) Integrated into release/v3.6.8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
release/v3.6.7globalThisWhy this PR exists
#1348 fixed the original issue and was merged into
release/v3.6.7.While validating that fix locally in Docker, I found an additional standalone-production detail: the audit module state could be split across different Next.js chunks, so a module-local cache was not reliable enough for shutdown cleanup. This PR is the follow-up on the same release branch and includes that standalone-safe fix.
Root cause
The main SQLite singleton already checkpointed and closed on shutdown, but MCP audit used a separate
better-sqlite3connection that was not part of the shutdown chain.In standalone production builds, the audit module could also be loaded in different chunks, so a module-local cache was not reliable enough for shutdown cleanup.
Validation
./node_modules/.bin/vitest run --config vitest.mcp.config.ts open-sse/mcp-server/__tests__/audit.test.ts./node_modules/.bin/eslint open-sse/mcp-server/audit.ts open-sse/mcp-server/__tests__/audit.test.ts./node_modules/.bin/tsc --pretty false -p tsconfig.typecheck-core.jsonstorage.sqlite-wal/storage.sqlite-shmwere removed after normal stop