Skip to content

WebSocket client handshaker to support "force close" after timeout - #8896

Merged
normanmaurer merged 1 commit into
netty:4.1from
kachayev:ft-websocket-force-close
Apr 10, 2019
Merged

normanmaurer merged 1 commit into
netty:4.1from
kachayev:ft-websocket-force-close

Conversation

@kachayev

@kachayev kachayev commented Feb 27, 2019 •

Copy link
Copy Markdown
Contributor

Motivation:

RFC 6455 defines that, generally, a WebSocket client should not close a TCP
connection as far as a server is the one who's responsible for doing that.
In practice tho', it's not always possible to control the server. Server's
misbehavior may lead to connections being leaked (if the server does not
comply with the RFC).

RFC 6455 #7.1.1 says

In abnormal cases (such as not having received a TCP Close from the server
after a reasonable amount of time) a client MAY initiate the TCP Close.

Modifications:

  • WebSocket client handshaker additional param forceCloseAfterMillis

  • Switched off by default to keep backward compatibility

Result:

WebSocket client handshaker to comply with RFC. Fixes #8883.


Open questions/comments:

  • I've implemented this as a WebSocketClientHandshaker functionality, not in handlers, as far as it's responsible for close.

  • Nothing prevents you to call handshaker.close twice, in such a case only first timeout would be applied.

  • Do we need to keep this feature switched OFF by default to ensure backward compatibility? I mean... it's hard to justify current implementation and I think 10 seconds default timeout should be enough to cover potential drawbacks. @normanmaurer WDYT?

  • Testing is kind of clumsy... It's impossible to detect if the channel was closed because of timeout or because of the server or... whatever. Maybe I'm missing any better approach to test this?

@netty-bot

Copy link
Copy Markdown

Can one of the admins verify this patch?

@normanmaurer

Copy link
Copy Markdown
Member

@kachayev its RFC6455 ;)

@kachayev

Copy link
Copy Markdown
Contributor Author

@kachayev its RFC6455 ;)

@normanmaurer Copy-paste error ;)

@kachayev
kachayev force-pushed the ft-websocket-force-close branch 2 times, most recently from 326620a to 1e91a34 Compare February 28, 2019 13:21
@kachayev

Copy link
Copy Markdown
Contributor Author

@normanmaurer Updated. I still think if need to cover this use case:

handshaker.setForceCloseTimeoutMillis(1_000).close(ch, new CloseWebsocketFrame());

It might be useful if the timeout depends on what happened during the communication (then I have no chance to setup it properly before the upgrade)... I just don't know how often you might need this in practice.

@normanmaurer

Copy link
Copy Markdown
Member

@kachayev another alternative would be to allow to overload the method with a timeout.

@kachayev
kachayev force-pushed the ft-websocket-force-close branch from 1e91a34 to c150a74 Compare February 28, 2019 14:14
@kachayev

Copy link
Copy Markdown
Contributor Author

@normanmaurer Added setter to cover more use cases. Changed test to use an internal flag that indicated that the timeout was actually fired.

@kachayev
kachayev force-pushed the ft-websocket-force-close branch 2 times, most recently from abac67d to cd8b306 Compare February 28, 2019 20:17
@kachayev
kachayev force-pushed the ft-websocket-force-close branch from cd8b306 to be40c4e Compare March 1, 2019 08:27
@kachayev
kachayev force-pushed the ft-websocket-force-close branch from be40c4e to f88cc9b Compare March 1, 2019 21:49
@kachayev
kachayev force-pushed the ft-websocket-force-close branch from f88cc9b to 481d380 Compare March 5, 2019 09:30
@normanmaurer

Copy link
Copy Markdown
Member

/cc @vietj

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@kachayev
kachayev force-pushed the ft-websocket-force-close branch from 481d380 to a3407f5 Compare March 14, 2019 21:41
@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer
normanmaurer requested a review from trustin March 26, 2019 08:20
@normanmaurer

Copy link
Copy Markdown
Member

@trustin can you have a look as well please ?

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@trustin

trustin commented Apr 1, 2019

Copy link
Copy Markdown
Member

Switched off by default to keep backward compatibility

Probably better enabling by default with reasonable default? Leaky connection for backward compatibility doesn't sound right to me.

@normanmaurer

Copy link
Copy Markdown
Member

@trustin sounds fine to me... maybe 3 seconds (which is also the default we use for SSL close notify) is ok ? Or do we want to use 10 seconds as we use for handshake timeout in SSL by default ?

@trustin trustin left a comment

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.

Mostly nits.

@trustin

trustin commented Apr 1, 2019

Copy link
Copy Markdown
Member

Not a WebSocket expert. 😅 Leaving it up to @kachayev 😆

@normanmaurer

Copy link
Copy Markdown
Member

@slandelle can you have a look as well ?

@kachayev

kachayev commented Apr 1, 2019

Copy link
Copy Markdown
Contributor Author

@trustin

enabling by default

Absolutely, 100% for it. I will put 10 seconds to stay consistent with SSL handshake timeouts.

@slandelle slandelle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2 small nits. LGTM otherwise!

Motivation:

RFC 6455 defines that, generally, a WebSocket client should not close a TCP
connection as far as a server is the one who's responsible for doing that.
In practice tho', it's not always possible to control the server. Server's
misbehavior may lead to connections being leaked (if the server does not
comply with the RFC).

RFC 6455 netty#7.1.1 says

> In abnormal cases (such as not having received a TCP Close from the server
after a reasonable amount of time) a client MAY initiate the TCP Close.

Modifications:

* WebSocket client handshaker additional param `forceCloseAfterMillis`

* Switched of by default to keep backward compatibility

Result:

WebSocket client handshaker to comply with RFC. Fixes netty#8883.
@kachayev
kachayev force-pushed the ft-websocket-force-close branch from d770d0b to d6795c4 Compare April 2, 2019 05:30
@kachayev

kachayev commented Apr 2, 2019

Copy link
Copy Markdown
Contributor Author

@trustin @slandelle Pushed changes, thanks for the review!

@normanmaurer

Copy link
Copy Markdown
Member

@trustin PTAL again

@normanmaurer

Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer normanmaurer self-assigned this Apr 10, 2019
@normanmaurer
normanmaurer merged commit ee351ef into netty:4.1 Apr 10, 2019
@normanmaurer

Copy link
Copy Markdown
Member

@kachayev thanks a lot!

normanmaurer pushed a commit that referenced this pull request Apr 10, 2019
…8896)

Motivation:

RFC 6455 defines that, generally, a WebSocket client should not close a TCP
connection as far as a server is the one who's responsible for doing that.
In practice tho', it's not always possible to control the server. Server's
misbehavior may lead to connections being leaked (if the server does not
comply with the RFC).

RFC 6455 #7.1.1 says

> In abnormal cases (such as not having received a TCP Close from the server
after a reasonable amount of time) a client MAY initiate the TCP Close.

Modifications:

* WebSocket client handshaker additional param `forceCloseAfterMillis`

* Use 10 seconds as default

Result:

WebSocket client handshaker to comply with RFC. Fixes #8883.
alfred-yeung added a commit to datastax-archive/riptano--netty that referenced this pull request May 28, 2022
…etty#8896)

Motivation:
RFC 6455 defines that, generally, a WebSocket client should not close a TCP
connection as far as a server is the one who's responsible for doing that.
In practice tho', it's not always possible to control the server. Server's
misbehavior may lead to connections being leaked (if the server does not
comply with the RFC).

RFC 6455 #7.1.1 says

In abnormal cases (such as not having received a TCP Close from the server
after a reasonable amount of time) a client MAY initiate the TCP Close.

Modifications:
WebSocket client handshaker additional param forceCloseAfterMillis

Switched off by default to keep backward compatibility

Result:
WebSocket client handshaker to comply with RFC. Fixes (netty#8883).
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
WebSocket close waits currently use a fixed three-second setting and a
periodic tick, even while connections are not closing. Applications need
to tune close-handshake waits without changing other timeout settings.

Modification:
Expose the positive client/server close timeout (maximum one hour) via
config and the Scala and Java settings APIs. Preserve three seconds by
default and for legacy config snippets. Arm a one-shot timer only while
waiting for peer Close or transport closure; keep the original budget
for a client transitioning between those phases. Keep the legacy stack
entry points and default getter, route settings-aware callers through
the configured value, use the shared Java duration converter, and
document the client behavior.

Result:
Both endpoints honor the configured wait with no periodic work on open
connections. Existing idle timeout semantics are unchanged. Timer
delivery remains subject to scheduler granularity.

Tests:
- Strict Scala 2.13.18 compilation of the six affected production sources
  and three affected specifications against cached dependencies: passed.
- Eight settings cases and two directional client/server close cases:
  passed; both directional cases fail against original-main wiring.
- Targeted MiMa against the exact baseline: zero findings.
- Cached Scalafmt and git diff --check: passed.
- Full repository CI and cross-Scala build not run locally.

References:
Refs apache#1486 and Netty netty/netty#8896, netty/netty#9967 (independent Pekko implementation).
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Allow applications to tune the current fixed three-second close wait
without changing the idle timeout or adding work to open connections.

Modification:
Configure the client and server close wait with Scala and Java settings
and a finite positive range up to one hour. Arm one timer while closing;
reuse the remaining time when an active client receives the peer Close
but still waits for transport closure. Route standard stack calls
through settings and remove the dead internal tick and default getter.
Keep the legacy messageAPI parameter with an explicit unused annotation.
Document the default, bounds and scheduler granularity.

Result:
Normal traffic has no periodic close check, and a client's two closing
phases share one configured budget. The default remains three seconds.

Tests:
- Strict Scala 2.13.18 production/test compile against cached modules.
- Eight settings cases and normal client/server configurable close
  cases passed in the first review round; the old wiring fails both.
- Current focused client configurable close and shared-budget cases
  passed; the shared-budget case fails when the timer restarts.
- Targeted MiMa with a freshly compiled source baseline reports zero.
  Two obsolete implementation-only symbols are intentionally removed.
- Scalafmt and git diff --check passed; full CI not run locally.

References:
Refs apache#1486, netty/netty#8896 and netty/netty#9967 (independent implementation).
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Allow applications to tune the fixed three-second close wait without
changing the idle timeout or adding work to open connections.

Modification:
Configure the client and server close wait with Scala and Java settings
and a finite positive range up to one hour. Arm one timer while closing
and reuse its remaining time after the peer Close reply. Route stack
calls through settings and remove the obsolete internal stack default
getter. Keep the legacy messageAPI overload and retain Tick only as an
ABI symbol from 2.0.0; do not schedule or emit it. Qualify the Java
getter's Scala value explicitly for cross-version compilation.

Result:
Open connections no longer receive periodic close checks; the client's
two closing phases share one budget. The default remains three seconds.

Tests:
- Changed sources compiled in Scala 2.13.18; eight settings and two
  focused client cases passed against the rebuilt classes.
- The three affected settings sources compiled on Scala 3.3.9 and 3.9.0
  against published dependencies; the unqualified getter reproduced E049.
- Targeted MiMa with the 2.0.0 artifact and rebuilt classes reproduced
  the missing Tick symbol, then reported zero problems with the fix.
- Scalafmt and git diff --check passed. The cloud JDK lacks the Java 17
  release symbols required by the unchanged aggregate build; full CI
  is not claimed locally.

References:
Refs apache#1486, netty/netty#8896 and netty/netty#9967 (independent implementation).
pjfanning pushed a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 18, 2026
Motivation:
Allow applications to tune the fixed three-second close wait without
changing the idle timeout or adding work to open connections.

Modification:
Configure the client and server close wait with Scala and Java settings
and a finite positive range up to one hour. Arm one timer while closing
and reuse its remaining time after the peer Close reply. Route stack
calls through settings and remove the obsolete internal stack default
getter. Keep the legacy messageAPI overload and retain Tick only as an
ABI symbol from 2.0.0; do not schedule or emit it. Qualify the Java
getter's Scala value explicitly for cross-version compilation.

Result:
Open connections no longer receive periodic close checks; the client's
two closing phases share one budget. The default remains three seconds.

Tests:
- Changed sources compiled in Scala 2.13.18; eight settings and two
  focused client cases passed against the rebuilt classes.
- The three affected settings sources compiled on Scala 3.3.9 and 3.9.0
  against published dependencies; the unqualified getter reproduced E049.
- Targeted MiMa with the 2.0.0 artifact and rebuilt classes reproduced
  the missing Tick symbol, then reported zero problems with the fix.
- Scalafmt and git diff --check passed. The cloud JDK lacks the Java 17
  release symbols required by the unchanged aggregate build; full CI
  is not claimed locally.

References:
Refs apache#1486, netty/netty#8896 and netty/netty#9967 (independent implementation).
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.

Websocket client closing handshake to support "force close" after given timetout

5 participants