Skip to content

docs(wsjson): document that Read closes the connection on bad JSON - #590

Open
mraj602 wants to merge 1 commit into
coder:masterfrom
mraj602:fix/wsjson-read-close-doc
Open

mraj602 wants to merge 1 commit into
coder:masterfrom
mraj602:fix/wsjson-read-close-doc

Conversation

@mraj602

@mraj602 mraj602 commented Oct 28, 2026

Copy link
Copy Markdown

wsjson.Read closes the connection with StatusInvalidFramePayloadData (reason "failed to unmarshal JSON") when the message can't be unmarshalled into v, but its doc comment doesn't say so. Callers who treat the returned error as per-message and continue in a read loop end up spinning on a closed connection, as reported in #399.

This adds a paragraph to the Read doc comment that states the behaviour. For callers who want to keep the connection open, it points to c.Read followed by json.Unmarshal.

What this PR does not do: change the behaviour. The linked discussion asked whether Read should close at all. This PR only documents what it does today, as the issue asks.

On js/wasm the close currently fails, because browsers reject 1007 (#575), so Read only returns the error there. #576 would send 1000 instead. I kept the comment to the native behaviour rather than describing that gap. Happy to add a js/wasm note if you prefer.

Checked locally with a throwaway test (not included): over a wstest.Pipe, sending {"a":1} junk makes wsjson.Read return an error that wraps *json.SyntaxError. The peer then gets received close frame: status = StatusInvalidFramePayloadData and reason = "failed to unmarshal JSON".

Fixes #450

Read closes the connection with StatusInvalidFramePayloadData when the
message cannot be unmarshalled, but its doc comment did not say so.
Callers that retry in a loop then spin on a closed connection. State the
behavior and point to c.Read plus json.Unmarshal for callers that want to
keep the connection open.

This branch has not been deployed

No deployments
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.

Document wsjson.Read closes the connection with the appropriate close status and error message if bad JSON is read

1 participant