Skip to content

Commit

Permalink
Fix data race in tailBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 18, 2022
1 parent 72c8dc1 commit da870b6
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"regexp"
"sync"
"time"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -151,14 +152,24 @@ func (c conn) waitWithTimeout() error {
}

type tailBuffer struct {
mu sync.Mutex

limit int
bytes.Buffer
buff bytes.Buffer
}

func (b *tailBuffer) Write(p []byte) (n int, err error) {
if len(p)+b.Buffer.Len() > b.limit {
b.Reset()
b.mu.Lock()
defer b.mu.Unlock()
if len(p)+b.buff.Len() > b.limit {
b.buff.Reset()
}
n, err = b.Buffer.Write(p)
n, err = b.buff.Write(p)
return
}

func (b *tailBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buff.String()
}

0 comments on commit da870b6

Please sign in to comment.