Repository navigation
Add WebSocket.bufferedAmount - #8479
Conversation
|
The generated output of |
There was a problem hiding this comment.
Superseded by the review of
7a2accb1.
This review, of be9f0fb7
🛑 Significant concerns — 1 BLOCKING: the new pure virtual getBufferedAmount() is not overridden in the final class HibernatableWebSocketAdapter. That triggers Clang's default-on -Wabstract-final-class, which the -Werror CI configuration turns into a build failure. Otherwise the change is sound.
Findings: 1 blocking · 1 info (2 are inline comments on the diff)
Not opened by the correctness review: 3 changed files it was meant to read
types/generated-snapshot/experimental/index.tstypes/generated-snapshot/index.d.tstypes/generated-snapshot/index.ts
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #8479.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## BLOCKING (1)
### [Code Quality] `HibernatableWebSocketAdapter` (declared `final`) does not override the new pure virtual `getBufferedAmount()`. The resulting abstract final class triggers Clang's default-on `-Wabstract-final-class`, which the `-Werror` config (`.bazelrc:607`, enabled by `.bazelrc:644`) makes a build error.
- **File:** src/workerd/api/web-socket.h:432
## INFO (1)
### [Code Quality] `types/generated-snapshot/*` was edited by hand, which the repository rules forbid. If the snapshot check fails, regenerate the files with `just generate-types`.
- **File:** types/generated-snapshot/index.d.ts:3417
📋 Code Quality: 🛑 1 blocking · 1 info
The new pure virtual is not implemented in HibernatableWebSocketAdapter, which breaks the build under -Werror. The type snapshots were edited by hand.
Recommendation: 🔴 Serious Concerns
- [BLOCKING]
HibernatableWebSocketAdapter(declaredfinal) does not override the new pure virtualgetBufferedAmount(). The resulting abstract final class triggers Clang's default-on-Wabstract-final-class, which the-Werrorconfig (.bazelrc:607, enabled by.bazelrc:644) makes a build error. (src/workerd/api/web-socket.h:432) - [INFO]
types/generated-snapshot/*was edited by hand, which the repository rules forbid. If the snapshot check fails, regenerate the files withjust generate-types. (types/generated-snapshot/index.d.ts:3417)
View full review
1. HibernatableWebSocketAdapter does not override getBufferedAmount() (BLOCKING)
web-socket.h:432 adds virtual double getBufferedAmount() = 0; to WebSocketAdapter, but only LegacyWebSocketAdapter overrides it.
HibernatableWebSocketAdapter(src/workerd/api/hibernatable-adapter.h:18) is declaredfinaland lists an override for every other adapter method, but not this one.hibernatable-adapter.c++is part of theapilibrary (src/workerd/api/BUILD.bazel:30).- An abstract class marked
finaltriggers Clang's-Wabstract-final-classwarning, which is on by default. - The
werrorconfig adds-Werror(.bazelrc:607). Thebazelflareconfig enables it (.bazelrc:644) and notes "Warnings are errors, as in CI" (.bazelrc:629). So the header fails to compile in CI and on any-Werrorbuild. - Even without
-Werror, the class cannot be instantiated once the hibernatable-adapter path is enabled.
Fix: add double getBufferedAmount() override; to the class, and define it in hibernatable-adapter.c++ with unimplemented();, as getReadyState() is.
2. Hand-edited type snapshots (INFO)
The repository rules say not to edit types/generated-snapshot/ directly. The description discloses this and relies on the snapshot check to confirm it. The added declaration matches how readyState is declared. If that check fails, regenerate the files with just generate-types.
✅ All other checks passed
🔒 Security: ✅ LGTM
bufferedAmount is a read-only counter of the caller's own payload bytes. It adds no new trust boundary, injection surface or data exposure.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md changes are needed.
⚡ Performance: ✅ LGTM
Keeping the count costs one integer add per send() and one subtract per completed write.
📝 Documentation: ✅ LGTM
The header comment and the type declaration describe the behaviour accurately.
Reviewed commit: be9f0fb7
be9f0fb to
7a2accb
Compare
There was a problem hiding this comment.
Superseded by the review of
8bcc25df.
This review, of 7a2accb1
💬 Approved with comments — The earlier BLOCKING item (missing override in the final HibernatableWebSocketAdapter) is fixed. The only remaining note is an INFO about the hand-edited type snapshots.
Findings: 1 info
Not opened by the correctness review: 3 changed files it was meant to read
types/generated-snapshot/experimental/index.tstypes/generated-snapshot/index.d.tstypes/generated-snapshot/index.ts
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
The earlier missing-override build break is fixed. One INFO remains about hand-edited type snapshots.
Recommendation: 💬 Optional Nits
- [INFO] The
types/generated-snapshot/files were edited by hand, which the repository rules forbid. Regenerate them withjust generate-typesif the snapshot check fails. (types/generated-snapshot/index.d.ts:3417)
View full review
- Override added.
HibernatableWebSocketAdapternow declaresdouble getBufferedAmount() override;and defines it withunimplemented(), so it is no longer an abstractfinalclass. - Counting.
send()adds the payload size on every path, including the ones that discard the message on a dead connection. The pump subtracts the size only after eachws->send()completes. The value is kept as auint64_tand returned as adouble. - Types (INFO).
types/generated-snapshot/*was edited by hand, which the repository rules forbid. If the snapshot check fails, regenerate the files withjust generate-types.
✅ All other checks passed
🔒 Security: ✅ LGTM
bufferedAmount is a read-only count of the caller's own payload bytes. It adds no new trust boundary or data exposure.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md changes are needed.
⚡ Performance: ✅ LGTM
Each message costs one integer add in send() and one subtract after its write completes, so the overhead is negligible.
📝 Documentation: ✅ LGTM
The header comment and the type declaration describe the behaviour accurately.
Reviewed commit: 7a2accb1
There was a problem hiding this comment.
Superseded by the review of
11a4b7d5.
This review, of 8bcc25df
✅ Approved — No new issues: the WPT config change is correct and the bufferedAmount counting is still sound. One earlier note about the hand-edited type snapshots remains open.
Not opened by the correctness review: 2 changed files it was meant to read
types/generated-snapshot/experimental/index.tstypes/generated-snapshot/index.ts
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
bufferedAmount is a read-only count of the caller's own payload bytes. It adds no new trust boundary and exposes no new data.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates are needed.
📋 Code Quality: ✅ LGTM
- WPT config:
removeUseCaptureis defined insrc/wpt/websockets-test.ts, so every re-enabledSend-*entry resolves. The three tests still off (Send-binary-blob,Send-unpaired-surrogates,Send-data.worker.js) each record a reason. - Counting:
send()adds the payload size on both discard paths and after queuing. The pump subtracts it only after each write completes.
⚡ Performance: ✅ LGTM
Each message costs one integer add in send() and one subtract after its write completes. That overhead is negligible.
📝 Documentation: ✅ LGTM
The header comment and the type declaration describe the behaviour accurately.
Reviewed commit: 8bcc25df
Since #8306, messages a WebSocket has queued but not yet written count toward the isolate's memory limit, but an application has no way to tell how much it has queued. A Worker that streams faster than its peer reads can only guess a send rate, and if it guesses wrong, the isolate exceeds its limit and every connection on it drops (#8477). bufferedAmount is the WHATWG WebSockets standard's answer: the number of payload bytes passed to send() that haven't been written to the connection yet. Text counts as its UTF-8 length; Close frames and framing overhead don't count. As the standard requires, data that send() discards because the connection has failed is counted and never subtracted, so the value keeps climbing if an application keeps sending to a dead connection. The count lives with the outgoing queue in the IoContext-owned native state, which the pump already updates without the isolate lock. It is lowered after each write completes, so it also covers the message the pump is currently writing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8bcc25d to
11a4b7d
Compare
There was a problem hiding this comment.
Superseded by the review of
8afc5934.
This review, of 11a4b7d5
✅ Approved — No new issues: the updated http-test expectation matches the property's registration order, and the bufferedAmount counting is still sound. One earlier note about the hand-edited type snapshots is still open.
Not opened by the correctness review: 2 changed files it was meant to read
types/generated-snapshot/experimental/index.tstypes/generated-snapshot/index.ts
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
bufferedAmount is a read-only count of the caller's own payload bytes. It adds no new trust boundary and exposes no new data. The only change since the last review is a test expectation.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates are needed.
📋 Code Quality: ✅ LGTM
http-test.js: The newbufferedAmount: 0sits right afterreadyState, which matches the registration order inweb-socket.hin both property branches.- Counting:
send()adds the payload size on both discard paths and after queuing. The pump subtracts it only after each write completes, and it reads the size from the queued message, which stays alive until the write finishes. - Hibernatable adapter:
getBufferedAmount()isunimplemented()there, likegetReadyState()next to it. - WPT config: Each re-enabled entry resolves, and each of the three tests still off records why.
⚡ Performance: ✅ LGTM
Each message costs one integer add in send() and one subtract after its write completes. That overhead is negligible.
📝 Documentation: ✅ LGTM
The header comment and the type declaration describe the behaviour accurately. One earlier note is still open: the files under types/generated-snapshot/ were edited by hand rather than regenerated. Those files have not changed since then.
Reviewed commit: 11a4b7d5
These tests were disabled because they check bufferedAmount, which workerd didn't have until the previous commit. 17 of the 20 now pass and are enabled. The idlharness checks for bufferedAmount pass too, so they come off its expected failures. The other three fail for reasons unrelated to bufferedAmount, and the config now says why: * Send-binary-blob.any.js stays disabled: send() doesn't accept a Blob, so no message comes back and the test hangs. * Send-unpaired-surrogates.any.js is an expected failure: send() doesn't replace an unpaired surrogate with U+FFFD, so the echo server never returns the message. * Send-data.worker.js is omitted: it loads its helpers with importScripts(), and Send-data.any.js covers the same case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11a4b7d to
8afc593
Compare
There was a problem hiding this comment.
✅ Approved — No findings. The only change since the last review is the WPT test config, which is correct, and the bufferedAmount counting is still sound.
Not opened by the correctness review: 2 changed files it was meant to read
types/generated-snapshot/experimental/index.tstypes/generated-snapshot/index.ts
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
bufferedAmount is a read-only count of the caller's own payload bytes. It adds no new trust boundary.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates are needed.
📋 Code Quality: ✅ LGTM
send() adds the payload size on both discard paths and after queuing. The pump subtracts it only after each write completes. The hibernatable adapter override is present. The WPT config entries resolve, and each disabled test records why.
⚡ Performance: ✅ LGTM
Each message costs one integer add and one subtract, which is negligible.
📝 Documentation: ✅ LGTM
The header comment and the type declarations accurately describe the behaviour. The .ts and .d.ts snapshot files match each other.
Reviewed commit: 8afc5934
Since #8306, messages a WebSocket has queued but not yet written count toward the isolate's memory limit. But a Worker has no way to tell how much it has queued, so one that streams faster than its peer reads can only guess a send rate. If it guesses wrong, the isolate exceeds its limit and every connection on it drops. #8477 is a speed test that hit this.
This adds the standard
WebSocket.bufferedAmount: the number of payload bytes passed tosend()that haven't been written to the connection yet. As in browsers, there's no drain event; applications poll it.send()silently discards because the connection has failed is added and never subtracted, so the value keeps climbing if an application keeps sending to a dead connection.The count lives in the IoContext-owned
Nativestate next to the outgoing queue, which the pump already updates without the isolate lock.No compatibility flag. The property was
undefinedbefore, so code that checksws.bufferedAmount > limitwas never applying backpressure and now will. The one thing that could break is a strict-mode assignment tows.bufferedAmount, which now throws because the property is read-only. That seems unlikely enough not to need a flag, but say so if you disagree.Types.
bufferedAmountis added totypes/generated-snapshotby hand, with the MDN comment from TypeScript'slib.dom.d.tsthat the generator uses. I couldn't runjust generate-typesin this setup, so the snapshot check job will confirm whether it matches the generated output.Testing
websocket-buffered-amount-testcovers: the starting value of 0, UTF-8 and binary counting, the count holding while the peer isn't reading and draining once it reads, andclose()not being counted. Every variant passed with--runs_per_test=5. The existing WebSocket wd-tests still pass, and clang-tidy is clean on//src/workerd/io.WPT
The second commit re-enables the WPT
Send-*tests that were disabled for lack ofbufferedAmount. 17 of the 20 now pass, as do idlharness's twobufferedAmountchecks. The config now records why the other three are still off:Send-binary-blob.any.jsstays disabled, becausesend()doesn't accept aBlob.Send-unpaired-surrogates.any.jsis an expected failure, becausesend()doesn't replace an unpaired surrogate with U+FFFD.Send-data.worker.jsis omitted, because it relies onimportScripts().The suite passed with
--runs_per_test=5.🤖 Generated with Claude Code