Skip to content

Commit af7015b

Browse files
Fix maxLifetime: 240 causing immediate WebSocket disconnection (#1231)
* Initial plan * Fix maxLifetime: 240 causing immediate WebSocket disconnection Cap maxLifetime at 239 in AppWrapper.h to prevent uSockets modulo wraparound bug (240 % 240 == current timestamp => instant timeout). Also clamp to non-negative values. Update TypeScript docs to document valid range as 0 | 1-239. Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
1 parent 087ebf8 commit af7015b

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

‎docs/index.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export interface WebSocketBehavior<UserData> {
270270
maxPayloadLength?: number;
271271
/** Whether or not we should automatically close the socket when a message is dropped due to backpressure. Defaults to false. */
272272
closeOnBackpressureLimit?: boolean;
273-
/** Maximum number of minutes a WebSocket may be connected before being closed by the server. 0 disables the feature. */
273+
/** Maximum number of minutes a WebSocket may be connected before being closed by the server. 0 disables the feature. Valid values are 0 and 1-239. */
274274
maxLifetime?: number;
275275
/** Maximum amount of seconds that may pass without sending or getting a message. Connection is closed if this timeout passes. Resolution (granularity) for timeouts are typically 4 seconds, rounded to closest.
276276
* Disable by using 0. Defaults to 120.

‎src/AppWrapper.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
7171
/* maxLifetime or default */
7272
MaybeLocal<Value> maybeMaxLifetime = behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "maxLifetime", NewStringType::kNormal).ToLocalChecked());
7373
if (!maybeMaxLifetime.IsEmpty() && !maybeMaxLifetime.ToLocalChecked()->IsUndefined()) {
74-
behavior.maxLifetime = maybeMaxLifetime.ToLocalChecked()->Int32Value(isolate->GetCurrentContext()).ToChecked();
74+
/* Cap at 239 to avoid modulo wraparound in uSockets (240 % 240 == 0 == current timestamp, causing immediate timeout), and ensure non-negative */
75+
behavior.maxLifetime = std::max(0, std::min<int>(maybeMaxLifetime.ToLocalChecked()->Int32Value(isolate->GetCurrentContext()).ToChecked(), 239));
7576
}
7677

7778
/* closeOnBackpressureLimit or default */

0 commit comments

Comments
 (0)