Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: protocol parsing errors with ws compression and pongs #1790

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

rsafonseca
Copy link
Contributor

Fixes: #1783

This PR addresses a problem that happens when using WS compression and receiving PONGs from the server along with other compressed messages.

The byte slices added to websocketReader.pending are memory references to the original buffer, so when the buffer is re-written in websocketReader.drainPending([]byte) the value for the pending PONG message takes a value that is part of the re-written bytes in the buffer.

This only happens with compression, because the size of the messages in the buffer changes, and only happens when receiving PONG messages, because the value for the regular messages is a new []byte generated after decompression, and not a memory reference to a sub-slice of the original buffer

@coveralls
Copy link

coveralls commented Jan 29, 2025

Coverage Status

coverage: 84.882% (-0.03%) from 84.911%
when pulling 66d84c6 on rsafonseca:fix_pong_bug
into f7dfee9 on nats-io:main.

@piotrpio
Copy link
Collaborator

Amazing find @rsafonseca, thank you. My only concern is the added allocations - maybe we could only copy the slice if it's necessary? What I mean is - if we already create a new byte slice when decompressing, this one does not have to be cloned again when appending to r.pending. As you said, we're only really concerned with frames coming from the server which are not compressed (when using compression). WDYT?

@rsafonseca
Copy link
Contributor Author

rsafonseca commented Jan 29, 2025

Yeah, i considered that, but when i ran the benchmark i didn't see any noticeable difference and just wanted to keep it simple for getting the issue fixed. The memory allocation should be quickly released though as soon as drainPendingMessages runs.
Right now there's no flag in the websocketReader to assert whether compression was negotiated with the server or not, only on the websocketWriter, the reader only checks this on a per-message basis, though that could be added for the purpose of this check, or we could find another way to implement it if you feel strongly about it

@rsafonseca
Copy link
Contributor Author

I've pushed a simple change for this, WDYT @piotrpio ?

Copy link
Collaborator

@piotrpio piotrpio left a comment

Choose a reason for hiding this comment

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

Looks nice and simple enough, I see no reason not to avoid those allocations 🙂

LGTM, but it would be good if @wallyqs could take a look as well.

Thanks for the contribution!

Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

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

LGTM

@piotrpio piotrpio merged commit dfb52ad into nats-io:main Jan 29, 2025
4 checks passed
@rsafonseca
Copy link
Contributor Author

rsafonseca commented Jan 29, 2025

qq, when do you expect a new tag version that includes it? :)

@piotrpio
Copy link
Collaborator

@rsafonseca most probably second week of February

@piotrpio piotrpio mentioned this pull request Feb 5, 2025
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.

Parsing errors leading to disconnections when WS compression is enabled
4 participants