Skip to content

Read script carets without blocking the UI thread - #20523

Open
xperiandri wants to merge 10 commits into
dotnet:mainfrom
xperiandri:fix/script-caret-deadlock
Open

xperiandri wants to merge 10 commits into
dotnet:mainfrom
xperiandri:fix/script-caret-deadlock

Conversation

@xperiandri

@xperiandri xperiandri commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #20522

Visual Studio could hang for good: the project options reactor asked the UI thread for a script's caret (to know whether an #r "nuget: …" line was still being typed) while the UI thread was itself synchronously waiting on the reactor, resolving a breakpoint in the same file. The UI thread now publishes the caret itself, through the text buffer the reactor already reads; the reactor never waits on the UI thread again, and only scripts look for a caret at all.

Moving focus away from a script now submits its pending #r "nuget: …" line, where before only moving the caret to another line did — the caret is unknown once nothing has focus.

@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Change path Release notes path Description
`vsintegration/src` docs/release-notes/.VisualStudio/18.vNext.md

@github-actions github-actions Bot added the ⚠️ Affects-Design-Time Tooling check: PR touches type providers or dependency manager label Sep 11, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot added the ⚠️ Affects-Restore Tooling check: PR touches NuGet packages or feeds label Sep 11, 2026
@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from 89eaf2a to 550abf6 Compare September 11, 2026 14:44
T-Gro
T-Gro previously approved these changes Sep 11, 2026

@T-Gro T-Gro 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.

🤖 🕵️ LGTM

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Sep 11, 2026
@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label Sep 11, 2026
@T-Gro
T-Gro self-requested a review September 11, 2026 15:12

@T-Gro T-Gro 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.

🤖🕵️ Please shorten the description using this guidance. Focus on the problem and why the change is needed, in simplified technical English. Leave the implementation inventory to the Files tab and retain necessary caveats.

@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from 550abf6 to 29779b0 Compare September 17, 2026 23:34
@xperiandri
xperiandri requested a review from T-Gro September 17, 2026 23:57

@T-Gro T-Gro 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.

🤖🕵️

@xperiandri xperiandri changed the title Read a script's caret without waiting for the UI thread Read script carets without blocking the UI thread Sep 18, 2026
@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from 86f3ffa to 4c110d6 Compare September 18, 2026 19:55

@xperiandri xperiandri left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressing the inline review feedback in the updated branch.

T-Gro
T-Gro previously approved these changes Sep 21, 2026
@T-Gro
T-Gro enabled auto-merge (squash) September 21, 2026 09:43
@T-Gro T-Gro added the vsintegration-only Changes only Visual Studio integration, plus optional docs, release notes, or tests label Sep 21, 2026
@github-actions github-actions Bot added the ⚠️ Affects-Build-Infra Tooling check: PR touches build infrastructure label Sep 21, 2026
@github-actions

This comment has been minimized.

auto-merge was automatically disabled September 21, 2026 11:22

Head branch was pushed to by a user without write access

@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch 2 times, most recently from 530c52d to 75fa3cb Compare September 21, 2026 14:08

@T-Gro T-Gro 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.

🤖 🕵️ Re-reviewed all recent changes as of eeab5c6; LGTM

@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from eeab5c6 to eb5c1bd Compare October 8, 2026 01:11
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 8, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@T-Gro T-Gro 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.

🤖 🕵️ Re-reviewed all recent changes as of eb5c1bd; LGTM

@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from eb5c1bd to a423d23 Compare October 10, 2026 22:28
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 10, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@T-Gro T-Gro 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.

The diff is still way too big - apply the NoBloat instructions and CodeCompaction skill.

Especially the massive code comments would benefit not just with drastic reduction, but ideally with well named functions/arguments/bindings/types making the intention clear.

If a well named piece of code just tells what the comment says - delete the comment.

  • This diff adds 17 new code comment lines and 1666 new code comment chars.
  • Comments bypass the type system and become stale maintenance state. Keep only durable public contracts; express implementation intent through names, functions, active patterns, discriminated unions, and types.

xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
T-Gro pushed a commit that referenced this pull request Oct 20, 2026
* Note when a shared field needs option instead of voption

The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR #20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Require volatile access for a field shared between threads without a lock

An atomic-sized write prevents tearing but does not publish the value:
the field also needs [<VolatileField>] or equivalent synchronization.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from 66fc40e to d689d96 Compare October 29, 2026 22:03
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
The voption guidance said no field is ever a reason to pick option,
including ones shared between threads. That is wrong for a lock-free
field: option is always one atomic-sized reference, voption is a
struct that can span more than one word and tear on an unsynchronized
write. PR dotnet#20523 hit this for a Position field a text-view listener
writes and the project options reactor reads.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@T-Gro T-Gro 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.

🤖 🕵️ Re-reviewed all recent changes as of d689d96; 1 finding

@xperiandri

Copy link
Copy Markdown
Contributor Author

@T-Gro compacted in 87ca3bf. The diff adds 6 comment lines (553 characters) now, down from 17 (1670): the two license headers, and one line each for what the type is for, why the field is an option, why LayoutChanged is subscribed and why Closed clears the caret. The rest repeated what the names and the tests say. Two test names now carry what their comments said, and one no longer claims the opposite of what it asserts.

The diff is +168/−127 (was +179/−127). What is left is the tracker, its tests and the reactor wiring, and Extensions.fs loses 86 lines of COM plumbing.

@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from 87ca3bf to d30b3cd Compare November 5, 2026 19:26
@xperiandri
xperiandri requested a review from T-Gro November 5, 2026 19:26
xperiandri and others added 10 commits November 8, 2026 03:04
The project options reactor looked up the caret itself through
ServiceProvider.GlobalProvider, the RDT, IVsTextView and an
IVsTextViewEvents connection point, all of which need the UI thread.
When the UI thread synchronously waited on project options (breakpoint
validation when a document frame is shown), the reactor waited for the
UI thread and the UI thread for the reactor.

An IWpfTextViewCreationListener now publishes the caret of the focused
editor into the text buffer's properties, and the reactor only reads it.
Only scripts look for it: FCS uses the caret only to skip the
`#r "nuget: ..."` line being typed.

Fixes dotnet#20522

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Only the shared FocusedCaret.Position field needed to become `option`
for the atomic reference write; the reactor's own local binding was
converted along with it for no reason. Route it back through
ValueOption and land on `option` once, at the two points that need it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The caret belongs to the buffer, which outlives the view. A view closed while it held
focus left its line published, and the reactor went on skipping the `#r "nuget: …"` on
a line no editor was on.

`FocusedCaretTests` covers what the reactor reads and when it is told to read again: a
move to another line, a move along one, focus leaving, focus already gone, and a text
no view is open on.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ITextCaret.PositionChanged is raised only for explicit caret moves, not when
the caret merely follows a text edit. A line pasted or typed above the caret
therefore left the old line published, and FCS skipped the wrong `#r`. The
tracker now also republishes on a layout that brings a new snapshot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Option.map _.Line allocated two options on every caret move; the match compares the lines in place.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…aves a line

The options recomputed when the caret moves to another line were built
from the text captured when the cache entry was created. Edits made since
then had not reached them, so leaving a `#r "nuget: ..."` line could
resolve the half-typed name it held at that point - the extra package
download the caret was introduced to avoid. Read the buffer's current
text instead; it is a snapshot read and needs no UI thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The type, the field and the two subscriptions keep one line each; the
rest said what the names, the types or the tests already say. Two test
names now carry what their comments said, and one no longer claims the
opposite of what it asserts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the fix/script-caret-deadlock branch from d30b3cd to 63d7506 Compare November 8, 2026 03:14

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ Affects-Bootstrap Tooling check: PR touches compiler bootstrap chain ⚠️ Affects-Build-Infra Tooling check: PR touches build infrastructure ⚠️ Affects-Design-Time Tooling check: PR touches type providers or dependency manager ⚠️ Affects-Restore Tooling check: PR touches NuGet packages or feeds AI-reviewed PR reviewed by AI review council vsintegration-only Changes only Visual Studio integration, plus optional docs, release notes, or tests

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

VS hangs: project options reactor reads the caret through UI-thread COM while the UI thread waits for the reactor

3 participants