{"record":{"id":"6e292bfe37be86b3","repo":"greenrobot/EventBus","slug":"only-the-currently-handled-event-may-be-aborted","errorCode":null,"errorMessage":"Only the currently handled event may be aborted","messagePattern":"Only the currently handled event may be aborted","errorType":"exception","errorClass":"EventBusException","httpStatus":null,"severity":"error","filePath":"EventBus/src/org/greenrobot/eventbus/EventBus.java","lineNumber":299,"sourceCode":"        }\n    }\n\n    /**\n     * Called from a subscriber's event handling method, further event delivery will be canceled. Subsequent\n     * subscribers\n     * won't receive the event. Events are usually canceled by higher priority subscribers (see\n     * {@link Subscribe#priority()}). Canceling is restricted to event handling methods running in posting thread\n     * {@link ThreadMode#POSTING}.\n     */\n    public void cancelEventDelivery(Object event) {\n        PostingThreadState postingState = currentPostingThreadState.get();\n        if (!postingState.isPosting) {\n            throw new EventBusException(\n                    \"This method may only be called from inside event handling methods on the posting thread\");\n        } else if (event == null) {\n            throw new EventBusException(\"Event may not be null\");\n        } else if (postingState.event != event) {\n            throw new EventBusException(\"Only the currently handled event may be aborted\");\n        } else if (postingState.subscription.subscriberMethod.threadMode != ThreadMode.POSTING) {\n            throw new EventBusException(\" event handlers may only abort the incoming event\");\n        }\n\n        postingState.canceled = true;\n    }\n\n    /**\n     * Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky\n     * event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}.\n     */\n    public void postSticky(Object event) {\n        synchronized (stickyEvents) {\n            stickyEvents.put(event.getClass(), event);\n        }\n        // Should be posted after it is putted, in case the subscriber wants to remove immediately\n        post(event);\n    }","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/greenrobot/EventBus/blob/0194926b3bcf70cc0d7bfd3c5da16708dd5ab876/EventBus/src/org/greenrobot/eventbus/EventBus.java#L281-L317","documentation":"Thrown by cancelEventDelivery(Object) when postingState.event != event — a reference-identity comparison against the event currently being dispatched on this thread. Cancellation can only abort the one event whose delivery loop is on the thread's PostingThreadState stack; passing a different instance (even an equal() one, e.g. a reconstructed copy or another event of the same class) is rejected because cancelling it would corrupt unrelated dispatch state.","triggerScenarios":"Constructing a new event instance (new MessageEvent(...)) and passing it to cancelEventDelivery instead of the instance received in the subscriber method; keeping the last event in a field and cancelling a previously delivered one; calling cancel for event B from inside the handler for event A.","commonSituations":"Developers assuming cancelEventDelivery works by event type or equality rather than instance identity; caching events and trying to retroactively cancel an already-delivered event.","solutions":["Always forward the exact instance from the subscriber method parameter to cancelEventDelivery(event)","Do not attempt to cancel past events — cancellation only affects subscribers that have not yet received the current event","If you need type-based suppression, unsubscribe or filter at post() time instead"],"exampleFix":"// before\nEventBus.getDefault().cancelEventDelivery(new MessageEvent(id)); // new instance -> throws\n\n// after\n@Subscribe(threadMode = ThreadMode.POSTING, priority = 100)\npublic void onEvent(MessageEvent event) {\n    EventBus.getDefault().cancelEventDelivery(event); // same instance being dispatched\n}","handlingStrategy":"validation","validationCode":"// cancel must use the exact instance handed to the subscriber\n@Subscribe(threadMode = ThreadMode.POSTING)\npublic void onEvent(MessageEvent event) {\n    EventBus.getDefault().cancelEventDelivery(event); // same reference, never a copy\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never construct a new event instance for cancellation","Remember comparison is by identity (==), not equals()"],"tags":["eventbus","cancellation","identity","validation"],"backgroundTag":null,"analyzedSha":"0194926b3bcf70cc0d7bfd3c5da16708dd5ab876","analyzedAt":"2026-08-14T10:41:08.786Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}