Skip to content

Commit f134a20

Browse files
committed
Make sure that a long-running computation of node children doesn't overwrite newer results.
1 parent 07b9a46 commit f134a20

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

visualvm/heapviewer/src/org/graalvm/visualvm/heapviewer/model/HeapViewerNode.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ protected void resetChildren() {
108108
}
109109

110110
public void forgetChildren(NodesCache cache) {
111+
synchronized (this) {
112+
if (currentWorker != null) {
113+
currentWorker.interrupt();
114+
currentWorker = null;
115+
}
116+
}
117+
111118
if (children != null && children.length > 0) {
112119
for (HeapViewerNode node : children) {
113120
node.forgetChildren(cache);
@@ -142,6 +149,8 @@ private HeapViewerNode[] resolveChildren() {
142149
}
143150

144151

152+
private Thread currentWorker;
153+
145154
protected HeapViewerNode[] computeChildren(final RootNode root) {
146155
// if (this == root) {
147156
// System.err.println(">>> COMPUTING CHILDREN OF ROOT in " + Thread.currentThread());
@@ -151,11 +160,32 @@ protected HeapViewerNode[] computeChildren(final RootNode root) {
151160

152161
SwingWorker<HeapViewerNode[], HeapViewerNode[]> worker = new SwingWorker<HeapViewerNode[], HeapViewerNode[]>() {
153162
protected HeapViewerNode[] doInBackground() throws Exception {
154-
return lazilyComputeChildren(root.getContext().getFragment().getHeap(), root.getViewID(), root.getViewFilter(), root.getDataTypes(), root.getSortOrders(), progress);
163+
synchronized (HeapViewerNode.this) {
164+
if (currentWorker != null) {
165+
currentWorker.interrupt();
166+
// System.err.println(">>> Cancelling children of " + currentWorker);
167+
}
168+
currentWorker = Thread.currentThread();
169+
// System.err.println(">>> Computing children in " + Thread.currentThread() + "...");
170+
}
171+
172+
HeapViewerNode[] ret = lazilyComputeChildren(root.getContext().getFragment().getHeap(), root.getViewID(), root.getViewFilter(), root.getDataTypes(), root.getSortOrders(), progress);
173+
174+
synchronized (HeapViewerNode.this) {
175+
if (currentWorker == Thread.currentThread()) {
176+
currentWorker = null;
177+
// System.err.println(">>> Computed children in " + Thread.currentThread());
178+
}
179+
if (Thread.interrupted() || currentWorker != null) {
180+
ret = null;
181+
// System.err.println(">>> Cancelled children in " + Thread.currentThread());
182+
}
183+
}
184+
185+
return ret;
155186
}
156187
protected void done() {
157188
if (children != null) try {
158-
// TODO: children not valid in case the sorting changed during computation!
159189
HeapViewerNode[] newChildren = get();
160190
// newChildren may be null, for example if the worker thread has been interrupted
161191
if (newChildren != null) {

0 commit comments

Comments
 (0)