greenrobot/EventBus · error · EventBusException

event handlers may only abort the incoming event

Error message

 event handlers may only abort the incoming event

What it means

Thrown by cancelEventDelivery(Object) when postingState.subscription.subscriberMethod.threadMode != ThreadMode.POSTING (note the message literally begins with a space: ' event handlers may only abort the incoming event'). Even though the call happens during dispatch, the currently executing subscriber method was routed to another thread (MAIN, BACKGROUND, ASYNC) via the HandlerPoster/BackgroundPoster/AsyncPoster machinery; that thread has no PostingThreadState with isPosting=true that owns the event loop, so cancellation of subsequent subscribers is impossible from there.

Source

Thrown at EventBus/src/org/greenrobot/eventbus/EventBus.java:301

    /**
     * Called from a subscriber's event handling method, further event delivery will be canceled. Subsequent
     * subscribers
     * won't receive the event. Events are usually canceled by higher priority subscribers (see
     * {@link Subscribe#priority()}). Canceling is restricted to event handling methods running in posting thread
     * {@link ThreadMode#POSTING}.
     */
    public void cancelEventDelivery(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        if (!postingState.isPosting) {
            throw new EventBusException(
                    "This method may only be called from inside event handling methods on the posting thread");
        } else if (event == null) {
            throw new EventBusException("Event may not be null");
        } else if (postingState.event != event) {
            throw new EventBusException("Only the currently handled event may be aborted");
        } else if (postingState.subscription.subscriberMethod.threadMode != ThreadMode.POSTING) {
            throw new EventBusException(" event handlers may only abort the incoming event");
        }

        postingState.canceled = true;
    }

    /**
     * Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky
     * event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}.
     */
    public void postSticky(Object event) {
        synchronized (stickyEvents) {
            stickyEvents.put(event.getClass(), event);
        }
        // Should be posted after it is putted, in case the subscriber wants to remove immediately
        post(event);
    }

    /**

View on GitHub (pinned to 0194926b3b)

Solutions

  1. Add a dedicated POSTING-mode subscriber with high priority that performs the cancellation: @Subscribe(threadMode = ThreadMode.POSTING, priority = 100)
  2. Keep the business logic in the MAIN/BACKGROUND handler; only the cancel call moves to the POSTING handler

Example fix

// before
@Subscribe(threadMode = ThreadMode.MAIN) // default mode
public void onEvent(OrderEvent event) {
    EventBus.getDefault().cancelEventDelivery(event); // throws: threadMode != POSTING
}

// after
@Subscribe(threadMode = ThreadMode.POSTING, priority = 100)
public void onEventCancelOrders(OrderEvent event) {
    EventBus.getDefault().cancelEventDelivery(event);
}
Defensive patterns

Strategy: validation

Validate before calling

// Dedicated high-priority POSTING handler owns all cancellation
@Subscribe(threadMode = ThreadMode.POSTING, priority = 100)
public void onEventCancel(MessageEvent event) {
    if (shouldSuppress(event)) {
        EventBus.getDefault().cancelEventDelivery(event);
    }
}

Prevention

When it happens

Trigger: Annotated a subscriber with @Subscribe(threadMode = ThreadMode.MAIN) (the default) or BACKGROUND/ASYNC and calling cancelEventDelivery inside it; posting from a background thread so the subscriber runs on the main thread while the posting loop lives on the original thread.

Common situations: Default threadMode is MAIN in many code samples, so developers add cancellation to a MAIN subscriber and hit this; UI code that cancels events from the main thread while events are posted by worker threads.

Related errors


AI-assisted analysis of greenrobot/EventBus@0194926b3b (2026-08-14). Data as JSON: /api/errors/719cb0f95abb2273. Report an issue: GitHub.