-
Notifications
You must be signed in to change notification settings - Fork 41
Add resource limits and cleanup to bridge and extension #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -502,10 +502,23 @@ async function createBridgeServer(options) { | |
| } | ||
|
|
||
| if (forceRecreate) { | ||
| try { | ||
| debugLog("[ctxce] Reinitializing remote MCP clients after session error."); | ||
| } catch { | ||
| // ignore logging failures | ||
| debugLog("[ctxce] Reinitializing remote MCP clients after session error."); | ||
|
|
||
| if (indexerClient) { | ||
| try { | ||
| await indexerClient.close(); | ||
| } catch { | ||
| // ignore close errors | ||
| } | ||
| indexerClient = null; | ||
| } | ||
| if (memoryClient) { | ||
| try { | ||
| await memoryClient.close(); | ||
| } catch { | ||
| // ignore close errors | ||
| } | ||
| memoryClient = null; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -830,11 +843,28 @@ export async function runHttpMcpServer(options) { | |
| return; | ||
| } | ||
|
|
||
| const MAX_BODY_SIZE = 10 * 1024 * 1024; // 10MB | ||
| let body = ""; | ||
| let bodyLimitExceeded = false; | ||
| req.on("data", (chunk) => { | ||
| if (bodyLimitExceeded) return; | ||
| body += chunk; | ||
| if (body.length > MAX_BODY_SIZE) { | ||
| bodyLimitExceeded = true; | ||
| req.destroy(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| res.statusCode = 413; | ||
| res.setHeader("Content-Type", "application/json"); | ||
| res.end( | ||
| JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| error: { code: -32000, message: "Request body too large" }, | ||
| id: null, | ||
| }), | ||
| ); | ||
| } | ||
| }); | ||
| req.on("end", async () => { | ||
| if (bodyLimitExceeded) return; | ||
| let parsed; | ||
| try { | ||
| parsed = body ? JSON.parse(body) : {}; | ||
|
|
@@ -902,6 +932,25 @@ export async function runHttpMcpServer(options) { | |
| httpServer.listen(port, '127.0.0.1', () => { | ||
| debugLog(`[ctxce] HTTP MCP bridge listening on 127.0.0.1:${port}`); | ||
| }); | ||
|
|
||
| let shuttingDown = false; | ||
| const shutdown = (signal) => { | ||
| if (shuttingDown) return; | ||
| shuttingDown = true; | ||
| debugLog(`[ctxce] Received ${signal}; closing HTTP server (waiting for in-flight requests).`); | ||
| httpServer.close(() => { | ||
| debugLog("[ctxce] HTTP server closed."); | ||
| process.exit(0); | ||
| }); | ||
| const SHUTDOWN_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes for long MCP calls | ||
| setTimeout(() => { | ||
| debugLog("[ctxce] Forcing exit after shutdown timeout."); | ||
| process.exit(1); | ||
| }, SHUTDOWN_TIMEOUT_MS).unref(); | ||
| }; | ||
|
|
||
| process.on("SIGTERM", () => shutdown("SIGTERM")); | ||
| process.on("SIGINT", () => shutdown("SIGINT")); | ||
| } | ||
|
|
||
| function loadConfig(startDir) { | ||
|
|
||
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
body.lengthis a character count after decoding chunks to a JS string, not the raw byte size on the wire; a request can exceed 10MB in bytes while staying under this limit. If the intent is a hard 10MB cap, tracking the incoming byte count is more reliable.🤖 Was this useful? React with 👍 or 👎