mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
fix: Add lock around read/write of circular buffer (#1258)
This fixes a race seen in: https://github.com/coder/coder/runs/6260926628?check_suite_focus=true#step:10:666
This commit is contained in:
@ -518,7 +518,9 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
part := buffer[:read]
|
part := buffer[:read]
|
||||||
|
rpty.circularBufferMutex.Lock()
|
||||||
_, err = rpty.circularBuffer.Write(part)
|
_, err = rpty.circularBuffer.Write(part)
|
||||||
|
rpty.circularBufferMutex.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.logger.Error(ctx, "reconnecting pty write buffer", slog.Error(err), slog.F("id", id))
|
a.logger.Error(ctx, "reconnecting pty write buffer", slog.Error(err), slog.F("id", id))
|
||||||
break
|
break
|
||||||
@ -545,7 +547,9 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
|
|||||||
a.logger.Error(ctx, "resize reconnecting pty", slog.F("id", id), slog.Error(err))
|
a.logger.Error(ctx, "resize reconnecting pty", slog.F("id", id), slog.Error(err))
|
||||||
}
|
}
|
||||||
// Write any previously stored data for the TTY.
|
// Write any previously stored data for the TTY.
|
||||||
|
rpty.circularBufferMutex.RLock()
|
||||||
_, err = conn.Write(rpty.circularBuffer.Bytes())
|
_, err = conn.Write(rpty.circularBuffer.Bytes())
|
||||||
|
rpty.circularBufferMutex.RUnlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.logger.Warn(ctx, "write reconnecting pty buffer", slog.F("id", id), slog.Error(err))
|
a.logger.Warn(ctx, "write reconnecting pty buffer", slog.F("id", id), slog.Error(err))
|
||||||
return
|
return
|
||||||
@ -640,9 +644,10 @@ type reconnectingPTY struct {
|
|||||||
activeConnsMutex sync.Mutex
|
activeConnsMutex sync.Mutex
|
||||||
activeConns map[string]net.Conn
|
activeConns map[string]net.Conn
|
||||||
|
|
||||||
circularBuffer *circbuf.Buffer
|
circularBuffer *circbuf.Buffer
|
||||||
timeout *time.Timer
|
circularBufferMutex sync.RWMutex
|
||||||
ptty pty.PTY
|
timeout *time.Timer
|
||||||
|
ptty pty.PTY
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close ends all connections to the reconnecting
|
// Close ends all connections to the reconnecting
|
||||||
|
Reference in New Issue
Block a user