Closed neex closed 5 years ago
This is correct, I have just pushed a fix to master which effectively creates a new buffer when borrowing. Previously it was doing enc.buf = enc.buf[:0]
which is wrong. I am patching versions with the fix.
If the fix works for you, please close the issue.
Note: the problem doesn't happen if you use an io.Writer with the Encode
interface because it writes to the writer before releasing.
I thought it was the intended behaviour, just not documented.
Thanks for the fix!
Hello,
I think that the fact that
gojay.Marshal
is not safe for concurrent usage from multiple goroutines should be explicitly mentioned both in the readme and godoc.Now it is stated that
Marshal
borrows an encoder and releases it back no matter what. While an attentive reader will have realised that it introduces a race (because same encoder could be reused by another goroutine and the buffer will get corrupted), the race is easily overseen problem as everything seems to be working most of the time, so it should be mentioned (may be even in bold and with some exclamation marks).Example program that shows the race exists: