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

Send timeout might corrupt outgoing messages #40

Closed
Matthias247 opened this issue Oct 10, 2016 · 2 comments · Fixed by #103
Closed

Send timeout might corrupt outgoing messages #40

Matthias247 opened this issue Oct 10, 2016 · 2 comments · Fixed by #103

Comments

@Matthias247
Copy link

Hi everyone,

I just reviewed the library a little bit since I'm also interested in stream multiplexing (actually want something for websockets instead of plain TCP, but I guess yamux would work there too).
Overall it looks really well written and I like the fact that you have a spec :-)

One thing that I stumbled upon during review was there might be situations where the send timeout for the session might corrupt outgoing data: If we look at the waitForSendErr function it will start the timout and try to send the data in parallel be handing it over to the send channel.

The select/timeout logic looks like it introduces a data race to me: If the timeout is expiring here the send will still be in progress. That means the data (header and body) will still be in use in the send routine. However the waitForSendErr call returns with an the calling stream could think the data is no longer required and reuse it. It might probably not do it for sendHdr as it sees that writes fail and no further writes and sendHdr mutations follow, but the payload data slice might be reused by the application and mutated - while the send is still in progress. As an end result you could send data to the wire that was not desired.

Fixing this is probably not that easy. I guess that there probably could be some cancellation or sendInProgress flags in the enqueued sendReady struct. If the message was not yet fetched by the writing proc it can be marked as cancelled and will there no longer be processed. However as soon as the writer goroutine catches it the safest thing is to let it write to completion as there is no kind of CancellationToken that can be passed to to the Write or Copy methods.

@dosgo
Copy link

dosgo commented Feb 5, 2021

I also encountered this problem!

@mafredri
Copy link
Contributor

mafredri commented Jun 1, 2022

I believe I've identified the same underlying issue, which I documented in #100. AFAICT, sendHdr is not guaranteed to be protected on access, especially considering sendCh is buffered.

mafredri added a commit to coder/yamux that referenced this issue Jul 11, 2022
This commit takes a minimal approach to fixing unsafe header re-use
during close and timeout by reallocating the header in those cases.

Partially fixes: coder/coder#2429

Related: hashicorp#40
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 a pull request may close this issue.

3 participants