net.Conn reading race #181
-
So I implemented my proxy and I am trying to parse copies of packets that are passed through (to avoid modifying them by chance) in different gorutine. For some reason sometimes I get straight incorrect data from packets even though id is correct and packet seems to be correct too (not getting connection lost on connected client). Here is example of how it is breaking itself: (parsing chunk and light packet) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
I checked with -race flag and it seems like net.Conn.ReadPacket inside bot's HandleGame is still writing to the packet while it is being read by my gorutine that received it through channel in generic event handler, makes no sense for me, how ReadPacket is not blocking until writing operation finished? |
Beta Was this translation helpful? Give feedback.
-
Instead of sending to channel packet structure I decided to reassemble it and make a new slice of bytes, looks like it is working now. |
Beta Was this translation helpful? Give feedback.
-
All data is only available in the context which give the data to you. And the buffer will be reused in the next receiving. You are exporting the data without copying the data, which is not allowed. When introducing multithreading and asynchrony, it's your own responsibility to ensure that there is no data race. |
Beta Was this translation helpful? Give feedback.
-
Reusing buffers is a very important optimization method, and allocating memory is a huge overhead, especially for infrastructure such as network library, which needs to pay great attention to performance. |
Beta Was this translation helpful? Give feedback.
Instead of sending to channel packet structure I decided to reassemble it and make a new slice of bytes, looks like it is working now.