-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
transport: fix racey send to writes channel in WriteStatus #1546
Conversation
Reproducible case: func TestHandlerTransport_HandleStreams_MultiWriteStatus(t *testing.T) {
st := newHandleStreamTest(t)
handleStream := func(s *Stream) {
if want := "/service/foo.bar"; s.method != want {
t.Errorf("stream method = %q; want %q", s.method, want)
}
st.bodyw.Close() // no body
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func() {
defer wg.Done()
st.ht.WriteStatus(s, status.New(codes.OK, ""))
}()
}
wg.Wait()
}
st.ht.HandleStreams(
func(s *Stream) { go handleStream(s) },
func(ctx context.Context, method string) context.Context { return ctx },
)
} We are using grpc-go v1.6.0. Thanks! |
Could anybody review this, please? /cc @menghanl @MakMukhi @dfawley We are seeing the panics on this code path pretty often when closing down grpc-server with inflight streams. type ServerTransport interface {
...
// WriteStatus sends the status of a stream to the client. WriteStatus is
// the final call made on a stream and always occurs.
WriteStatus(s *Stream, st *status.Status) error
... I believe This patch just marks the stream as done before calling |
The change LGTM. Thanks for the fix. |
@menghanl PTAL. And could we have patch release v1.6.1 with this? Thanks a lot! |
Thanks for the quick fix. |
@menghanl Sounds good. Thanks! |
Can please also you do a rebase? |
Concurrent 'SendMsg' calls to stream lead to multiple 'WriteStatus' calls, while closing 'writes' channel is not synchronized. This patch marks 'streamDone' first before 'ht.do', so that following 'WriteStatus' does not trigger panic on 'writes' channel. Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
@menghanl Just rebased with current master. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
LGTM.
Concurrent 'SendMsg' calls to stream lead to
multiple 'WriteStatus' calls, while closing
'writes' channel is not synchronized.
This patch marks 'streamDone' first before 'ht.do',
so that following 'WriteStatus' does not trigger panic
on 'writes' channel.
Address #1111.
c.f. etcd-io/etcd#8627.