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 all commits
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
3 changes: 2 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tea
import (
"context"
"io"
"sync/atomic"

"github.com/muesli/termenv"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func WithoutCatchPanics() ProgramOption {
// This is mainly useful for testing.
func WithoutSignals() ProgramOption {
return func(p *Program) {
p.ignoreSignals = true
atomic.StoreUint32(&p.ignoreSignals, 1)
}
}

Expand Down
3 changes: 2 additions & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tea

import (
"bytes"
"sync/atomic"
"testing"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func TestOptions(t *testing.T) {

t.Run("without signals", func(t *testing.T) {
p := NewProgram(nil, WithoutSignals())
if !p.ignoreSignals {
if atomic.LoadUint32(&p.ignoreSignals) == 0 {
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 uint32

// 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 atomic.LoadUint32(&p.ignoreSignals) == 0 {
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
atomic.StoreUint32(&p.ignoreSignals, 1)
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
atomic.StoreUint32(&p.ignoreSignals, 0)

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