Skip to content

Commit

Permalink
Fix data race in problem view tree (#13841)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Jun 25, 2024
1 parent b16f49b commit 377de26
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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

0 comments on commit 377de26

Please sign in to comment.