Skip to content

Commit

Permalink
fix data races on width changes
Browse files Browse the repository at this point in the history
Fixes chzyer#214

Even under a further-reaching concurrency redesign, it makes sense to use
sync/atomic for these because they are modified from signal handlers.
  • Loading branch information
slingamn committed Feb 13, 2023
1 parent f0118c0 commit 9c2be2d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"sync/atomic"
)

type AutoCompleter interface {
Expand All @@ -27,7 +28,7 @@ func (t *TabCompleter) Do([]rune, int) ([][]rune, int) {
type opCompleter struct {
w io.Writer
op *Operation
width int
width atomic.Int32

inCompleteMode bool
inSelectMode bool
Expand All @@ -39,11 +40,12 @@ type opCompleter struct {
}

func newOpCompleter(w io.Writer, op *Operation, width int) *opCompleter {
return &opCompleter{
o := &opCompleter{
w: w,
op: op,
width: width,
}
o.width.Store(int32(width))
return o
}

func (o *opCompleter) doSelect() {
Expand All @@ -65,7 +67,7 @@ func (o *opCompleter) nextCandidate(i int) {
}

func (o *opCompleter) OnComplete() bool {
if o.width == 0 {
if o.width.Load() == 0 {
return false
}
if o.IsInCompleteSelectMode() {
Expand Down Expand Up @@ -182,7 +184,7 @@ func (o *opCompleter) getMatrixSize() int {
}

func (o *opCompleter) OnWidthChange(newWidth int) {
o.width = newWidth
o.width.Store(int32(newWidth))
}

func (o *opCompleter) CompleteRefresh() {
Expand All @@ -201,7 +203,7 @@ func (o *opCompleter) CompleteRefresh() {
same := o.op.buf.RuneSlice(-o.candidateOff)

// -1 to avoid reach the end of line
width := o.width - 1
width := int(o.width.Load()) - 1
colNum := width / colWidth
if colNum != 0 {
colWidth += (width - (colWidth * colNum)) / colNum
Expand Down
14 changes: 8 additions & 6 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"container/list"
"fmt"
"io"
"sync/atomic"
)

const (
Expand All @@ -29,21 +30,22 @@ type opSearch struct {
cfg *Config
markStart int
markEnd int
width int
width atomic.Int32
}

func newOpSearch(w io.Writer, buf *RuneBuffer, history *opHistory, cfg *Config, width int) *opSearch {
return &opSearch{
o := &opSearch{
w: w,
buf: buf,
cfg: cfg,
history: history,
width: width,
}
o.width.Store(int32(width))
return o
}

func (o *opSearch) OnWidthChange(newWidth int) {
o.width = newWidth
o.width.Store(int32(newWidth))
}

func (o *opSearch) IsSearchMode() bool {
Expand Down Expand Up @@ -97,7 +99,7 @@ func (o *opSearch) SearchChar(r rune) {
}

func (o *opSearch) SearchMode(dir int) bool {
if o.width == 0 {
if o.width.Load() == 0 {
return false
}
alreadyInMode := o.inMode
Expand Down Expand Up @@ -135,7 +137,7 @@ func (o *opSearch) SearchRefresh(x int) {
}
x = o.buf.CurrentWidth(x)
x += o.buf.PromptLen()
x = x % o.width
x = x % int(o.width.Load())

if o.markStart > 0 {
o.buf.SetStyle(o.markStart, o.markEnd, "4")
Expand Down

0 comments on commit 9c2be2d

Please sign in to comment.