-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
bytes: Buffer crashes in long run #47424
Comments
I'm not sure how this could happen. It should always be the case that Without a reproducer we can run, it's going to be hard for us to debug this. |
This does look like a data race. I'd encourage you to use one of the forums at https://golang.org/wiki/Questions to get help first. |
I will try to provide more info using race detector, but the way I'm accessing the buffer is like this type SafeBuffer struct { buf bytes.Buffer locker sync.Mutex } func (f *SafeBuffer) write(data []byte) { f.locker.Lock() defer f.locker.Unlock() f.buf.Write(data) } func (f *SafeBuffer) read(len int) []byte { f.locker.Lock() defer f.locker.Unlock() if f.buf.Len() >= len { return f.buf.Next(len) } return make([]byte, 0) } |
@umtwo You need to copy the slice returned by Next to a new slice if you intend for your read method to be used concurrently. |
@peterbourgon It seems reasonable, I will give it a try. Thanks for the advice |
Given no further information, I think this can be closed for now |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?linux/amd64
What did you do?
I used bytes.Buffer package for network operations data buffering in a multi-threaded server.
The flow is very simple as writing to the buffer in any socket read, then read the needed amount if the frame is completed.
The write and read process is protected by Mutex. So no thread conflicts there.
What did you expect to see?
normal operation as a buffer
What did you see instead?
Some times it causes panic while in grow function mostly after a long run.
The trace is as bellow:
The text was updated successfully, but these errors were encountered: