src: avoid accessing EnvInst on Env destruction#468
Conversation
WalkthroughThree environment iteration loops in N-Solid's API layer now acquire explicit ChangesEnvironment Locking Refactor
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~8 minutes 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)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
There was a problem hiding this comment.
Pull request overview
This PR hardens EnvList’s cross-thread environment iteration during shutdown by taking an EnvInst::Scope before dereferencing each EnvInst, which matches the goal in the title/descripton of avoiding use-after-destroy access to Environment state during teardown.
Changes:
- In
PromiseTracking(), switch from rawEnvInst*access to a scopedSharedEnvInstguard before readingenv()and issuing the interrupt command. - In
UpdateTracingFlags()andUpdateHasMetricsStreamHooks(), add the same scope guard and skip environments whoseEnvironmenthas already been cleared. - Keep command dispatch using the already-held
SharedEnvInst, avoiding a fresh lookup while destruction may be in progress.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@src/nsolid/nsolid_api.cc`:
- Line 1364: The loop currently dispatches commands using snapshot entry.second
which may be stale if RemoveEnv() has erased from env_map_ or env_ was nulled;
before calling RunCommand, re-check liveness by calling
EnvInst::GetInst(entry.first) and use the returned env pointer (or skip dispatch
if null) instead of the copied entry.second to avoid queuing commands to an env
in teardown; apply this pattern in the three loops that currently use
entry.second (the dispatch sites around RunCommand at the spots mentioned) so
you only call RunCommand on a live EnvInst.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9c07d711-c2dd-4cd7-a3aa-75066a2533e3
📒 Files selected for processing (1)
src/nsolid/nsolid_api.cc
I was seeing random crashes when running
test/parallel/test-cli-node-options.jsSummary by CodeRabbit