Skip to content

websockets: buffered amount not tracked in ArrayBuffer for typed arrays - fixes #5715 - #5716

Merged
szkiba merged 11 commits into
grafana:masterfrom
prakharbirla-ng:master
Apr 24, 2026
Merged

szkiba merged 11 commits into
grafana:masterfrom
prakharbirla-ng:master

Conversation

@prakharbirla-ng

@prakharbirla-ng prakharbirla-ng commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor

What?

When sending TypedArrays, bufferedAmount was not incremented.
Fixes the bug introduced in #5358

Why?

Buffered amount should be tracked when data is added to the queue so that it does not become negative.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

Closes #5715

@prakharbirla-ng
prakharbirla-ng requested a review from a team as a code owner March 7, 2026 13:48
@prakharbirla-ng
prakharbirla-ng requested review from ankur22 and szkiba and removed request for a team March 7, 2026 13:48
@cla-assistant

cla-assistant Bot commented Mar 7, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Thanks for the fix. I left some comments around what I think would improve the testing of the fix.

ws.send(sent.subarray(0, 1))
ws.send(sent.subarray(0, 10))
if (ws.bufferedAmount != 10) {
throw "Expected 10 bufferedAmount got "+ ws.bufferedAmount

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.

Is there a reason why you're throwing a string instead of a Error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

require.Len(t, logs, 0)
}

func TestBinaryType_ArrayBuffer_issue_5226(t *testing.T) {

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.

Is there a better place that modifying this test? Maybe a new test would be better?

Also need a test for DataView.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed, augmented the existing test TestArrayBufferViewSupport to have a separate pass for testArrayBufferViewBufferedAmount. I reused the original runner as it maintains a list of TypedArrays supported by Sobek.

Regarding DataView, I found that it isn't supported by this package. Trying to send a DataView invokes this part of the code resulting in an error:

	case map[string]any:
		rt := w.vu.Runtime()
		obj := msg.ToObject(rt)
		if !isBlob(obj, w.blobConstructor) {
			common.Throw(rt, fmt.Errorf("unsupported send type %T", o))
		}

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.

Ok, can you update the issue and PR title/description by removing DataView? Thanks!

@prakharbirla-ng prakharbirla-ng changed the title websockets: buffered amount not tracked in ArrayBuffer for typed arrays or DataView - fixes #5715 websockets: buffered amount not tracked in ArrayBuffer for typed arrays - fixes #5715 Mar 13, 2026
@ankur22 ankur22 added this to the v2.0.0 milestone Apr 7, 2026
}
ws.onmessage = async (e) => {
const received = new %[1]s(await e.data.arrayBuffer());
if (ws.bufferedAmount != 0) {

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.

received is assigned but never used, can it be removed?

@szkiba
szkiba temporarily deployed to azure-trusted-signing April 22, 2026 14:48 — with GitHub Actions Inactive
@szkiba
szkiba temporarily deployed to azure-trusted-signing April 22, 2026 14:50 — with GitHub Actions Inactive
@szkiba
szkiba temporarily deployed to azure-trusted-signing April 22, 2026 15:29 — with GitHub Actions Inactive
@szkiba
szkiba temporarily deployed to azure-trusted-signing April 22, 2026 15:31 — with GitHub Actions Inactive
@szkiba
szkiba temporarily deployed to azure-trusted-signing April 22, 2026 17:56 — with GitHub Actions Inactive
@szkiba
szkiba temporarily deployed to azure-trusted-signing April 22, 2026 17:58 — with GitHub Actions Inactive
@szkiba
szkiba merged commit b693d6f into grafana:master Apr 24, 2026
88 of 141 checks passed
mstoykov added a commit that referenced this pull request Apr 25, 2026
- #5814, #5716 as bug fixes
- #5872, #5871, #5751, #5841, #5860 as maintenance
- #5861 combined into codeql-action entry (updated to v4.35.2)
- #5862 combined into k6-cloud-openapi-client-go entry

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
mstoykov added a commit that referenced this pull request Apr 25, 2026
- #5814, #5716 as bug fixes
- #5872, #5871, #5751, #5841, #5860 as maintenance
- #5861 combined into codeql-action entry (updated to v4.35.2)
- #5862 combined into k6-cloud-openapi-client-go entry

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
inancgumus pushed a commit that referenced this pull request May 15, 2026
websockets: buffered amount not tracked in ArrayBuffer for typed arrays - fixes #5715

Co-authored-by: Iván Szkiba <szkiba@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
azure-trusted-signing — f520efb4 Deployed Apr 22, 2026 by szkiba via sign-packages #9098
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 bufferedAmount isn't tracked for TypedArrays

3 participants