Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race changing ignoreSignals #791

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
"os/signal"
"runtime/debug"
"sync"
"sync/atomic"
"syscall"

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

// 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
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@
return

case <-sig:
if !p.ignoreSignals {
if !p.ignoreSignals.Load() {
p.msgs <- QuitMsg{}
return
}
Expand Down Expand Up @@ -632,7 +633,7 @@
// 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 @@
// 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