Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ private sealed class DocumentActiveContextChangedEventSource(ITextBuffer subject
{
private WorkspaceEventRegistration? _documentActiveContextChangedDisposer;

// Require main thread on the callback as RaiseChanged implementors may have main thread dependencies.
protected override void ConnectToWorkspace(Workspace workspace)
=> _documentActiveContextChangedDisposer = workspace.RegisterDocumentActiveContextChangedHandler(OnDocumentActiveContextChanged);
=> _documentActiveContextChangedDisposer = workspace.RegisterDocumentActiveContextChangedHandler(OnDocumentActiveContextChanged, WorkspaceEventOptions.RequiresMainThreadOptions);

protected override void DisconnectFromWorkspace(Workspace workspace)
=> _documentActiveContextChangedDisposer?.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core/Tagging/ITaggerEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal interface ITaggerEventSource

/// <summary>
/// An event has happened on the thing the tagger is attached to. The tagger should
/// recompute tags. May be raised on any thread.
/// recompute tags.
/// </summary>
event EventHandler<TaggerEventArgs> Changed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public CodeAnalysisDiagnosticAnalyzerService(
_workspace = workspace;
_diagnosticAnalyzerService = _workspace.Services.GetRequiredService<IDiagnosticAnalyzerService>();

_ = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged);
// Main thread as OnWorkspaceChanged's call to IDiagnosticAnalyzerService.RequestDiagnosticRefresh isn't clear on
// threading requirements
_ = workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChanged, WorkspaceEventOptions.RequiresMainThreadOptions);
}

private void OnWorkspaceChanged(WorkspaceChangeEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ internal interface IDiagnosticAnalyzerService : IWorkspaceService
/// <summary>
/// Re-analyze all projects and documents. This will cause an LSP diagnostic refresh request to be sent.
/// </summary>
/// <remarks>
/// This implementation must be safe to call on any thread.
/// </remarks>
void RequestDiagnosticRefresh();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public virtual void Register(Workspace? workspace)
m["WorkspacePartialSemanticsEnabled"] = workspace.PartialSemanticsEnabled;
}, workspace));

var workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnLspWorkspaceChanged);
// Forward workspace change events for all registered LSP workspaces. Requires main thread as it
// fires LspSolutionChanged which hasn't been guaranteed to be thread safe.
var workspaceChangedDisposer = workspace.RegisterWorkspaceChangedHandler(OnLspWorkspaceChanged, WorkspaceEventOptions.RequiresMainThreadOptions);

lock (_gate)
{
Expand Down Expand Up @@ -92,7 +94,9 @@ public void Dispose()
}

/// <summary>
/// Indicates whether the LSP solution has changed in a non-tracked document context. May be raised on any thread.
/// Indicates whether the LSP solution has changed in a non-tracked document context.
///
/// <b>IMPORTANT:</b> Implementations of this event handler should do as little synchronous work as possible since this will block.
/// </summary>
public EventHandler<WorkspaceChangeEventArgs>? LspSolutionChanged;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public StackTraceExplorerViewModel(IThreadingContext threadingContext, Workspace
_threadingContext = threadingContext;
_workspace = workspace;

// Main thread dependency as Workspace_WorkspaceChanged modifies an ObservableCollection
_ = workspace.RegisterWorkspaceChangedHandler(Workspace_WorkspaceChanged, WorkspaceEventOptions.RequiresMainThreadOptions);

_classificationTypeMap = classificationTypeMap;
_formatMap = formatMap;

Expand Down Expand Up @@ -100,6 +103,15 @@ private void CallstackLines_CollectionChanged(object sender, System.Collections.
NotifyPropertyChanged(nameof(IsInstructionTextVisible));
}

private void Workspace_WorkspaceChanged(WorkspaceChangeEventArgs e)
{
if (e.Kind == WorkspaceChangeKind.SolutionChanged)
{
Selection = null;
Frames.Clear();
}
}

private FrameViewModel GetViewModel(ParsedFrame frame)
=> frame switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal sealed partial class CpsDiagnosticItemSource : BaseDiagnosticAndGenerat
{
private readonly IVsHierarchyItem _item;
private readonly string _projectDirectoryPath;
private readonly string? _analyzerFilePath;

private WorkspaceEventRegistration? _workspaceChangedDisposer;

Expand All @@ -38,8 +37,6 @@ public CpsDiagnosticItemSource(
_item = item;
_projectDirectoryPath = Path.GetDirectoryName(projectPath);

_analyzerFilePath = CpsUtilities.ExtractAnalyzerFilePath(_projectDirectoryPath, _item.CanonicalName);

this.AnalyzerReference = TryGetAnalyzerReference(Workspace.CurrentSolution);
if (this.AnalyzerReference == null)
{
Expand All @@ -50,7 +47,9 @@ public CpsDiagnosticItemSource(
// then connect to it.
if (workspace.CurrentSolution.ContainsProject(projectId))
{
_workspaceChangedDisposer = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChangedLookForAnalyzer);
// Main thread dependency as OnWorkspaceChangedLookForAnalyzer accesses the IVsHierarchy
// and fires the PropertyChanged event
_workspaceChangedDisposer = Workspace.RegisterWorkspaceChangedHandler(OnWorkspaceChangedLookForAnalyzer, WorkspaceEventOptions.RequiresMainThreadOptions);
item.PropertyChanged += IVsHierarchyItem_PropertyChanged;

// Now that we've subscribed, check once more in case we missed the event
Expand Down Expand Up @@ -119,11 +118,14 @@ private void OnWorkspaceChangedLookForAnalyzer(WorkspaceChangeEventArgs e)
return null;
}

if (string.IsNullOrEmpty(_analyzerFilePath))
var canonicalName = _item.CanonicalName;
var analyzerFilePath = CpsUtilities.ExtractAnalyzerFilePath(_projectDirectoryPath, canonicalName);

if (string.IsNullOrEmpty(analyzerFilePath))
{
return null;
}

return project.AnalyzerReferences.FirstOrDefault(r => string.Equals(r.FullPath, _analyzerFilePath, StringComparison.OrdinalIgnoreCase));
return project.AnalyzerReferences.FirstOrDefault(r => string.Equals(r.FullPath, analyzerFilePath, StringComparison.OrdinalIgnoreCase));
}
}
Loading