Skip to content

Fix BZ 70362 - don't restart async error handling after a completed dispatch - #1264

Open
codeconsole wants to merge 4 commits into
apache:mainfrom
codeconsole:bz-70362-async-complete-once
Open

codeconsole wants to merge 4 commits into
apache:mainfrom
codeconsole:bz-70362-async-complete-once

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes https://bz.apache.org/bugzilla/show_bug.cgi?id=70362

Since the fix for BZ 66875, CoyoteAdapter.asyncDispatch starts async error handling whenever the response is in an error state once an async dispatch returns. When that dispatch called sendError() and returned normally without starting async processing again, AsyncContextImpl.doInternalDispatch has already fired onComplete. The listeners are then told of an error with a null throwable and of completion a second time: [onComplete, onError(null), onComplete]. 10.1.15 and earlier delivered [onComplete].

The same early onComplete affects a dispatch that records a failure and returns (as InputBuffer does for a read error the application swallows) and a dispatch that throws after an I/O error: the listeners get onComplete, then onError, then onComplete again. Separately, sendError() followed by a new async cycle that the dispatch ends with complete() or dispatch() got onError(null) in that cycle.

The change:

  • doInternalDispatch records that it completed the async processing, and asyncDispatch does not start error handling in that case.
  • When the dispatch recorded a failure, or threw with I/O no longer allowed and error handling has not started yet, doInternalDispatch defers onComplete. The error handling then reports the failure first and completes once. If the error handling does not start, asyncDispatch fires the deferred onComplete itself, in a finally, so onComplete is never lost (BZ 69121).
  • An error response with no exception does not start error handling when the dispatch started a new async cycle and ended it with complete() or dispatch().
  • A dispatch that throws with I/O allowed is unchanged.
  • The flags are reset when async processing starts again and when the context is recycled.

Tests, in TestAsyncContextImpl:

  • Each test waits for the access log entry, made once processing of the request has ended, then checks the listener events, the throwable onError received and the in-progress async count.
  • sendError() with dispatch() on the request thread, from start() before the request thread exits service(), and from start() after it exits: onComplete-. These fail on main with onComplete-onError-onComplete-.
  • sendError(), then a new cycle ended with complete() or with dispatch(): onComplete-. Fails on main with onError-onComplete-.
  • A recorded failure, and an IOException thrown after the client has gone: onError-onComplete- with that exception. Fails on main with onComplete-onError-onComplete-.
  • A thrown ServletException: onError-onComplete- with that exception and a 500, the same as on main.
  • A failure the dispatch handles itself by writing a 500 response (BZ 66875): onComplete- only, the same as on main.

With the change, 996 tests in 39 classes pass on NIO: TestAsync*, every catalina.connector test (including TestCoyoteAdapterAsyncErrorState), TestStandardHostValve, TestStandardWrapper, TestErrorReportValve, TestNonBlockingAPI, TestHttp11Processor, TestAsyncStateMachine, and the HTTP/2 TestAsync* and TestStream* tests. ant validate is clean.

It should be back-ported to 11.0.x, 10.1.x and 9.0.x, which carry the 66875 change.

…ispatch

An asynchronous dispatch that called sendError() and returned normally
had already notified the AsyncListeners of completion when the check
added for BZ 66875 started async error handling, which notified them of
an error with no throwable and then of completion a second time.

https://bz.apache.org/bugzilla/show_bug.cgi?id=70362
@arturobernalg

Copy link
Copy Markdown
Member

Reproduced on main (f442523) with the servlet from the BZ: [onComplete, onError(null), onComplete]. With this PR: [onComplete], both for dispatch() on the request thread and via start(). Dispatches that return normally or throw behave as before, and TestAsyncContextImpl* passes.

Scott Murphy Heiberg added 3 commits November 21, 2026 21:48
…te-once

# Conflicts:
#	webapps/docs/changelog.xml
Cover the dispatch() made from a container-managed thread via start(),
and check that an exception thrown by the async dispatch still reaches
the listeners as onError before onComplete.
An asynchronous dispatch that recorded a failure and returned, or threw
after an I/O error, notified the listeners of completion before the
error handling in CoyoteAdapter.asyncDispatch notified them of the
failure and of completion again. The notification of completion is now
deferred in those cases so the error handling reports the failure first
and completes once, with CoyoteAdapter firing the deferred notification
if the error handling does not start.

An error response with no exception no longer starts async error
handling when the dispatch started a new async cycle and ended it with
complete() or dispatch(), which replaced that with onError(null).

The tests now wait for the access log entry, made once processing of
the request has ended, rather than a fixed delay; check the throwable
the listeners receive; cover dispatch() from start() both before and
after the request thread exits service(); and check the in-progress
async count returns to zero.
@codeconsole

Copy link
Copy Markdown
Author

Thanks for testing it, including the start() path. I've added tests for that path, for dispatch() from start() both before and after the request thread leaves service(), and I've widened the fix in f2da85a:

  • A dispatch that records a failure and returns, or throws after the client has gone, used to get onComplete, onError, onComplete. It now gets onError and then a single onComplete.
  • sendError() followed by a new async cycle that the dispatch ends with complete() or dispatch() no longer gets onError(null).

The tests wait for the access log entry rather than a fixed delay, check the throwable onError receives, and check that the in-progress async count returns to zero. I've also merged current main into the branch (455f626). The only conflict was the changelog, and I kept both entries.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants