Repository navigation
fix(events): stop scan and import registries growing forever - #383
Conversation
Two slow memory leaks in the events package. The scan registry kept every completed, failed and cancelled run for the life of the process, and since the admin events socket sorts the whole map on every snapshot, cost grew with total historical scans rather than active ones. Terminal runs only ever get delivered through the event published when they finish and are never read back, so drop them from the map instead of retaining them. The history import observer had the same shape. Its throttle map is keyed by run ID, which is unique per import, so finished entries sat there forever. Clear them once the run reaches a terminal status too.
📝 WalkthroughWalkthroughTerminal history import and scan runs are no longer retained in in-memory registries after terminal events. Non-terminal import runs remain throttled, and cancellation behavior is covered by a registry test. ChangesTerminal Run Cleanup
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c6830936af
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| run.Status = "cancelled" | ||
| run.CompletedAt = &completedAt | ||
| r.entries[id] = run | ||
| delete(r.entries, id) |
There was a problem hiding this comment.
Keep scan entries until the worker actually exits
When the direct in-memory scanner path is used (ScanQueue == nil), deleting the accepted/running entry here makes later worker updates disappear: markScanRunning, markScanCompleted, markScanFailed, and markScanCancelled all return immediately if ScanRegistry.Get misses. If /scan/cancel races with a scan starting/completing, or the scanner does not promptly honor the canceled context, clients get only the optimistic scan.cancelled event while the ingest can still run and finish with no correcting terminal event.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right that a late completion goes silent once the entry is gone. But the race is pre-existing: the mark* funcs already bail on a Get miss and cancel is optimistic. The entry only used to survive because terminal runs leaked forever, which is what this PR fixes. Proper fix is synchronous cancel or idempotent terminal transitions, which felt out of scope here. Happy to open a follow up issue.
Two slow memory leaks in the events package. The scan registry kept every completed, failed and cancelled run for the life of the process, and since the admin events socket sorts the whole map on every snapshot, cost grew with total historical scans rather than active ones. Terminal runs are only ever delivered through the event published when they finish and are never read back, so they're now dropped from the map instead of retained.
The history import observer's throttle map had the same shape, keyed by run ID which is unique per import, so finished entries sat there forever. They're now cleared once the run reaches a terminal status too.
Tested locally: terminal scan and import runs are dropped from the in-memory maps once they finish, while active runs still show up in the admin events view.
Built with AI assistance. I've read through the whole diff and tested it myself, and I'm happy to own it.