fix(dev): use globalThis for singleton state to prevent HMR memory leaks#4869
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryLow Risk Overview Redis ( Pub/sub & long-lived services: copilot tool confirmation, task status, execution cancellation, MCP tools/workflow channels, and McpConnectionManager (non-test) are lazily created once per process on Rate limiting: cached storage adapter and the Runtime behavior in production is unchanged; the intent is one instance per Node process, including across HMR reloads in development. Reviewed by Cursor Bugbot for commit 138f146. Configure here. |
Greptile SummaryThis PR addresses an unbounded memory growth bug in development (observed at 68GB) caused by Next.js HMR re-evaluating module code on every file save and creating new singleton instances while old ones were held alive by
Confidence Score: 5/5Safe to merge — the change is purely a re-homing of singleton state from module scope to globalThis, with no behavioral difference in production. All seven files apply the globalThis singleton pattern correctly and consistently. No logic changes were made to the actual Redis/pub-sub behavior, and the reconnect-listener accumulation bug from the previous review thread is confirmed resolved. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Next.js HMR — module re-evaluated] --> B{Key already on globalThis?}
B -- No: first process start --> C[Create singleton]
C --> D[Store on globalThis._key]
D --> E[Assign to module-scoped const]
B -- Yes: subsequent HMR reload --> F[Skip creation]
F --> G[Read existing value from globalThis._key]
G --> E
E --> H[Export / use singleton]
Reviews (3): Last reviewed commit: "fix(types): resolve McpConnectionManager..." | Re-trigger Greptile |
…revent listener accumulation
|
@greptile |
|
@cursor review |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 54d4446. Configure here.
|
@greptile |
|
@cursor review |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 138f146. Configure here.
Summary
setInterval/setTimeoutclosures — causing unbounded memory growth in dev (observed: 68GB)globalThisso it survives HMR re-evaluations and is only created once per processType of Change
Testing
Tested manually
Checklist