Fix nil dereference crash in loadModuleFromSpecificNodeModulesDirectory#3941
Draft
Copilot wants to merge 3 commits into
Draft
Fix nil dereference crash in loadModuleFromSpecificNodeModulesDirectory#3941Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Add a `packageInfo.Exists()` guard before accessing `packageInfo.Contents` at line 1132 in `loadModuleFromSpecificNodeModulesDirectory`. Without this check, a nil `Contents` field (possible under concurrent resolution races where `LoadOrStore` returns a stale cache entry) causes a fatal nil-pointer dereference. This matches the defensive pattern already used at other `Contents` access sites in the same function (lines 1078, 1101, 1124). Fixes #1290 Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix fatal server crash at loadModuleFromSpecificNodeModulesDirectory
Fix nil dereference crash in loadModuleFromSpecificNodeModulesDirectory
May 18, 2026
Member
|
The test seems insufficient if this is about race conditions. @copilot is there a better test here? Possibly a server test or a programmatic test of our APIs? |
Remove the compiler test (nodeModulesSubpathResolution.ts) which didn't exercise the actual race condition. Add a Go unit test (TestResolveSubpathNilContentsRace) that deterministically reproduces the nil-Contents cache race using a flipFileExistsFS wrapper: 1. Two goroutines resolve `pkg/sub` concurrently, both miss the cache 2. First caller's FileExists returns false → stores nil-Contents entry 3. Second caller's FileExists returns true → ReadFile is gated until the first's Set completes → its Set (LoadOrStore) returns the stale nil-Contents entry 4. Without the Exists() guard: nil dereference panic 5. With the guard: gracefully skipped The test deterministically fails when the fix is reverted. Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Contributor
Author
Replaced the compiler test with a programmatic race condition test in
I verified it deterministically fails when the fix is reverted. See commit 3dc7a86. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fatal crash when
packageInfo.Contentsis nil at thetypesVersionslookup inloadModuleFromSpecificNodeModulesDirectory. This can occur under concurrent resolution whenpackageJsonInfoCache.Set()(backed bysync.Map.LoadOrStore) returns a stale entry with nilContentsto a thread that parsed a validpackage.json.packageInfo.Exists()guard before accessingContents.GetVersionPaths(), matching the defensive pattern at every otherContentsaccess site in the same functionpkg/subwith nestedpackage.json)