Skip to content

Commit

Permalink
fix: race changing ignoreSignals
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Aug 1, 2023
1 parent 91dd120 commit e3fac6f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func WithoutCatchPanics() ProgramOption {
// This is mainly useful for testing.
func WithoutSignals() ProgramOption {
return func(p *Program) {
p.ignoreSignals = true
p.ignoreSignals.Store(true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestOptions(t *testing.T) {

t.Run("without signals", func(t *testing.T) {
p := NewProgram(nil, WithoutSignals())
if !p.ignoreSignals {
if !p.ignoreSignals.Load() {
t.Errorf("ignore signals should have been set")
}
})
Expand Down
9 changes: 5 additions & 4 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os/signal"
"runtime/debug"
"sync"
"sync/atomic"
"syscall"

"github.com/containerd/console"
Expand Down Expand Up @@ -153,7 +154,7 @@ type Program struct {

// was the altscreen active before releasing the terminal?
altScreenWasActive bool
ignoreSignals bool
ignoreSignals atomic.Bool

Check failure on line 157 in tea.go

View workflow job for this annotation

GitHub Actions / build (~1.17, ubuntu-latest)

undefined: atomic.Bool

Check failure on line 157 in tea.go

View workflow job for this annotation

GitHub Actions / build (~1.17, ubuntu-latest)

undefined: atomic.Bool

// Stores the original reference to stdin for cases where input is not a
// TTY on windows and we've automatically opened CONIN$ to receive input.
Expand Down Expand Up @@ -238,7 +239,7 @@ func (p *Program) handleSignals() chan struct{} {
return

case <-sig:
if !p.ignoreSignals {
if !p.ignoreSignals.Load() {
p.msgs <- QuitMsg{}
return
}
Expand Down Expand Up @@ -632,7 +633,7 @@ func (p *Program) shutdown(kill bool) {
// ReleaseTerminal restores the original terminal state and cancels the input
// reader. You can return control to the Program with RestoreTerminal.
func (p *Program) ReleaseTerminal() error {
p.ignoreSignals = true
p.ignoreSignals.Store(true)
p.cancelReader.Cancel()
p.waitForReadLoop()

Expand All @@ -648,7 +649,7 @@ func (p *Program) ReleaseTerminal() error {
// terminal to the former state when the program was running, and repaints.
// Use it to reinitialize a Program after running ReleaseTerminal.
func (p *Program) RestoreTerminal() error {
p.ignoreSignals = false
p.ignoreSignals.Store(false)

if err := p.initTerminal(); err != nil {
return err
Expand Down

0 comments on commit e3fac6f

Please sign in to comment.