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

Fix data race in problem view tree #13841

Merged
merged 2 commits into from
Jun 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import { ProblemUtils } from './problem-utils';

export namespace ProblemCompositeTreeNode {

export interface Child {
node: MarkerInfoNode;
markers: Marker<Diagnostic>[];
}

export function setSeverity(parent: MarkerInfoNode, markers: Marker<Diagnostic>[]): void {
let maxSeverity: DiagnosticSeverity | undefined;
markers.forEach(marker => {
Expand All @@ -33,7 +38,7 @@ export namespace ProblemCompositeTreeNode {
parent.severity = maxSeverity;
};

export function addChildren(parent: CompositeTreeNode, insertChildren: { node: MarkerInfoNode, markers: Marker<Diagnostic>[] }[]): void {
export function addChildren(parent: CompositeTreeNode, insertChildren: ProblemCompositeTreeNode.Child[]): void {
for (const { node, markers } of insertChildren) {
ProblemCompositeTreeNode.setSeverity(node, markers);
}
Expand Down
19 changes: 14 additions & 5 deletions packages/markers/src/browser/problem/problem-tree-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import debounce = require('@theia/core/shared/lodash.debounce');

@injectable()
export class ProblemTree extends MarkerTree<Diagnostic> {
protected markers: { node: MarkerInfoNode, markers: Marker<Diagnostic>[] }[] = [];

protected queuedMarkers = new Map<string, ProblemCompositeTreeNode.Child>();

constructor(
@inject(ProblemManager) markerManager: ProblemManager,
Expand Down Expand Up @@ -79,20 +80,28 @@ export class ProblemTree extends MarkerTree<Diagnostic> {
}

protected override insertNodeWithMarkers(node: MarkerInfoNode, markers: Marker<Diagnostic>[]): void {
this.markers.push({ node, markers });
// Add the element to the queue.
// In case a diagnostics collection for the same file already exists, it will be replaced.
this.queuedMarkers.set(node.id, { node, markers });
this.doInsertNodesWithMarkers();
}

protected doInsertNodesWithMarkers = debounce(() => {
ProblemCompositeTreeNode.addChildren(this.root as MarkerRootNode, this.markers);
const root = this.root;
// Sanity check; This should always be of type `MarkerRootNode`
if (!MarkerRootNode.is(root)) {
return;
}
const queuedItems = Array.from(this.queuedMarkers.values());
ProblemCompositeTreeNode.addChildren(root, queuedItems);

for (const { node, markers } of this.markers) {
for (const { node, markers } of queuedItems) {
const children = this.getMarkerNodes(node, markers);
node.numberOfMarkers = markers.length;
this.setChildren(node, children);
}

this.markers.length = 0;
this.queuedMarkers.clear();
}, 50);
}

Expand Down
Loading