Skip to content

FSI: improve thread-safety #18152

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

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/Compiler/Interactive/fsi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,9 @@ type internal FsiDynamicCompiler

let multiAssemblyName = ilxMainModule.ManifestOfAssembly.Name

// The name of the assembly is "FSI-ASSEMBLY" for all submissions. This number is used for the Version
let dynamicAssemblyId = Interlocked.Increment &dynamicAssemblyId

// Adjust the assembly name of this fragment, and add InternalsVisibleTo attributes to
// allow internals access by all future assemblies with the same name (and only differing in version)
let manifest =
Expand All @@ -1811,13 +1814,10 @@ type internal FsiDynamicCompiler
{ manifest with
Name = multiAssemblyName
// Because the coreclr loader will not load a higher assembly make versions go downwards
Version = Some(parseILVersion $"0.0.0.{maxVersion - dynamicAssemblyId}")
Version = Some(parseILVersion $"0.0.0.{maxVersion - dynamicAssemblyId % maxVersion}")
CustomAttrsStored = storeILCustomAttrs (mkILCustomAttrs attrs)
}

// The name of the assembly is "FSI-ASSEMBLY" for all submissions. This number is used for the Version
dynamicAssemblyId <- (dynamicAssemblyId + 1) % maxVersion

let ilxMainModule =
{ ilxMainModule with
Manifest = Some manifest
Expand Down
18 changes: 8 additions & 10 deletions src/Compiler/Interactive/fsihelp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ open FSharp.Compiler.IO
module Parser =

open System.Xml
open System.Collections.Concurrent

type Help =
{
Expand Down Expand Up @@ -79,22 +80,19 @@ module Parser =
let s = if idx > 0 then s.Substring(0, idx) else s
s

let xmlDocCache = Dictionary<string, string>()
let xmlDocCache = ConcurrentDictionary<string, Lazy<XmlDocument>>()

let tryGetXmlDocument xmlPath =
try
match xmlDocCache.TryGetValue(xmlPath) with
| true, value ->
let xmlDocument = XmlDocument()
xmlDocument.LoadXml(value)
Some xmlDocument
| _ ->
let valueFactory xmlPath =
lazy
use stream = FileSystem.OpenFileForReadShim(xmlPath)
let rawXml = stream.ReadAllText()
let xmlDocument = XmlDocument()
xmlDocument.LoadXml(rawXml)
xmlDocCache.Add(xmlPath, rawXml)
Some xmlDocument
xmlDocument

try
Some(xmlDocCache.GetOrAdd(xmlPath, valueFactory).Value)
with _ ->
None

Expand Down
Loading