agentscope-java
agentscope-java copied to clipboard
[Bug]: WorkspaceMessageBus.subscribe() heartbeat ticks on parallel (NonBlocking); WakeupDispatcher .block() throws IllegalStateException
Describe the bug
WorkspaceMessageBus.subscribe() (agentscope-harness, io.agentscope.harness.agent.bus) returns a heartbeat Flux:
return Flux.interval(POLL_INTERVAL)
.map(tick -> Map.<String, Object>of())
.subscribeOn(Schedulers.boundedElastic());
subscribeOn(...) has no effect on Flux.interval: the periodic ticks are emitted by interval's own timer, which defaults to Schedulers.parallel(). subscribeOn only relocates the
subscribe call, not the timer emissions — so every heartbeat tick and everything downstream of it runs on a parallel thread.
The tick consumer WakeupDispatcher (agentscope-harness/.../gateway/WakeupDispatcher.java) does blocking work on each tick:
// WakeupDispatcher.start()
messageBus.subscribeWakeup().subscribe(signal -> drainAndDispatch(), ...);
// WakeupDispatcher.drainAndDispatch() (line ~104)
List<BusEntry> entries = messageBus.queueDrain("agentscope:wakeups", MAX_DRAIN_COUNT).block();
Schedulers.parallel() threads implement reactor.core.scheduler.NonBlocking, and calling Mono.block() on a NonBlocking thread is rejected by Reactor:
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
In WakeupDispatcher this exception is currently caught and logged (catch (Exception e) → log.warn(...)), so the dispatcher silently stops draining wakeup signals rather than
propagating the error. In setups where the deprecated global tracing hook is active (see Additional context), the same defect surfaces as an uncaught exception.
To Reproduce
Minimal repro — no tracing hook needed (the tick is on parallel regardless):
WorkspaceMessageBus bus = new WorkspaceMessageBus(fs, "/bus");
AtomicReference<Throwable> err = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
bus.subscribe("repro")
.doOnNext(m -> { try { bus.queuePeek("x").block(); } catch (Throwable t) { err.set(t); } })
.take(1)
.subscribe(m -> latch.countDown());
latch.await(5, TimeUnit.SECONDS);
// err.get() → IllegalStateException: ... not supported in thread parallel-1
Expected behavior
The heartbeat should tick on a scheduler where blocking per-tick work is legal — boundedElastic, whose threads are not NonBlocking. Then WakeupDispatcher.drainAndDispatch()'s
.block() is always legal.
Error messages
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
at reactor.core.publisher.Mono.block(...)
at ...WakeupDispatcher.drainAndDispatch(WakeupDispatcher.java:104)
Suggested fix
Pass the scheduler to Flux.interval so the timer itself runs on boundedElastic:
@Override
public Flux<Map<String, Object>> subscribe(String key) {
return Flux.interval(POLL_INTERVAL, Schedulers.boundedElastic())
.map(tick -> Map.<String, Object>of());
}
Flux.interval(Duration, Scheduler) runs the periodic emission on the supplied scheduler; boundedElastic threads are non-NonBlocking, so the downstream .block() is always legal.
Verified: with this change the tick emits on boundedElastic-* and drainAndDispatch().block() no longer throws — including with the global tracing hook enabled.
Environment
- AgentScope-Java Version: 2.0.0-SNAPSHOT (current main)
- Java Version: 21 (source target 17)
- OS: Windows 10
Additional context
The defect is always present but often invisible because WakeupDispatcher.drainAndDispatch() catches the IllegalStateException. It can surface as an uncaught exception when a
non-Noop Tracer is registered via TracerRegistry.register(...), which enables a global Hooks.onEachOperator lift (TracerRegistry.enableTracingHook()) wrapping every Reactor
operator in the JVM. Note TracerRegistry is @Deprecated(forRemoval = true, since = "2.0.0") ("Use OtelTracingMiddleware instead") and its Javadoc warns the hook "affects all
Reactor operations in the JVM." The proposed fix makes the heartbeat independent of that hook.
---