GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port: LocalChannel: complete connect() when the accepted channel dies before registration (#17656) by normanmaurer · Pull Request #17666 · netty/netty

Conversation

@normanmaurer

@normanmaurer normanmaurer commented Sep 29, 2026

Copy link
Copy Markdown
Member

Motivation:

If the server side LocalChannel of a connection is closed before it is
registered, the client's connect() never completes (the local
transport has no connect timeout). This happens when:

  • the LocalServerChannel is closed while the connection is still
    queued (auto-read off, or server.close() right after connect()),
  • the accepted channel cannot be registered (for example the child group
    is shut down).

Related problems on the same path:

  • A connection that reaches the server after the server channel was
    closed is still accepted, and connect() succeeds against a closed
    server.
  • If the client closes while its connection is still queued,
    client.close() fails with IllegalStateException: channel not registered to an event loop. When the server accepts the connection
    later, the client is reopened (isOpen() is true again) and the
    accepted channel becomes active with a closed peer. The same
    IllegalStateException happens for server.close(); client.close();
    issued back to back while the connection is queued.

Modification:

  • LocalServerChannel: close queued connections on close, and close
    connections that arrive after the channel was closed.
  • The client no longer touches an accepted channel that has not
    completed the connection. When the accepted channel is registered, it
    moves the client from BOUND to CONNECTED with a CAS. The client's
    doClose() uses getAndSet(CLOSED), so exactly one side wins. If the
    client was closed first, the accepted channel closes itself on its own
    event loop.
  • LocalChannel.tryClose(false): if a connect is still pending, the
    accepted channel was closed before the connection was established. Fail
    the connect with ConnectException (as when nothing is bound to the
    address) and close the client. A client that closes itself still fails
    its connect with ClosedChannelException.
  • registered() reads peer once instead of checking it and reading it
    again.
  • Add five tests to LocalChannelTest.

Result:

connect() fails with ConnectException instead of hanging when the
server side goes away, a connection to an already closed server fails,
and client.close() succeeds in the orderings above.

The five new tests fail on 4.2 without the change and pass with it.
LocalChannelTest (32 tests), the transport module (407 tests), the
handler, handler-proxy, codec-haproxy and codec-http2 tests that use the
local transport, and checkstyle pass.

I also ran a stress test (not included): client, server and accepted
channels on three event loops, and the client closes 0 to 100 µs after
connect(). On 4.2, 20,000 iterations gave 499 close() failures with
IllegalStateException, 2,024 clients open again after close(), and
1,523 accepted channels left open and active. With this change, 100,000
iterations with -ea had none of these, and every accepted channel was
closed and deregistered.

Known limitations:

  • A client that closes while its connection is queued is still accepted
    later: the child handler's initChannel runs and channelRegistered
    fires, then the accepted channel is closed without channelActive.
  • Queued connections that the server closes (on close, or when they
    arrive after close) were never registered. They are closed with
    closeForcibly(), so their closeFuture does not complete. Only code
    that overrides newLocalChannel can see these channels.
  • Not changed by this PR: if the client closes after the accepted
    channel completed the connection but before the client's event loop runs
    the connect success task, the client's pipeline gets channelInactive
    without channelActive. 4.2 does the same.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@allthingssecurity @normanmaurer
LocalChannel: complete connect() when the accepted channel dies befor…
8c647b5
…e registration (#17656)

Motivation:

If the server side `LocalChannel` of a connection is closed before it is
registered, the client's `connect()` never completes (the local
transport has no connect timeout). This happens when:

- the `LocalServerChannel` is closed while the connection is still
queued (auto-read off, or `server.close()` right after `connect()`),
- the accepted channel cannot be registered (for example the child group
is shut down).

Related problems on the same path:

- A connection that reaches the server after the server channel was
closed is still accepted, and `connect()` succeeds against a closed
server.
- If the client closes while its connection is still queued,
`client.close()` fails with `IllegalStateException: channel not
registered to an event loop`. When the server accepts the connection
later, the client is reopened (`isOpen()` is true again) and the
accepted channel becomes active with a closed peer. The same
`IllegalStateException` happens for `server.close(); client.close();`
issued back to back while the connection is queued.

Modification:

- `LocalServerChannel`: close queued connections on close, and close
connections that arrive after the channel was closed.
- The client no longer touches an accepted channel that has not
completed the connection. When the accepted channel is registered, it
moves the client from `BOUND` to `CONNECTED` with a CAS. The client's
`doClose()` uses `getAndSet(CLOSED)`, so exactly one side wins. If the
client was closed first, the accepted channel closes itself on its own
event loop.
- `LocalChannel.tryClose(false)`: if a connect is still pending, the
accepted channel was closed before the connection was established. Fail
the connect with `ConnectException` (as when nothing is bound to the
address) and close the client. A client that closes itself still fails
its connect with `ClosedChannelException`.
- `registered()` reads `peer` once instead of checking it and reading it
again.
- Add five tests to `LocalChannelTest`.

Result:

`connect()` fails with `ConnectException` instead of hanging when the
server side goes away, a connection to an already closed server fails,
and `client.close()` succeeds in the orderings above.

The five new tests fail on 4.2 without the change and pass with it.
`LocalChannelTest` (32 tests), the transport module (407 tests), the
handler, handler-proxy, codec-haproxy and codec-http2 tests that use the
local transport, and checkstyle pass.

I also ran a stress test (not included): client, server and accepted
channels on three event loops, and the client closes 0 to 100 µs after
`connect()`. On 4.2, 20,000 iterations gave 499 `close()` failures with
`IllegalStateException`, 2,024 clients open again after `close()`, and
1,523 accepted channels left open and active. With this change, 100,000
iterations with `-ea` had none of these, and every accepted channel was
closed and deregistered.

Known limitations:

- A client that closes while its connection is queued is still accepted
later: the child handler's `initChannel` runs and `channelRegistered`
fires, then the accepted channel is closed without `channelActive`.
- Queued connections that the server closes (on close, or when they
arrive after close) were never registered. They are closed with
`closeForcibly()`, so their `closeFuture` does not complete. Only code
that overrides `newLocalChannel` can see these channels.
- Not changed by this PR: if the client closes after the accepted
channel completed the connection but before the client's event loop runs
the connect success task, the client's pipeline gets `channelInactive`
without `channelActive`. 4.2 does the same.
@normanmaurer normanmaurer mentioned this pull request Sep 29, 2026
LocalChannel: complete connect() when the accepted channel dies before registration #17656
Merged
@normanmaurer
Fixes
f11b037
@normanmaurer normanmaurer added this to the 4.1.139.Final milestone Sep 29, 2026
normanmaurer added 2 commits September 29, 2026 12:01
@normanmaurer
Fixes
f3082b7
@normanmaurer
Fixes
0c911f9
@normanmaurer normanmaurer changed the title LocalChannel: complete connect() when the accepted channel dies before registration (#17656) Port: LocalChannel: complete connect() when the accepted channel dies before registration (#17656) Sep 29, 2026
@testlens-app

This comment has been minimized.

Sign in to view
@normanmaurer
Fixes
510331b
@normanmaurer
normanmaurer merged commit 8345572 into 4.1 Sep 29, 2026
18 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer
normanmaurer deleted the localchannel41 branch September 29, 2026 19:05
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@normanmaurer @allthingssecurity @lltopk
Port: LocalChannel: complete connect() when the accepted channel dies…
449294b
… before registration (netty#17656) (netty#17666)

Motivation:

If the server side `LocalChannel` of a connection is closed before it is
registered, the client's `connect()` never completes (the local
transport has no connect timeout). This happens when:

- the `LocalServerChannel` is closed while the connection is still
queued (auto-read off, or `server.close()` right after `connect()`),
- the accepted channel cannot be registered (for example the child group
is shut down).

Related problems on the same path:

- A connection that reaches the server after the server channel was
closed is still accepted, and `connect()` succeeds against a closed
server.
- If the client closes while its connection is still queued,
`client.close()` fails with `IllegalStateException: channel not
registered to an event loop`. When the server accepts the connection
later, the client is reopened (`isOpen()` is true again) and the
accepted channel becomes active with a closed peer. The same
`IllegalStateException` happens for `server.close(); client.close();`
issued back to back while the connection is queued.

Modification:

- `LocalServerChannel`: close queued connections on close, and close
connections that arrive after the channel was closed.
- The client no longer touches an accepted channel that has not
completed the connection. When the accepted channel is registered, it
moves the client from `BOUND` to `CONNECTED` with a CAS. The client's
`doClose()` uses `getAndSet(CLOSED)`, so exactly one side wins. If the
client was closed first, the accepted channel closes itself on its own
event loop.
- `LocalChannel.tryClose(false)`: if a connect is still pending, the
accepted channel was closed before the connection was established. Fail
the connect with `ConnectException` (as when nothing is bound to the
address) and close the client. A client that closes itself still fails
its connect with `ClosedChannelException`.
- `registered()` reads `peer` once instead of checking it and reading it
again.
- Add five tests to `LocalChannelTest`.

Result:

`connect()` fails with `ConnectException` instead of hanging when the
server side goes away, a connection to an already closed server fails,
and `client.close()` succeeds in the orderings above.

The five new tests fail on 4.2 without the change and pass with it.
`LocalChannelTest` (32 tests), the transport module (407 tests), the
handler, handler-proxy, codec-haproxy and codec-http2 tests that use the
local transport, and checkstyle pass.

I also ran a stress test (not included): client, server and accepted
channels on three event loops, and the client closes 0 to 100 µs after
`connect()`. On 4.2, 20,000 iterations gave 499 `close()` failures with
`IllegalStateException`, 2,024 clients open again after `close()`, and
1,523 accepted channels left open and active. With this change, 100,000
iterations with `-ea` had none of these, and every accepted channel was
closed and deregistered.

Known limitations:

- A client that closes while its connection is queued is still accepted
later: the child handler's `initChannel` runs and `channelRegistered`
fires, then the accepted channel is closed without `channelActive`.
- Queued connections that the server closes (on close, or when they
arrive after close) were never registered. They are closed with
`closeForcibly()`, so their `closeFuture` does not complete. Only code
that overrides `newLocalChannel` can see these channels.
- Not changed by this PR: if the client closes after the accepted
channel completed the connection but before the client's event loop runs
the connect success task, the client's pipeline gets `channelInactive`
without `channelActive`. 4.2 does the same.

---------

Co-authored-by: allthingssecurity <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.1.139.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@normanmaurer @allthingssecurity
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.