Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved threadsafety for FileIndexTable.FileToIndex #18137

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/Compiler/Utilities/range.fs
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,29 @@ type FileIndexTable() =
match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->
// If a write operation can happen, we have to lock the whole read-check-write to avoid race conditions
lock fileToIndexTable
<| fun () ->
match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->

// Try again looking for a normalized entry.
let normalizedFilePath =
if normalize then
FileSystem.NormalizePathShim filePath
else
filePath
// Try again looking for a normalized entry.
let normalizedFilePath =
if normalize then
FileSystem.NormalizePathShim filePath
else
filePath

match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx ->
// Record the non-normalized entry if necessary
if filePath <> normalizedFilePath then
fileToIndexTable[filePath] <- idx
match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx ->
// Record the non-normalized entry if necessary
if filePath <> normalizedFilePath then
fileToIndexTable[filePath] <- idx

// Return the index
idx
// Return the index
idx

| _ ->
lock indexToFileTable (fun () ->
// See if it was added on another thread
match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx -> idx
| _ ->
// Get the new index
// Okay it's really not there
let idx = indexToFileTable.Count

// Record the normalized entry
Expand All @@ -232,7 +230,7 @@ type FileIndexTable() =
fileToIndexTable[filePath] <- idx

// Return the index
idx
idx)

member t.IndexToFile n =
if n < 0 then
Expand Down
Loading