mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(debugger): stream trace from an else-if line, of a stream made parallel earlier, and its cleanup
- At a stop on an `} else if` line, the earlier conditions have already run: their calls no longer count as exits to skip before the chain's root. The trace used to wait for a second `stream()` that never came. Conditions on the same line still count. - A stream that was parallel before the traced statement (`Stream<T> s = list.parallelStream(); … s.map(…)`) is checked with `isParallel()` when the trace arms and made sequential like a chain with `parallel()`, so its records link correctly; the result says it ran sequentially. - `arm` carries a token. When its reply times out, the client releases the trace armed with that token, so a slow arm no longer leaves breakpoints off and the next trace busy. - A finished trace's records keep their objects from being collected until the stop they were read in ends: they are released on the next resume, step or `continued` event instead of the next trace. The bundle is rebuilt; `tests/stream.py` (JDT LS 1.63.0, JDK 25) passes, with new cases for the three bundle changes.
This commit is contained in:
Binary file not shown.
@@ -6,6 +6,7 @@ import ai.openidea.jvminspect.JavaDebugSessions.HubEvent;
|
||||
import ai.openidea.jvminspect.RealRunPlan.StepSpec;
|
||||
import com.sun.jdi.AbsentInformationException;
|
||||
import com.sun.jdi.ArrayReference;
|
||||
import com.sun.jdi.BooleanValue;
|
||||
import com.sun.jdi.ClassType;
|
||||
import com.sun.jdi.Field;
|
||||
import com.sun.jdi.IncompatibleThreadStateException;
|
||||
@@ -162,6 +163,8 @@ final class RealRunTrace {
|
||||
/** Recorder number of the next wrapped op; modifiers take none. */
|
||||
private int nextStep;
|
||||
private ObjectReference lastWrapped;
|
||||
/** Whether the stream was made sequential: the plan asked, or the stream the chain starts from was parallel. */
|
||||
private volatile boolean sequentialized;
|
||||
/** {@code forceEarlyReturn} at a method exit makes that frame exit once more, returning the forced value. */
|
||||
private Method echoMethod;
|
||||
private Value echoValue;
|
||||
@@ -240,11 +243,14 @@ final class RealRunTrace {
|
||||
subscription = hub.subscribe(this::onEvent, e -> errors.add("event listener: " + e));
|
||||
if (held != null) {
|
||||
// Not put back: the program consumes the stream right after.
|
||||
lastWrapped = wrap(held, nextStep++, plan.sequentialize());
|
||||
// A stream made parallel before this statement shows no `parallel` in the chain.
|
||||
sequentialized = plan.sequentialize() || isParallel(held);
|
||||
lastWrapped = wrap(held, nextStep++, sequentialized);
|
||||
// The invokes resumed the thread, which invalidated the frame read before them.
|
||||
thread.frame(0).setValue(local, lastWrapped);
|
||||
} else {
|
||||
rootSkip = plan.root().skip();
|
||||
sequentialized = plan.sequentialize();
|
||||
}
|
||||
// Last: with these on, an invoke into the thread would raise the trace's own events inside it.
|
||||
// The step out of the frame never fires inside an invoke, which runs above it.
|
||||
@@ -402,7 +408,8 @@ final class RealRunTrace {
|
||||
}
|
||||
ObjectReference stream = returned(exit, root);
|
||||
setEnabled(false);
|
||||
replace(exit, wrap(stream, nextStep++, plan.sequentialize()));
|
||||
if (!sequentialized && isParallel(stream)) sequentialized = true;
|
||||
replace(exit, wrap(stream, nextStep++, sequentialized));
|
||||
return;
|
||||
}
|
||||
if (method.isStatic() || method.isConstructor() || !calledByUser() || !lastWrapped.equals(thread.frame(0).thisObject())) return;
|
||||
@@ -597,6 +604,11 @@ final class RealRunTrace {
|
||||
return sequentialize ? invoke(wrapped, "sequential") : wrapped;
|
||||
}
|
||||
|
||||
private boolean isParallel(ObjectReference stream) throws Exception {
|
||||
Method method = streamMethod(stream.referenceType(), "isParallel", 0);
|
||||
return stream.invokeMethod(thread, method, List.of(), INVOKE) instanceof BooleanValue b && b.value();
|
||||
}
|
||||
|
||||
private ObjectReference invoke(ObjectReference stream, String name, Value... arguments) throws Exception {
|
||||
Method method = streamMethod(stream.referenceType(), name, arguments.length);
|
||||
return keep(RecorderLoader.pin(() -> (ObjectReference) stream.invokeMethod(thread, method, List.of(arguments), INVOKE)));
|
||||
@@ -1008,7 +1020,7 @@ final class RealRunTrace {
|
||||
};
|
||||
int skipped = silencers.stream().mapToInt(Silencer::silencedBreakpoints).sum();
|
||||
return Values.json("records", records, "dropped", longOf(fields.get(field("dropped"))), "terminal", terminal,
|
||||
"skippedBreakpoints", skipped, "sequentialized", plan.sequentialize());
|
||||
"skippedBreakpoints", skipped, "sequentialized", sequentialized);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,6 +40,8 @@ public final class StreamCommand implements IDelegateCommandHandler {
|
||||
private static final int GONE_KEPT = 16;
|
||||
private static final AtomicLong IDS = new AtomicLong();
|
||||
private static final Map<VirtualMachine, RealRunTrace> TRACES = new IdentityHashMap<>();
|
||||
/** The {@code token} each trace was armed with: a client whose {@code arm} timed out releases by it. */
|
||||
private static final Map<RealRunTrace, String> TOKENS = new IdentityHashMap<>();
|
||||
/** By trace id, the answer {@link RealRunTrace#ended} gave: the trace itself would keep its dead VM's mirrors. */
|
||||
private static final Map<Long, Map<String, Object>> GONE = new LinkedHashMap<>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -64,6 +66,7 @@ public final class StreamCommand implements IDelegateCommandHandler {
|
||||
} catch (Failure e) {
|
||||
Map<String, Object> gone = name.equals("arm") ? null : gone(args);
|
||||
if (gone == null) throw e;
|
||||
if (name.equals("release") && args.get("token") instanceof String) return InspectCommand.ok(Map.of());
|
||||
return InspectCommand.ok(switch (name) {
|
||||
case "wait" -> gone;
|
||||
case "release" -> Map.of();
|
||||
@@ -76,7 +79,9 @@ public final class StreamCommand implements IDelegateCommandHandler {
|
||||
case "stop" -> Values.json("records", trace(vm, args).stopRecording());
|
||||
case "abort" -> trace(vm, args).abort();
|
||||
case "collect" -> collect(vm, target, trace(vm, args));
|
||||
case "release" -> release(vm, trace(vm, args));
|
||||
case "release" -> args.get("token") instanceof String token && !(args.get("traceId") instanceof Number)
|
||||
? releaseArmed(vm, token)
|
||||
: release(vm, trace(vm, args));
|
||||
default -> throw new IllegalArgumentException("unknown command " + command);
|
||||
});
|
||||
} catch (Failure e) {
|
||||
@@ -131,6 +136,7 @@ public final class StreamCommand implements IDelegateCommandHandler {
|
||||
RealRunTrace trace = RealRunTrace.arm(IDS.incrementAndGet(), vm, thread, plan, budget, target.generation(), hub);
|
||||
synchronized (TRACES) {
|
||||
TRACES.put(vm, trace);
|
||||
if (args.get("token") instanceof String token) TOKENS.put(trace, token);
|
||||
}
|
||||
return trace.id;
|
||||
}
|
||||
@@ -166,11 +172,27 @@ public final class StreamCommand implements IDelegateCommandHandler {
|
||||
if (!trace.state().wire.equals("running")) {
|
||||
synchronized (TRACES) {
|
||||
TRACES.remove(vm, trace);
|
||||
TOKENS.remove(trace);
|
||||
}
|
||||
}
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* The trace armed with {@code token}, whose {@code arm} reply the client gave up on: released,
|
||||
* so its breakpoints come back on. Under the arm lock, so an arm still setting up finishes first.
|
||||
*/
|
||||
private static Map<String, Object> releaseArmed(VirtualMachine vm, String token) {
|
||||
synchronized (InspectCommand.handlesFor(vm)) {
|
||||
RealRunTrace current;
|
||||
synchronized (TRACES) {
|
||||
current = TRACES.get(vm);
|
||||
if (current == null || !token.equals(TOKENS.get(current))) return Map.of();
|
||||
}
|
||||
return release(vm, current);
|
||||
}
|
||||
}
|
||||
|
||||
/** Releases the traces of VMs java-debug no longer has; their requests and objects went with the VM. */
|
||||
private static void forgetGone(List<VmInfo> vms) {
|
||||
Set<VirtualMachine> live = Collections.newSetFromMap(new IdentityHashMap<>());
|
||||
@@ -182,6 +204,7 @@ public final class StreamCommand implements IDelegateCommandHandler {
|
||||
if (live.contains(e.getKey())) continue;
|
||||
RealRunTrace trace = e.getValue();
|
||||
trace.vmGone();
|
||||
TOKENS.remove(trace);
|
||||
GONE.put(trace.id, trace.ended());
|
||||
gone.add(trace);
|
||||
i.remove();
|
||||
|
||||
@@ -91,6 +91,8 @@ public class Real {
|
||||
case "H": h(); break;
|
||||
case "local": local(); break;
|
||||
case "parallel": parallel(); break;
|
||||
case "plocal": parallelLocal(); break;
|
||||
case "elif": elseIf(); break;
|
||||
case "nested": nested(); break;
|
||||
case "hostile": hostile(); break;
|
||||
case "sr": stopRecording(Integer.parseInt(a[1])); break;
|
||||
@@ -167,6 +169,20 @@ public class Real {
|
||||
System.out.println("P " + p + " " + q);
|
||||
}
|
||||
|
||||
static void parallelLocal() {
|
||||
Stream<Integer> s = nums.parallelStream();
|
||||
List<Integer> r = s.map(x -> x + 1).collect(Collectors.toList()); // @plocal
|
||||
System.out.println("PL " + r);
|
||||
}
|
||||
|
||||
static void elseIf() {
|
||||
if (nums.stream().anyMatch(x -> x > 100)) {
|
||||
System.out.println("ELIF no");
|
||||
} else if (nums.stream().anyMatch(x -> x == 8)) { // @elif
|
||||
System.out.println("ELIF yes");
|
||||
}
|
||||
}
|
||||
|
||||
static void nested() {
|
||||
long n = nums.stream().flatMap(x -> Stream.of(x, x)).count(); // @nested
|
||||
System.out.println("NESTED " + n);
|
||||
@@ -833,6 +849,61 @@ def real_run_parallel():
|
||||
dbg.end()
|
||||
|
||||
|
||||
@case
|
||||
def real_run_parallel_local():
|
||||
dbg = real('plocal')
|
||||
try:
|
||||
# No `parallel` in the chain: the stream was made parallel before the statement.
|
||||
trace, state, _ = run_until_parked(dbg, 'plocal', root=None, local='s', steps=(spec('map', 1),), step_kinds=('sync',),
|
||||
class_filters=('java.util.stream.*',))
|
||||
c = step_out_and_collect(dbg, trace)
|
||||
collected_ok('parallel local', c, records='0=5 1=6 0=3 1=4 0=8 1=9 0=1 1=2 0=4 1=5 0=2 1=3 0=9 1=10 0=6 1=7'.split())
|
||||
check('parallel local: sequentialized', c['sequentialized'] is True, c['sequentialized'])
|
||||
resume(dbg)
|
||||
check('parallel local: program output', output_line(dbg, 'PL ') == 'PL [6, 4, 9, 2, 5, 3, 10, 7]')
|
||||
finally:
|
||||
dbg.end()
|
||||
|
||||
|
||||
@case
|
||||
def real_run_else_if():
|
||||
dbg = real('elif')
|
||||
try:
|
||||
# Stopped on the `else if` line, the first condition's `stream()` has already returned.
|
||||
trace, state, _ = run_until_parked(dbg, 'elif', steps=(), terminal=spec('anyMatch', 1), step_kinds=(), timeout=20)
|
||||
check('else if: the trace parks with nothing skipped', state['state'] == 'parked', state)
|
||||
c = step_out_and_collect(dbg, trace)
|
||||
collected_ok('else if', c, terminal='true')
|
||||
resume(dbg)
|
||||
check('else if: program output', output_line(dbg, 'ELIF ') == 'ELIF yes')
|
||||
finally:
|
||||
dbg.end()
|
||||
|
||||
|
||||
@case
|
||||
def release_by_arm_token():
|
||||
dbg = launch()
|
||||
try:
|
||||
def arm_with(token):
|
||||
return stream('arm', dbg, threadId=dbg.thread, plan=plan('loop'), budget=50_000, token=token)
|
||||
|
||||
ok(arm_with('first'))
|
||||
check('release by another token leaves the trace', ok(stream('release', dbg, token='other')) == {})
|
||||
check('so the next arm is busy', error(arm_with('second')) == 'busy')
|
||||
check('release by the arm\'s token answers {}', ok(stream('release', dbg, token='first')) == {})
|
||||
trace = ok(arm_with('third'))['traceId']
|
||||
check('arm works again after it', trace is not None, trace)
|
||||
ok(stream('release', dbg, traceId=trace))
|
||||
resume(dbg)
|
||||
try:
|
||||
body = expect_stop(dbg, 10, 'stop after release')
|
||||
check('breakpoints are back after a release by token', body.get('reason') == 'breakpoint', body)
|
||||
except TimeoutError:
|
||||
check('breakpoints are back after a release by token', False, 'no stop within 10 s')
|
||||
finally:
|
||||
dbg.end()
|
||||
|
||||
|
||||
@case
|
||||
def real_run_nested():
|
||||
dbg = real('nested')
|
||||
|
||||
Reference in New Issue
Block a user