Skip to content

fix(events): stop scan and import registries growing forever - #383

Merged
Quick104 merged 2 commits into
Silo-Server:mainfrom
JeremySNR:fix/events-terminal-run-leaks
Aug 20, 2026
Merged

Quick104 merged 2 commits into
Silo-Server:mainfrom
JeremySNR:fix/events-terminal-run-leaks

Conversation

@JeremySNR

@JeremySNR JeremySNR commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.
@coderabbitai

coderabbitai Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Terminal 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.

Changes

Terminal Run Cleanup

Layer / File(s) Summary
History import terminal-state handling
internal/events/publishers.go
Terminal import statuses remove their state entries, while non-terminal updates continue recording timestamps through historyImportTerminal.
Scan registry terminal cleanup
internal/events/scan_registry.go, internal/events/scan_registry_test.go
MarkTerminal deletes terminal scans, and cancellation tests verify the run remains retrievable until terminal marking before removal.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: cleaning up scan and import registry entries to prevent unbounded growth.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread internal/events/scan_registry.go Outdated
run.Status = "cancelled"
run.CompletedAt = &completedAt
r.entries[id] = run
delete(r.entries, id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Quick104
Quick104 merged commit a0538d3 into Silo-Server:main Aug 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants