Skip to content

MONITOR listeners are skipped when there are no ordinary listeners #114

Closed

Description

@Ecdcaeb

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

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:

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() returns true.
  • Removing the last ordinary listener does not stop the remaining monitor.
  • Removing the last monitor from an otherwise empty bus makes hasListeners() return false.

Activity

  1. LexManos commented on Nov 21, 2026

    @LexManos
    Member

    Seems like a simple issue enough.
    On a related note, if im reading the code correctly, monitor listeners are not inherited from inheritable events.

    var backingList = new ArrayList<EventListener>();
    List<EventBus<?>> parents = Collections.emptyList();
    if (Constants.isInheritable(characteristics)) {
    parents = getParentEvents(eventType);
    for (var parent : parents) {
    backingList.addAll(((AbstractEventBusImpl<?, ?>) parent).backingList());
    }
    }
    @SuppressWarnings("rawtypes")
    var bus = Constants.isCancellable(characteristics)
    ? new CancellableEventBusImpl<>(this.name, (Class) (Class<? extends Cancellable>) eventType, backingList, characteristics)
    : new EventBusImpl<>(this.name, eventType, backingList, characteristics);

    And the associated constructors have the same backingList.isEmpty() check that will need to be changed.

  2. self-assigned this
    on Nov 21, 2026
  3. PaintNinja commented on Nov 21, 2026

    @PaintNinja
    Contributor

    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 Cancellable or a dedicated BusGroup - monitoring listeners can be added to non-cancellable events, and you can have events dispose of themselves if you implement SelfDestructing.

    For the inheritance bug, please file a separate issue and I'll look into a fix for that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions