Skip to content

Close Channel when connect is cancelled during resolution - #17321

Merged
normanmaurer merged 7 commits into
netty:4.2from
Gimini-3:fix-bootstrap-resolve-cancellation
Aug 28, 2026
Merged

normanmaurer merged 7 commits into
netty:4.2from
Gimini-3:fix-bootstrap-resolve-cancellation

Conversation

@Gimini-3

Copy link
Copy Markdown
Contributor

Motivation:

Bootstrap.connect(...) creates and registers a Channel before asynchronous address resolution. If the connect future is cancelled while resolution is pending, the Channel remains open until resolution completes. A late resolver failure also attempts to fail the cancelled promise and logs IllegalStateException: complete already.

Modification:

  • Install the close-on-failure listener before address resolution starts so cancellation closes the registered Channel.
  • Skip address resolution when cancellation wins the registration race.
  • Ignore late resolver completion after the connect promise is done and use tryFailure(...) for cancellation races.
  • Keep the resolver future active so shared in-flight resolutions are not cancelled.
  • Add regression tests for cancellation during resolution with a real NIO Channel and before delayed registration completes.

Result:

Cancelling a Bootstrap connect closes its Channel while address resolution is pending, late resolver completion no longer tries to complete a cancelled promise, and delayed registration does not start unnecessary resolution.

Gimini-3 and others added 2 commits August 25, 2026 01:49
Install the close-on-failure listener before asynchronous address resolution starts. Ignore late resolver completion after cancellation and avoid resolving addresses when cancellation wins the registration race.
@normanmaurer

Copy link
Copy Markdown
Member

@Gimini-3 I think it would be better to call setUncancellable(.....) on the promise and don't use try*.

Mark the connect promise uncancellable before address resolution starts and use strict promise completion methods. Keep cancellation before registration supported and covered by tests.
@Gimini-3

Copy link
Copy Markdown
Contributor Author

@normanmaurer Thanks, updated. The promise is now marked uncancellable before address resolution starts, and the try* calls and late isDone() guard have been removed. Cancellation that wins before registration still closes the Channel and skips resolution. I also updated the tests to cover both boundaries; ./mvnw -pl transport test passes (397 tests, 9 skipped).

@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Aug 27, 2026
@normanmaurer
normanmaurer enabled auto-merge (squash) August 27, 2026 06:26
auto-merge was automatically disabled August 27, 2026 06:26

Pull request was closed

@normanmaurer normanmaurer reopened this Aug 27, 2026
@normanmaurer

Copy link
Copy Markdown
Member

@Gimini-3 hmm I think this needs some more thoughts... as the unit test failures show as with the current changes cancellation is not possible at all. So maybe your original changes were better. WDYT ?

@Gimini-3

Copy link
Copy Markdown
Contributor Author

I agree that the original approach is better here. setUncancellable() changes the semantics of the entire connect operation and prevents cancellation even after address resolution, as SocketConnectionAttemptTest shows.
The original approach preserves connect cancellation by installing CLOSE_ON_FAILURE before resolution, returning when the connect promise is already done, and safely ignoring late resolver completion without cancelling the resolver future. Would you prefer that I restore this approach?

@normanmaurer

Copy link
Copy Markdown
Member

Yes

@Gimini-3

Copy link
Copy Markdown
Contributor Author

Thanks, restored the original approach in b5f9f40. The relevant tests pass locally, and CI is running.


// Wait until the name resolution is finished.
resolveFuture.addListener((FutureListener<SocketAddress>) future -> {
if (promise.isDone()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is this correct ? This means we would also not try to close the channel in this of a failure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. Updated in 306143a1d7 to handle resolver failures first, so the Channel is always closed on failure. A completed promise now only skips doConnect(...) after successful resolution.

@testlens-app

testlens-app Bot commented Aug 28, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 306143a
▶️ Tests: 450798 executed
⚪️ Checks: 18/18 completed


Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit 66af5f8 into netty:4.2 Aug 28, 2026
19 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 4.1.

@netty-project-bot

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 5.0.

@normanmaurer

Copy link
Copy Markdown
Member

PR for 4.1 #17349

normanmaurer added a commit that referenced this pull request Aug 28, 2026
…17349)

Motivation:

`Bootstrap.connect(...)` creates and registers a Channel before
asynchronous address resolution. If the connect future is cancelled
while resolution is pending, the Channel remains open until resolution
completes. A late resolver failure also attempts to fail the cancelled
promise and logs `IllegalStateException: complete already`.

Modification:

- Install the close-on-failure listener before address resolution starts
so cancellation closes the registered Channel.
- Skip address resolution when cancellation wins the registration race.
- Ignore late resolver completion after the connect promise is done and
use `tryFailure(...)` for cancellation races.
- Keep the resolver future active so shared in-flight resolutions are
not cancelled.
- Add regression tests for cancellation during resolution with a real
NIO Channel and before delayed registration completes.

Result:

Cancelling a Bootstrap connect closes its Channel while address
resolution is pending, late resolver completion no longer tries to
complete a cancelled promise, and delayed registration does not start
unnecessary resolution.

---------

Co-authored-by: Norman Maurer <[email protected]>

---------

Co-authored-by: Gimin Kim <[email protected]>
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
@normanmaurer normanmaurer removed needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Oct 16, 2026
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
… (netty#17349)

Motivation:

`Bootstrap.connect(...)` creates and registers a Channel before
asynchronous address resolution. If the connect future is cancelled
while resolution is pending, the Channel remains open until resolution
completes. A late resolver failure also attempts to fail the cancelled
promise and logs `IllegalStateException: complete already`.

Modification:

- Install the close-on-failure listener before address resolution starts
so cancellation closes the registered Channel.
- Skip address resolution when cancellation wins the registration race.
- Ignore late resolver completion after the connect promise is done and
use `tryFailure(...)` for cancellation races.
- Keep the resolver future active so shared in-flight resolutions are
not cancelled.
- Add regression tests for cancellation during resolution with a real
NIO Channel and before delayed registration completes.

Result:

Cancelling a Bootstrap connect closes its Channel while address
resolution is pending, late resolver completion no longer tries to
complete a cancelled promise, and delayed registration does not start
unnecessary resolution.

---------

Co-authored-by: Norman Maurer <[email protected]>

---------

Co-authored-by: Gimin Kim <[email protected]>
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.

3 participants