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 WebSocket#stream flushing for not exactly buffer size, add specs #11299

Merged
merged 2 commits into from
Nov 29, 2021

Conversation

will
Copy link
Contributor

@will will commented Oct 8, 2021

StreamIO#flush was incorrectly sending data from the insertion position
until the end of the buffer, instead of from the start of the buffer up
until the position.

The existing tests did not catch this because they were sending 1.5x
buffer size of the same character, and so when it did the second flush,
the position happened to have the exact number of bytes left and they
were already set with that character from the first time around.

This patch fixes the problem, and adds a test for streaming less than
one buffer size.

It also adds a few end-to-end websocket integration tests to cover
several different situations to hopefully prevent regressions in the
future.

Fixes #11296

StreamIO#flush was incorrectly sending data from the insertion position
until the end of the buffer, instead of from the start of the buffer up
until the position.

The existing tests did not catch this because they were sending 1.5x
buffer size of the same character, and so when it did the second flush,
the position happened to have the exact number of bytes left and they
were already set with that character from the first time around.

This patch fixes the problem, and adds a test for streaming less than
one buffer size.

It also adds a few end-to-end websocket integration tests to cover
several different situations to hopefully prevent regressions in the
future.
spec/std/http/web_socket_spec.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:networking labels Oct 9, 2021
@oprypin oprypin changed the title Fix WebSocket#stream Fix WebSocket#stream flushing for less than one buffer size, add specs Oct 26, 2021
@straight-shoota straight-shoota added this to the 1.3.0 milestone Oct 26, 2021
@will
Copy link
Contributor Author

will commented Oct 26, 2021

oprypin changed the title Fix WebSocket#stream Fix WebSocket#stream flushing for less than one buffer size, add specs 5 hours ago

@oprypin I think it's a problem for any stream that is not exactly a multiple of the buffer size. Like if you send today 1.2x a buffer size, you get the last 80% of the first buffer appended to the end of the second flush. But it's been a few days now and all of this has fallen out of my head completely, so I'm not sure.

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

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

Thank you @will 🙏

@straight-shoota straight-shoota changed the title Fix WebSocket#stream flushing for less than one buffer size, add specs Fix WebSocket#stream flushing for not exactly buffer size, add specs Nov 26, 2021
@straight-shoota straight-shoota changed the title Fix WebSocket#stream flushing for not exactly buffer size, add specs Fix WebSocket#stream flushing for not exactly buffer size, add specs Nov 29, 2021
@straight-shoota straight-shoota merged commit 4e10284 into crystal-lang:master Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:networking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WebSocket::StreamIO seems broken
4 participants