Skip to content

Commit 97c4fbe

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 77362c5 commit 97c4fbe

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

internal/lsp/protocol/log.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@ import (
1414

1515
type loggingStream struct {
1616
stream jsonrpc2.Stream
17+
logMu sync.Mutex
1718
log io.Writer
1819
}
1920

2021
// LoggingStream returns a stream that does LSP protocol logging too
2122
func LoggingStream(str jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream {
22-
return &loggingStream{str, w}
23+
return &loggingStream{stream: str, log: w}
2324
}
2425

2526
func (s *loggingStream) Read(ctx context.Context) ([]byte, int64, error) {
2627
data, count, err := s.stream.Read(ctx)
2728
if err == nil {
29+
s.logMu.Lock()
30+
defer s.logMu.Unlock()
2831
logIn(s.log, data)
2932
}
3033
return data, count, err
3134
}
3235

3336
func (s *loggingStream) Write(ctx context.Context, data []byte) (int64, error) {
37+
s.logMu.Lock()
38+
defer s.logMu.Unlock()
3439
logOut(s.log, data)
3540
count, err := s.stream.Write(ctx, data)
3641
return count, err

0 commit comments

Comments
 (0)