Skip to content

[fix][broker] Return failed future instead of throwing exception in async methods#25289

Open
zhanghaou wants to merge 7 commits intoapache:masterfrom
zhanghaou:fix-broker-not-complete-future
Open

[fix][broker] Return failed future instead of throwing exception in async methods#25289
zhanghaou wants to merge 7 commits intoapache:masterfrom
zhanghaou:fix-broker-not-complete-future

Conversation

@zhanghaou
Copy link
Contributor

@zhanghaou zhanghaou commented Mar 5, 2026

same to this pr #25287

Motivation

In Java's CompletableFuture, callbacks registered via stages such as thenApply, thenCompose, etc., are executed within internal try/catch blocks. If a callback throws an exception, the framework automatically captures it and completes the dependent future exceptionally.

Therefore, throwing an exception inside a stage callback is safe and will be translated into an exceptionally completed future.

However, if an async method itself throws an exception instead of returning a failed future, the exception is raised synchronously and no CompletableFuture is returned. This breaks the expected async error propagation model.

Some async validation paths in the broker currently throw exceptions directly instead of returning a failed CompletableFuture. This breaks the async contract of methods that return CompletableFuture, because the exception is thrown synchronously at invocation time rather than being propagated through the future.

As a result, callers relying on async error handling (e.g. exceptionally, handle, or whenComplete) may not observe these failures correctly.

Modifications

  1. Convert validation synchronous throws to failed futures in broker async methods.
  2. Refactor original-principal validation to an async helper and preserve original auth/authorization semantics.
  3. Ensure default async UnsupportedOperationException paths return failed futures.

Verifying this change

  • Make sure that the change passes the CI checks.

(Please pick either of the following options)

This change is a trivial rework / code cleanup without any test coverage.

(or)

This change is already covered by existing tests, such as (please describe tests).

(or)

This change added tests and can be verified as follows:

(example:)

  • Added integration tests for end-to-end deployment with large payloads (10MB)
  • Extended integration test for recovery after broker failure

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository:

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Mar 5, 2026
@zhanghaou zhanghaou marked this pull request as draft March 5, 2026 09:22
@zhanghaou zhanghaou marked this pull request as ready for review March 5, 2026 11:43
@codelipenghui codelipenghui added this to the 4.2.0 milestone Mar 5, 2026
Copy link
Contributor

@codelipenghui codelipenghui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. The core intent is sound — methods returning CompletableFuture should not throw synchronous exceptions. A few issues found below.

张浩 added 2 commits March 6, 2026 10:23
@zhanghaou zhanghaou requested a review from codelipenghui March 6, 2026 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants