Skip to content

Fix NullReferenceException in reportTime when compilations run concurrently - #20974

Merged
abonie merged 4 commits into
dotnet:mainfrom
jhorv:fix-race-reportTime
Nov 23, 2026
Merged

abonie merged 4 commits into
dotnet:mainfrom
jhorv:fix-race-reportTime

Conversation

@jhorv

@jhorv jhorv commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

reportTime in src/Compiler/Utilities/illib.fs keeps the current profiling activity in a process-wide mutable, tPrev. When two compilations write IL at the same time and an ActivityListener samples fsc_with_env_stats, one thread can null tPrev between another thread's null check and its Dispose, which surfaces as FS0193: internal error: Object reference not set to an instance of an object.

This takes a lock around disposing the previous activity and storing the next, so the check and the dereference can no longer interleave with another thread.

This only stops the NRE.

Revised: replaced global mutable (and the lock this PR initially added) with a locally-allocated timer object being passed down to each timed section.

There is still one process-wide slot, so concurrent compilations still mix each other's phase timings, as ReportTime in CompilerOptions.fs already does. Making the timing per compilation would be a larger change and is left out of this PR.

Fixes #20973

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Change path Release notes path Description
`src/Compiler` docs/release-notes/.FSharp.Compiler.Service/11.0.200.md

@jhorv

jhorv commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@abonie abonie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️ AI review — verify independently.

Comment thread src/Compiler/Utilities/illib.fs Resolved
@majocha

majocha commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Is it possible to get rid of the global mutable instead of locking?

@jhorv

jhorv commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Anything is possible, I just kept it intentionally point-fixing because 1) it's my first PR here 2) my F# skill is a WIP and 3) I saw the handling elsewhere (e.g. CompilerOptions.ReportTime) wasn't remarkably different (a global mutable there too).

I'll look this afternoon for a way to remove the global mutable entirely.

@jhorv

jhorv commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@majocha do you know how tied we are to these stage labels passed to reportTime? Are they considered public API? Because I'm thinking about restructuring things so they are held locally, like:

use _ = Activity.Profiling.startAndMeasureEnvironmentStats "<the stage label>"

I was going to start attempting this but then I realized - I think the current reportTime calls are mislabeling the stage they are timing! They're placed like they're saying "this stage finished, report how long it took with this stage name". Some examples from ilwrite.fs, function GenModule:

    GenTypeDefsPass1 [] cenv tdefs
    reportTime "Module Generation Pass 1"
    GenTypeDefsPass2 0 [] cenv tdefs
    reportTime "Module Generation Pass 2"

But that doesn't jive with what reportTime is actually doing: starting an Activity named for the string provided, and the elapsed time will be determined when it is disposed.

Looking back, I found this commit changed reportTime just so: before, it reported the elapsed time since it was last called. After, it's the version we have today that I put a lock in.

So I think that should be fixed, and if I adjust the calls to be scoped with a use _ I think some of them are going to end up being adjusted and possibly nested, and thus my question about whether they are considered public API or not.

@majocha

majocha commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

I was going to start attempting this but then I realized - I think the current reportTime calls are mislabeling the stage they are timing! They're placed like they're saying "this stage finished, report how long it took with this stage name". Some examples from ilwrite.fs, function GenModule:

    GenTypeDefsPass1 [] cenv tdefs
    reportTime "Module Generation Pass 1"
    GenTypeDefsPass2 0 [] cenv tdefs
    reportTime "Module Generation Pass 2"

Yeah, this does not look right. This is clearly error prone and confusing.

Instead of global mutable I'd add some wrapper type holding the current activity, create an instance at the topmost call (writeBinaryFiles ?) and pass it to inner functions during il write.

@jhorv

jhorv commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Instead of global mutable I'd add some wrapper type holding the current activity, create an instance at the topmost call (writeBinaryFiles ?) and pass it to inner functions during il write.

Okay, working on that.

@jhorv
jhorv force-pushed the fix-race-reportTime branch from 949fb9b to cd037b7 Compare November 17, 2026 01:23
@jhorv

jhorv commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Instead of global mutable I'd add some wrapper type holding the current activity, create an instance at the topmost call (writeBinaryFiles ?) and pass it to inner functions during il write.

This is now done as you described with a new type PhaseTimer. I put it under Activity.Profiling so that someday it may also be used to fix a similar Activity race in src/Compiler/Driver/CompilerOptions.fs.

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Nov 19, 2026
@abonie
abonie merged commit 13d028c into dotnet:main Nov 23, 2026
55 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in F# Compiler and Tooling Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Race in PervasiveAutoOpens.reportTime: NRE (FS0193) when two compilations write IL at once and an ActivityListener samples "fsc_with_env_stats"

3 participants