Skip to content

Commit aa2ed9a

Browse files
committed
Patch for potential deadlock
1 parent 3d6b652 commit aa2ed9a

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

server/rpc_server.go

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -327,39 +327,49 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
327327
wg.Add(2)
328328

329329
// Process the outbound messages from the socket
330-
go func(psock *socket.Socket) {
331-
defer func() {
332-
// TODO: don't hack this but if its grpc just break out of the stream
333-
// We do this because the underlying connection is h2 and its a stream
334-
if protocol == "grpc" {
335-
if err := sock.Close(); err != nil {
336-
logger.Logf(log.ErrorLevel, "Failed to close socket: %v", err)
337-
}
338-
}
339-
340-
s.deferer(pool, psock, wg)
341-
}()
342-
343-
for {
344-
// Get the message from our internal handler/stream
345-
m := new(transport.Message)
346-
if err := psock.Process(m); err != nil {
347-
return
348-
}
349-
350-
// Send the message back over the socket
351-
if err := sock.Send(m); err != nil {
352-
return
353-
}
354-
}
355-
}(psock)
330+
go func(psock *socket.Socket) {
331+
defer func() {
332+
if r := recover(); r != nil {
333+
logger.Log(log.ErrorLevel, "panic recovered in outbound goroutine: ", r)
334+
logger.Log(log.ErrorLevel, string(debug.Stack()))
335+
}
336+
// TODO: don't hack this but if its grpc just break out of the stream
337+
// We do this because the underlying connection is h2 and its a stream
338+
if protocol == "grpc" {
339+
if err := sock.Close(); err != nil {
340+
logger.Logf(log.ErrorLevel, "Failed to close socket: %v", err)
341+
}
342+
}
343+
344+
s.deferer(pool, psock, wg)
345+
}()
346+
347+
for {
348+
// Get the message from our internal handler/stream
349+
m := new(transport.Message)
350+
if err := psock.Process(m); err != nil {
351+
return
352+
}
353+
354+
// Send the message back over the socket
355+
if err := sock.Send(m); err != nil {
356+
return
357+
}
358+
}
359+
}(psock)
356360

357361
// Serve the request in a go routine as this may be a stream
358-
go func(psock *socket.Socket) {
359-
defer s.deferer(pool, psock, wg)
360-
361-
s.serveReq(ctx, msg, &request, &response, rcodec)
362-
}(psock)
362+
go func(psock *socket.Socket) {
363+
defer func() {
364+
if r := recover(); r != nil {
365+
logger.Log(log.ErrorLevel, "panic recovered in serveReq goroutine: ", r)
366+
logger.Log(log.ErrorLevel, string(debug.Stack()))
367+
}
368+
s.deferer(pool, psock, wg)
369+
}()
370+
371+
s.serveReq(ctx, msg, &request, &response, rcodec)
372+
}(psock)
363373
}
364374
}
365375

0 commit comments

Comments
 (0)