Skip to content

Commit f5b7d19

Browse files
committed
[GR-42173] Migrated Chrome Inspector and DAP to system threads.
PullRequest: graal/16525
2 parents 94ea46c + 11233a9 commit f5b7d19

File tree

9 files changed

+149
-71
lines changed

9 files changed

+149
-71
lines changed

tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorTestInstrument.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -97,6 +97,7 @@ protected void onFinalize(Env env) {
9797
InspectServerSession session = (sessionRef != null) ? sessionRef.get() : null;
9898
if (session != null) {
9999
session.notifyClosing();
100+
session.dispose();
100101
}
101102
}
102103

tools/src/com.oracle.truffle.tools.chromeinspector.test/src/com/oracle/truffle/tools/chromeinspector/test/InspectorTester.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -347,11 +347,6 @@ public void run() {
347347
// To cleanup any references to the closed context, we need to set a new instance
348348
// of ProxyLanguage.
349349
ProxyLanguage.setDelegate(new ProxyLanguage());
350-
try {
351-
inspect.sendClose();
352-
} catch (IOException e) {
353-
fail(e.getLocalizedMessage());
354-
}
355350
inspect = null;
356351
inspectorContext = null;
357352
evalValue = null;

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/InspectorDebugger.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,8 @@
2626

2727
import java.io.File;
2828
import java.io.PrintWriter;
29+
import java.lang.ref.Reference;
30+
import java.lang.ref.WeakReference;
2931
import java.util.ArrayList;
3032
import java.util.HashSet;
3133
import java.util.LinkedList;
@@ -1064,6 +1066,7 @@ private class SuspendedCallbackImpl implements SuspendedCallback {
10641066
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, new SchedulerThreadFactory());
10651067
private final AtomicReference<ScheduledFuture<?>> future = new AtomicReference<>();
10661068
private Thread locked = null;
1069+
private volatile Reference<Thread> schedulerThread;
10671070

10681071
@Override
10691072
public void onSuspend(SuspendedEvent se) {
@@ -1266,27 +1269,27 @@ private void dispose() {
12661269
scheduler.shutdown();
12671270
try {
12681271
scheduler.awaitTermination(30, TimeUnit.SECONDS);
1272+
Thread t = (schedulerThread != null) ? schedulerThread.get() : null;
1273+
if (t != null) {
1274+
t.join();
1275+
}
12691276
} catch (InterruptedException e) {
1277+
// Interrupted
12701278
}
12711279
}
1272-
}
12731280

1274-
private static class SchedulerThreadFactory implements ThreadFactory {
1281+
private class SchedulerThreadFactory implements ThreadFactory {
12751282

1276-
private final ThreadGroup group;
1277-
1278-
@SuppressWarnings("deprecation")
1279-
SchedulerThreadFactory() {
1280-
SecurityManager s = System.getSecurityManager();
1281-
this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
1282-
}
1283+
SchedulerThreadFactory() {
1284+
}
12831285

1284-
@Override
1285-
public Thread newThread(Runnable r) {
1286-
Thread t = new Thread(group, r, "Suspend Unlocking Scheduler");
1287-
t.setDaemon(true);
1288-
t.setPriority(Thread.NORM_PRIORITY);
1289-
return t;
1286+
@Override
1287+
public Thread newThread(Runnable r) {
1288+
Thread t = context.getEnv().createSystemThread(r);
1289+
t.setName("chromeinspector.server.Suspend_Unlocking_Scheduler");
1290+
schedulerThread = new WeakReference<>(t);
1291+
return t;
1292+
}
12901293
}
12911294
}
12921295

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectServerSession.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -98,7 +98,7 @@ private static IOException createClosedException() {
9898

9999
@Override
100100
public void sendClose() throws IOException {
101-
if (processThread == null) {
101+
if (isClosed()) {
102102
throw createClosedException();
103103
}
104104
Runnable onCloseRunnable = onClose;
@@ -109,7 +109,8 @@ public void sendClose() throws IOException {
109109
}
110110

111111
boolean isClosed() {
112-
return processThread == null;
112+
CommandProcessThread cmdProcessThread = processThread;
113+
return cmdProcessThread == null || cmdProcessThread.isClosed();
113114
}
114115

115116
// For tests only
@@ -141,7 +142,6 @@ public void dispose() {
141142
cmdProcessThread = processThread;
142143
if (cmdProcessThread != null) {
143144
cmdProcessThread.dispose();
144-
processThread = null;
145145
}
146146
}
147147
if (cmdProcessThread != null) {
@@ -189,7 +189,7 @@ private void startUp() {
189189

190190
@Override
191191
public void sendText(String message) throws IOException {
192-
if (processThread == null) {
192+
if (isClosed()) {
193193
throw createClosedException();
194194
}
195195
Command cmd;
@@ -271,14 +271,14 @@ public void sendBinary(ByteBuffer data) {
271271

272272
@Override
273273
public void sendPing(ByteBuffer data) throws IOException {
274-
if (processThread == null) {
274+
if (isClosed()) {
275275
throw createClosedException();
276276
}
277277
}
278278

279279
@Override
280280
public void sendPong(ByteBuffer data) throws IOException {
281-
if (processThread == null) {
281+
if (isClosed()) {
282282
throw createClosedException();
283283
}
284284
}
@@ -618,14 +618,15 @@ void run() {
618618
}
619619
}
620620

621-
private class CommandProcessThread extends Thread {
621+
private class CommandProcessThread implements Runnable {
622622

623+
private final Thread thread;
623624
private volatile boolean disposed = false;
624625
private final BlockingQueue<Command> commands = new LinkedBlockingQueue<>();
625626

626627
CommandProcessThread() {
627-
super("chromeinspector.server.CommandProcessThread");
628-
setDaemon(true);
628+
thread = context.getEnv().createSystemThread(this);
629+
thread.setName("chromeinspector.server.CommandProcessThread");
629630
}
630631

631632
void push(Command cmd) {
@@ -639,9 +640,17 @@ void push(Command cmd) {
639640
}
640641
}
641642

643+
void start() {
644+
thread.start();
645+
}
646+
642647
void dispose() {
643648
disposed = true;
644-
interrupt();
649+
thread.interrupt();
650+
}
651+
652+
boolean isClosed() {
653+
return disposed && !thread.isAlive();
645654
}
646655

647656
@Override
@@ -679,6 +688,11 @@ public void run() {
679688
}
680689
}
681690

691+
private void join() throws InterruptedException {
692+
thread.join();
693+
commands.clear();
694+
}
695+
682696
}
683697

684698
}

tools/src/com.oracle.truffle.tools.chromeinspector/src/com/oracle/truffle/tools/chromeinspector/server/InspectorServer.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -470,6 +470,7 @@ public void close(Token token) throws IOException {
470470
if (iws != null) {
471471
iws.connection.close(1001 /* Going Away */);
472472
}
473+
sps.dispose();
473474
}
474475
if (sessions.isEmpty()) {
475476
try {
@@ -526,6 +527,18 @@ boolean getDebugBrkAndReset() {
526527
ConnectionWatcher getConnectionWatcher() {
527528
return connectionWatcher;
528529
}
530+
531+
void dispose() {
532+
InspectServerSession iss = serverSession.getAndSet(null);
533+
if (iss != null) {
534+
iss.dispose();
535+
} else {
536+
InspectWebSocketHandler iws = activeWS;
537+
if (iws != null) {
538+
iws.disposeSession();
539+
}
540+
}
541+
}
529542
}
530543

531544
private class InspectWebSocketHandler {
@@ -612,6 +625,10 @@ void onPing() {
612625
void onPong() {
613626
iss.context.logMessage("CLIENT: ", "PONG");
614627
}
628+
629+
void disposeSession() {
630+
iss.dispose();
631+
}
615632
}
616633

617634
private static class HTTPChannelWrapper implements ByteChannel {

0 commit comments

Comments
 (0)