Repository navigation
Description
Monitoring listeners are silently skipped when an event bus has no ordinary listeners. In this state, hasListeners() also returns false, even though a monitor is registered.
This affects both:
- Registering a monitor on an otherwise empty bus.
- Removing the last ordinary listener while leaving a monitor registered.
I encountered this while integrating EventBus 7.0.6 into Cleanroom CleanroomMC/Cleanroom#749. The reproducer below uses only upstream public APIs and calls post() unconditionally, so this is independent of any downstream wrapper or hasListeners() guard.
Version and environment
- EventBus source revision corresponding to 7.0.6:
[588a6c7a2fb3d936aefc3c1bd89435c42f5a92d8](https://github.com/MinecraftForge/EventBus/commit/588a6c7a2fb3d936aefc3c1bd89435c42f5a92d8). - Java: OpenJDK 21.0.13.
- Reproduced using the unmodified upstream implementation compiled from this revision.
Minimal reproducer
import java.util.concurrent.atomic.AtomicInteger;
import net.minecraftforge.eventbus.api.bus.BusGroup;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.event.RecordEvent;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
public class MonitorOnlyRepro {
public record Signal() implements RecordEvent, Cancellable {}
private static final BusGroup GROUP =
BusGroup.create("monitor-only-repro");
private static final CancellableEventBus<Signal> BUS =
CancellableEventBus.create(GROUP, Signal.class);
public static void main(String[] args) {
try {
var calls = new AtomicInteger();
BUS.addListener((event, cancelled) -> calls.incrementAndGet());
BUS.post(new Signal());
System.out.printf("monitor only: hasListeners=%s, calls=%d%n",
BUS.hasListeners(), calls.get());
var ordinary = BUS.addListener(event -> {});
BUS.post(new Signal());
System.out.printf("with ordinary: hasListeners=%s, calls=%d%n",
BUS.hasListeners(), calls.get());
BUS.removeListener(ordinary);
BUS.post(new Signal());
System.out.printf("ordinary removed: hasListeners=%s, calls=%d%n",
BUS.hasListeners(), calls.get());
} finally {
GROUP.dispose();
}
}
}Actual output
monitor only: hasListeners=false, calls=0
with ordinary: hasListeners=true, calls=1
ordinary removed: hasListeners=false, calls=1
The monitor starts working when an ordinary listener is added, then stops working when that listener is removed. The monitor itself is never removed.
Expected output
monitor only: hasListeners=true, calls=1
with ordinary: hasListeners=true, calls=2
ordinary removed: hasListeners=true, calls=3
The monitor should receive all three events, with cancelled == false.
The same issue affects non-cancellable buses when a listener is registered with Priority.MONITOR.
Root cause
Both implementations choose the no-op invoker based only on whether the ordinary listener list is empty:
[EventBusImpl.invalidateInvoker()](https://github.com/MinecraftForge/EventBus/blob/588a6c7a2fb3d936aefc3c1bd89435c42f5a92d8/src/main/java/net/minecraftforge/eventbus/internal/EventBusImpl.java#L88-L92)[CancellableEventBusImpl.invalidateInvoker()](https://github.com/MinecraftForge/EventBus/blob/588a6c7a2fb3d936aefc3c1bd89435c42f5a92d8/src/main/java/net/minecraftforge/eventbus/internal/CancellableEventBusImpl.java#L123-L127)
monitorBackingList is not considered. When backingList is empty, the call site is set to MH_NO_OP_CONSUMER or MH_NO_OP_PREDICATE, even if monitoring listeners remain.
Consequently, hasListeners() returns false, and direct calls to post() also do nothing. Removing a downstream hasListeners() guard does not resolve the problem.
Suggested fix
In both invalidateInvoker() implementations, select the no-op invoker only when both lists are empty:
boolean empty = backingList.isEmpty() && monitorBackingList.isEmpty();Otherwise, invalidate the invoker so the existing monitoring dispatch path is rebuilt. The existing alreadyInvalidated guard should remain unchanged to preserve shutdown behavior.
Suggested regression coverage for both ordinary and cancellable buses:
- A monitor registered without any ordinary listeners is invoked, and
hasListeners()returnstrue. - Removing the last ordinary listener does not stop the remaining monitor.
- Removing the last monitor from an otherwise empty bus makes
hasListeners()returnfalse.
Activity
LexManos commentedon Nov 21, 2026
Seems like a simple issue enough.
On a related note, if im reading the code correctly, monitor listeners are not inherited from inheritable events.
EventBus/src/main/java/net/minecraftforge/eventbus/internal/BusGroupImpl.java
Lines 116 to 129 in 588a6c7
And the associated constructors have the same backingList.isEmpty() check that will need to be changed.
PaintNinja commentedon Nov 21, 2026
Thanks for your bug report, I'll release a fix shortly with a test case added to check for this.
By the way, the minimal repro you provided doesn't need
Cancellableor a dedicatedBusGroup- monitoring listeners can be added to non-cancellable events, and you can have events dispose of themselves if you implementSelfDestructing.For the inheritance bug, please file a separate issue and I'll look into a fix for that as well.