Skip to content

Limit non-hibernatable WebSocket connections per Durable Object - #8169

Merged
rinkle-cf merged 1 commit into
mainfrom
rinkle/stor-5843-non-hibernatable-websocket-limit
Nov 4, 2026
Merged

rinkle-cf merged 1 commit into
mainfrom
rinkle/stor-5843-non-hibernatable-websocket-limit

Conversation

@rinkle-cf

Copy link
Copy Markdown
Contributor

A Durable Object instance could accept or open any number of non-hibernatable WebSockets, all on the one machine that hosts it. Each uses memory and connection-handling capacity that the machine shares with every other Durable Object on it. One instance holding tens of thousands, whose clients all reconnect at once when it restarts, can exhaust those resources and starve its neighbors. Hibernatable WebSockets are already limited to 32768 per instance.

Limit each instance to 32768 non-hibernatable WebSockets, counted separately from its hibernatable ones. A WebSocket counts from when it is accepted, by accept() or when a connection made with new WebSocket(url) opens, until it is released. Past the limit, accept() throws an Error. A connection made with new WebSocket(url) is closed once it opens, and the WebSocket reports the Error in its error event.

The DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH autogate turns off the limit.

This changes behavior for existing Workers without a compatibility flag: a Durable Object that holds more than 32768 non-hibernatable WebSockets starts seeing errors. A flag would leave existing Durable Objects unlimited, which defeats the purpose, so we accept the risk, with the kill switch as the fallback.

@rinkle-cf

Copy link
Copy Markdown
Contributor Author

Open questions:
(a) Should this be behind a compat flag given this is technically a breaking change (my guess is no as that would defeat the purpose)
(b) Should the limit be combined for hibernatable and non-hibernatable websockets. Keeping them separate as of now

@rinkle-cf
rinkle-cf marked this pull request as ready for review November 2, 2026 23:56
@rinkle-cf
rinkle-cf requested review from a team as code owners November 2, 2026 23:56
@rinkle-cf
rinkle-cf force-pushed the rinkle/stor-5843-non-hibernatable-websocket-limit branch from 4480c17 to 6d684b7 Compare November 2, 2026 23:58

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 6d684b73.

This review, of 4480c176

💬 Approved with comments — The limit enforcement and release logic is correct. One optional suggestion: add test coverage for the kill-switch autogate path.

Findings: 1 suggestion (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 suggestion

The limit logic is correct. Nothing tests the kill-switch path.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] No test covers the DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH path (src/workerd/api/tests/BUILD.bazel:1215)
View full review

The limit is checked before any state changes. Hibernatable WebSockets are excluded from the count. Each WebSocket gives its slot back from the Accepted destructor through a refcounted counter, so a WebSocket that outlives its actor still releases its slot safely.

One optional gap: the new test target turns off the @all-autogates variant, so the case where the kill switch removes the limit has no test.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change reduces a resource-exhaustion risk and doesn't weaken any security boundary. The counter is only touched on the isolate thread, so the non-atomic kj::Rc is safe. The kill switch is set per process and can't be set from untrusted input.


📖 AGENTS.md: ✅ LGTM

The change follows the repository's conventions. The compatibility decision is recorded in the commit message, as the backward-compatibility rules require.


⚡ Performance: ✅ LGTM

No performance concerns. The check and the counter updates are O(1).


📝 Documentation: ✅ LGTM

The comments on the limit constant and the autogate accurately describe the behavior.


Reviewed commit: 4480c176

Comment thread src/workerd/api/tests/BUILD.bazel Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of f51da278.

This review, of 6d684b73

💬 Approved with comments — The limit enforcement and release logic is correct. One optional suggestion: add test coverage for the kill-switch autogate path.

Findings: 1 suggestion

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 suggestion

The limit logic is correct. Nothing tests the kill-switch path.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] No test covers the DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH path (src/workerd/api/tests/BUILD.bazel:1215)
View full review

The limit is checked before any state changes. Hibernatable WebSockets are excluded from the count. Each WebSocket gives its slot back from the Accepted destructor through a refcounted counter, so a WebSocket that outlives its actor still releases its slot safely.

One optional gap: the new test target turns off the @all-autogates variant, so the case where the kill switch removes the limit has no test.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change reduces a resource-exhaustion risk and doesn't weaken any security boundary. The counter is only touched on the isolate thread, so the non-atomic kj::Rc is safe. The kill switch is set per process and can't be set from untrusted input.


📖 AGENTS.md: ✅ LGTM

The change follows the repository's conventions. The compatibility decision is recorded in the commit message, as the backward-compatibility rules require.


⚡ Performance: ✅ LGTM

No performance concerns. The check and the counter updates are O(1).


📝 Documentation: ✅ LGTM

The comments on the limit constant and the autogate accurately describe the behavior.


Reviewed commit: 6d684b73 — same changes as 4480c176, so this is that commit's review (no new review was run; line numbers are as of 4480c176)

@apeacock1991 apeacock1991 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.

LGTM - left a comment agreeing with the AI reviewer on one point

@apeacock1991

Copy link
Copy Markdown
Contributor

Open questions: (a) Should this be behind a compat flag given this is technically a breaking change (my guess is no as that would defeat the purpose) (b) Should the limit be combined for hibernatable and non-hibernatable websockets. Keeping them separate as of now

My 2c:

A) Agree with you, don't think it should be but might be worth checking if there are cases of actors with 32k+ connections for non-hibernated WebSockets just in case someone is somehow operating at say 40k fine and we break their app (seems unlikely)
B) I'd combine it personally, 32k connections for a single actor is already way too many, so allowing it to be circumvented by mixing the two seems not ideal

@rinkle-cf
rinkle-cf force-pushed the rinkle/stor-5843-non-hibernatable-websocket-limit branch from 6d684b7 to f51da27 Compare November 3, 2026 12:57

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 46d3b421.

This review, of f51da278

✅ Approved — No issues found. The earlier suggestion to test the kill-switch path is fixed by the new kill-switch test.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change reduces a resource-exhaustion risk and doesn't weaken any security boundary.


📖 AGENTS.md: ✅ LGTM

The change follows the repository's conventions. The commit message records the compatibility decision, as the backward-compatibility rules require.


📋 Code Quality: ✅ LGTM

The limit is checked before any state changes in both internalAccept() and initConnection(). The kill-switch test accepts more WebSockets than the limit allows with the autogate on, so it covers the case where the limit is turned off.


⚡ Performance: ✅ LGTM

The check and the counter updates are O(1).


📝 Documentation: ✅ LGTM

The comments on the limit constant, the counter and the autogate accurately describe the behavior.


Reviewed commit: f51da278

@rinkle-cf

Copy link
Copy Markdown
Contributor Author

Open questions: (a) Should this be behind a compat flag given this is technically a breaking change (my guess is no as that would defeat the purpose) (b) Should the limit be combined for hibernatable and non-hibernatable websockets. Keeping them separate as of now

My 2c:

A) Agree with you, don't think it should be but might be worth checking if there are cases of actors with 32k+ connections for non-hibernated WebSockets just in case someone is somehow operating at say 40k fine and we break their app (seems unlikely) B) I'd combine it personally, 32k connections for a single actor is already way too many, so allowing it to be circumvented by mixing the two seems not ideal

Added tests for killswitch case.
(a) https://grafana.cfdata.org/goto/qlSqa-qDR?orgId=1 --> this is rare, but does not count hibernated websockets that are not active.
(b) I agree, but we don't have data on this so I thought keeping this separate would be easy to push through: we also state the limit for hibernatable websockets separately in docs: https://developers.cloudflare.com/durable-objects/api/state/#acceptwebsocket

@apeacock1991

Copy link
Copy Markdown
Contributor

Open questions: (a) Should this be behind a compat flag given this is technically a breaking change (my guess is no as that would defeat the purpose) (b) Should the limit be combined for hibernatable and non-hibernatable websockets. Keeping them separate as of now

My 2c:
A) Agree with you, don't think it should be but might be worth checking if there are cases of actors with 32k+ connections for non-hibernated WebSockets just in case someone is somehow operating at say 40k fine and we break their app (seems unlikely) B) I'd combine it personally, 32k connections for a single actor is already way too many, so allowing it to be circumvented by mixing the two seems not ideal

Added tests for killswitch case. (a) https://grafana.cfdata.org/goto/qlSqa-qDR?orgId=1 --> this is rare, but does not count hibernated websockets that are not active. (b) I agree, but we don't have data on this so I thought keeping this separate would be easy to push through: we also state the limit for hibernatable websockets separately in docs: https://developers.cloudflare.com/durable-objects/api/state/#acceptwebsocket

How much tricker is combining the limit out of curiosity? mostly because it's easier to reason about if it's just "~32k websocket limit" vs it being split (but fine to merge as-is if it's tricky, or if we think split is better for some reason)

A Durable Object instance could accept or open any number of
non-hibernatable WebSockets, all on the one machine that hosts it.
Each uses memory and connection-handling capacity that the machine
shares with every other Durable Object on it. One instance holding
tens of thousands, whose clients all reconnect at once when it
restarts, can exhaust those resources and starve its neighbors.
Hibernatable WebSockets are already limited to 32768 per instance.

Limit each instance to 32768 non-hibernatable WebSockets, counted
separately from its hibernatable ones. A WebSocket counts from when
it is accepted, by accept() or when a connection made with
`new WebSocket(url)` opens, until it is released. Past the limit,
accept() throws an Error. A connection made with `new WebSocket(url)`
is closed once it opens, and the WebSocket reports the Error in its
error event. That check runs before the WebSocket takes the
connection, so that an error listener that throws can't keep it
open.

The count is refcounted, since a WebSocket can give back its slot
after its actor is destroyed.

The DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH autogate removes the
limit. Unlike most autogates, enabling it turns the change off, so
the limit applies wherever this is deployed. The limit's test has no
@all-autogates variant, and a separate test checks that the kill
switch lets an actor accept more WebSockets than the limit.

This changes behavior for existing Workers without a compatibility
flag: a Durable Object that holds more than 32768 non-hibernatable
WebSockets starts seeing errors. A flag would leave existing Durable
Objects unlimited, which defeats the purpose, so we accept the risk,
with the kill switch as the fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rinkle-cf
rinkle-cf force-pushed the rinkle/stor-5843-non-hibernatable-websocket-limit branch from f51da27 to 46d3b42 Compare November 4, 2026 11:09

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No issues found. The earlier suggestion to test the kill-switch path is fixed by the new kill-switch test.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change reduces a resource-exhaustion risk and doesn't weaken any security boundary.


📖 AGENTS.md: ✅ LGTM

The change follows the repository's conventions. The commit message records the compatibility decision, as the backward-compatibility rules require.


📋 Code Quality: ✅ LGTM

The limit is checked before any state changes in both internalAccept() and initConnection(). The kill-switch test accepts more WebSockets than the limit allows with the autogate on, so it covers the case where the limit is turned off.


⚡ Performance: ✅ LGTM

The check and the counter updates are O(1).


📝 Documentation: ✅ LGTM

The comments on the limit constant, the counter and the autogate accurately describe the behavior.


Reviewed commit: 46d3b421 — same changes as f51da278, so this is that commit's review (no new review was run; line numbers are as of f51da278)

@rinkle-cf

Copy link
Copy Markdown
Contributor Author

mostly because it's easier to reason about if it's just "~32k websocket limit" vs it being split (but fine to merge as-is if it's tricky, or if we think split is better for some reason)

Keeping separate for purpose of this MR per comment on https://jira.cfdata.org/browse/STOR-5843. We can keep combine the limits as a follow up change if desired.

@rinkle-cf
rinkle-cf enabled auto-merge November 4, 2026 11:33
@rinkle-cf
rinkle-cf merged commit 2a82c55 into main Nov 4, 2026
49 of 52 checks passed
@rinkle-cf
rinkle-cf deleted the rinkle/stor-5843-non-hibernatable-websocket-limit branch November 4, 2026 12:44
KastanDay pushed a commit to KastanDay/workerd that referenced this pull request Nov 4, 2026
Both timed out at the 15s "medium" timeout in a downstream debug build,
on a change that touched neither:

* websocket-accept-limit-test, added in cloudflare#8169, accepts the 32768
  WebSockets of the limit. Its test case passed after 13.5s, and
  starting and stopping workerd took it past 15s.
* tail-worker-test@all-autogates runs several other tests' workers to
  trace them. Recent debug builds took 7.2s to 12.5s of its 15s, and a
  slower executor timed it out before its first test case.

"large" gives them 60s. On bazelflare it also runs each on an executor
of its own (test_exec_properties).
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.

2 participants