fix(nats): prevent KV scan consumer leaks - #793
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Walkthrough
ChangesNATS KV key listing
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to This change narrows KV lookups and cleans up temporary consumers to prevent scan overhead and consumer accumulation; no actionable merge-blocking risk remains beyond normal checks and review. Sequence Diagram(s)sequenceDiagram
participant kv_list_keys
participant KeyValue
participant JetStream
kv_list_keys->>KeyValue: start prefix-filtered watcher
KeyValue->>JetStream: create metadata-only consumer
JetStream-->>kv_list_keys: provide KV updates
kv_list_keys->>KeyValue: stop watcher
kv_list_keys->>JetStream: delete consumer
Suggested reviewers: Poem
🚥 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 |
|
@coderabbitai review |
✅ Action performedReview finished.
|
Summary
kv.keys()scans with prefix-scoped metadata watchersThis prevents large shared KV buckets from turning each per-node queue lookup into a full scan. It also avoids abandoned consumers accumulating after request timeouts, which otherwise creates a feedback loop of higher NATS load, sync delays, and subscription timeouts.
Type of change
Checklist
Testing
python -m uv run --frozen pytest tests/test_nats_node_memory.py tests/test_node_manager_sync.py tests/test_connect_concurrency.py tests/test_record_usages.py -q— 24 passedpython -m ruff check app/nats/kv_cas.py tests/test_nats_node_memory.py— passedScreenshots
Not applicable.
Notes for reviewers
KeyValue.Watcher.stop()only unsubscribes the client; nats-py leaves the ephemeral consumer until its inactivity threshold. The explicit consumer deletion infinallymakes cleanup immediate and is covered for both normal completion and exceptions.Summary by CodeRabbit