xtaci / smux

A Stream Multiplexing Library for golang with least memory usage(TDMA)
MIT License
1.27k stars 189 forks source link

cmdFIN and cmdPSH race condition #82

Closed MicOestergaard closed 3 years ago

MicOestergaard commented 3 years ago

I have a client and a server that establishes many concurrent short lived streams. Once a stream is established both the server and the client reads and writes data from/to it. Just before the stream is closed, the server writes the final block of data to the stream and closes it. The client reads that final block of data and closes the stream as well.

Normally this works very well, but under high load something strange happens:

The client is waiting in a Read() call, but fails with an EOF error instead of getting the last block of data from the server. After much debugging it seems to me that the client receives the cmdFIN frame before the cmdPSH frame, even through the server calls Write() and Close() in the right order.

This only happens under high load (around 20+ new sessions per second) and only once in every 500-1000 sessions.

xtaci commented 3 years ago

could you please try this fix: https://github.com/xtaci/smux/commit/fa45f32081fc91cfe98889120de1ca0fcb5daee7

MicOestergaard commented 3 years ago

That seems to have fixed the problem. Thanks!