1
0
mirror of https://github.com/golang/go synced 2024-11-05 15:46:11 -07:00

internal/lsp/protocol: make loggingStream log writes concurrency-safe

Per the documentation for jsonrpc2.Stream Write must be safe for
concurrent use, but this isn't the case for the loggingStream.

Guard it with a mutex.

Change-Id: I384892b90cef950d518089421d05cf8040c6b233
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227487
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rob Findley 2020-04-07 17:35:36 -04:00 committed by Robert Findley
parent 77362c5149
commit 97c4fbe514

View File

@ -14,23 +14,28 @@ import (
type loggingStream struct { type loggingStream struct {
stream jsonrpc2.Stream stream jsonrpc2.Stream
logMu sync.Mutex
log io.Writer log io.Writer
} }
// LoggingStream returns a stream that does LSP protocol logging too // LoggingStream returns a stream that does LSP protocol logging too
func LoggingStream(str jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream { func LoggingStream(str jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream {
return &loggingStream{str, w} return &loggingStream{stream: str, log: w}
} }
func (s *loggingStream) Read(ctx context.Context) ([]byte, int64, error) { func (s *loggingStream) Read(ctx context.Context) ([]byte, int64, error) {
data, count, err := s.stream.Read(ctx) data, count, err := s.stream.Read(ctx)
if err == nil { if err == nil {
s.logMu.Lock()
defer s.logMu.Unlock()
logIn(s.log, data) logIn(s.log, data)
} }
return data, count, err return data, count, err
} }
func (s *loggingStream) Write(ctx context.Context, data []byte) (int64, error) { func (s *loggingStream) Write(ctx context.Context, data []byte) (int64, error) {
s.logMu.Lock()
defer s.logMu.Unlock()
logOut(s.log, data) logOut(s.log, data)
count, err := s.stream.Write(ctx, data) count, err := s.stream.Write(ctx, data)
return count, err return count, err