Repository navigation
refactor(server): clean up Session lifetime model - #462
Conversation
…co_await Sessions stored inline in DenseMap could be invalidated by container reallocation while coroutines held references across suspension points. Store shared_ptr<Session> instead, decouple Compiler and Indexer from the sessions map via overlay callbacks, and use enable_shared_from_this to guard session lifetime in detached compile tasks.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughSession lifetime is refactored from map-value/raw-pointer storage to ChangesSession Shared Ownership and Overlay-Based Access Refactoring
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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: b557e152f0
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
- Public Compiler coroutine APIs take shared_ptr<Session> by value, owning their lifetime across co_await instead of relying on callers. - Remove enable_shared_from_this and all shared_from_this() calls. - Remove Session::closed flag; bump generation on close/reopen instead. All staleness checks now use a single generation != snapshot mechanism. - Add missing generation checks in forward_build and forward_query after co_await, fixing a bug where stale data could be sent to workers. - Simplify run_compile to take only shared_ptr<Session>, grabbing PendingCompile from session->compiling internally.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/server/compiler/compiler.cpp (1)
732-732: 💤 Low valueConsider using
params.textinstead ofsession->textforofi.content.While the generation check at line 707 ensures the session text hasn't changed if we reach this point, using
params.text(the actual content sent to the worker) would make the intent clearer and provide defense against future refactoring.Suggested change
- ofi.content = session->text; + ofi.content = params.text;🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/server/compiler/compiler.cpp` at line 732, The assignment of ofi.content is currently using session->text, but should instead use params.text which represents the actual content sent to the worker. This change makes the intent clearer and provides better protection against future refactoring. Locate the line where ofi.content is assigned to session->text and change it to use params.text instead.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/server/compiler/compiler.cpp`:
- Line 732: The assignment of ofi.content is currently using session->text, but
should instead use params.text which represents the actual content sent to the
worker. This change makes the intent clearer and provides better protection
against future refactoring. Locate the line where ofi.content is assigned to
session->text and change it to use params.text instead.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 2c4314f1-9506-4401-be1f-30f0537ea92f
📒 Files selected for processing (5)
src/server/compiler/compiler.cppsrc/server/compiler/compiler.hsrc/server/service/lsp_client.cppsrc/server/service/master_server.cppsrc/server/service/session.h
✅ Files skipped from review due to trivial changes (1)
- src/server/service/session.h
🚧 Files skipped from review as they are similar to previous changes (1)
- src/server/service/lsp_client.cpp
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f43565c1ef
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Use the snapshotted text sent to the worker rather than re-reading session->text, making the data flow explicit.
When ensure_compiled is waiting on an in-flight compile that gets aborted due to close (generation bump), don't spawn a new compile for the dead session — return false and let the caller bail out.
Summary
shared_ptr<Session>in the sessions map to prevent use-after-free when coroutines hold references acrossco_awaitwhileDenseMapreallocates or entries are erased bydidClose.shared_ptr<Session>by value in all public coroutine methods (ensure_compiled,forward_query,forward_build,forward_format,handle_completion), expressing lifetime ownership in the type signature. The sessions map reference is removed from Compiler entirely.DenseMap<uint32_t, Session>&access with two callbacks (is_open,each_overlay), exposed asfor_each_overlay()/get_overlay()helpers.generation— bump generation on bothdidChangeanddidClose, making theclosedflag unnecessary. All post-co_awaitchecks use a snapshot-and-compare pattern (session->generation != gen).ensure_compilednow checks generation after the wait loop exits, preventing it from spawning a compile for a session that was closed while waiting.forward_queryandforward_buildnow check generation after their respectiveco_awaitpoints (previouslyforward_buildonly checked session existence, missing edits;forward_queryre-looked up the map instead of checking the held session).params.textfor overlay content —run_compilestores the text snapshot taken beforeco_awaitinto theOpenFileIndex, not the potentially-mutatedsession->text.Motivation
Sessions stored inline in
DenseMapcould be invalidated by container reallocation while async coroutines heldSession&references across suspension points. Theclosedflag andfind_session()re-lookups were ad-hoc mitigations that didn't cover all code paths. Moving toshared_ptrstorage with generation-based staleness gives a single, uniform mechanism that is correct by construction.Test plan
Summary by CodeRabbit