Skip to content
Myr

fix: race changing ignoreSignals (#791)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: race changing ignoreSignals * fix: atomic.Uint32

authored by Carlos Alexandro Becker · committed by GitHub · parent 5536bca · Browse files · 3 files+9 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+9 added−6 removed

  • other:+9 added−6 removed

3 files+9 added−6 removed

modified:options_test.go+2 added-1 removed
1package tea23import (4 "bytes"5 "sync/atomic"6 "testing"7)89func TestOptions(t *testing.T) {
27 unmodified lines
37 })3839 t.Run("without signals", func(t *testing.T) {40 p := NewProgram(nil, WithoutSignals())40 if !p.ignoreSignals {41 if atomic.LoadUint32(&p.ignoreSignals) == 0 {42 t.Errorf("ignore signals should have been set")43 }44 })45
83 unmodified lines
modified:options.go+2 added-1 removed
1 unmodified line
23import (4 "context"5 "io"6 "sync/atomic"78 "github.com/muesli/termenv"9)10
65 unmodified lines
76// WithoutSignals will ignore OS signals.77// This is mainly useful for testing.78func WithoutSignals() ProgramOption {79 return func(p *Program) {79 p.ignoreSignals = true80 atomic.StoreUint32(&p.ignoreSignals, 1)81 }82}8384// WithAltScreen starts the program with the alternate screen buffer enabled
128 unmodified lines
modified:tea.go+5 added-4 removed
16 unmodified lines
17 "os"18 "os/signal"19 "runtime/debug"20 "sync"21 "sync/atomic"22 "syscall"2324 "github.com/containerd/console"25 isatty "github.com/mattn/go-isatty"
127 unmodified lines
153 console console.Console154155 // was the altscreen active before releasing the terminal?156 altScreenWasActive bool156 ignoreSignals bool157 ignoreSignals uint32158159 // Stores the original reference to stdin for cases where input is not a160 // TTY on windows and we've automatically opened CONIN$ to receive input.161 // When the program exits this will be restored.
76 unmodified lines
238 case <-p.ctx.Done():239 return240241 case <-sig:241 if !p.ignoreSignals {242 if atomic.LoadUint32(&p.ignoreSignals) == 0 {243 p.msgs <- QuitMsg{}244 return245 }246 }
385 unmodified lines
632633// ReleaseTerminal restores the original terminal state and cancels the input634// reader. You can return control to the Program with RestoreTerminal.635func (p *Program) ReleaseTerminal() error {635 p.ignoreSignals = true636 atomic.StoreUint32(&p.ignoreSignals, 1)637 p.cancelReader.Cancel()638 p.waitForReadLoop()639640 if p.renderer != nil {
7 unmodified lines
648// RestoreTerminal reinitializes the Program's input reader, restores the649// terminal to the former state when the program was running, and repaints.650// Use it to reinitialize a Program after running ReleaseTerminal.651func (p *Program) RestoreTerminal() error {651 p.ignoreSignals = false652 atomic.StoreUint32(&p.ignoreSignals, 0)653654 if err := p.initTerminal(); err != nil {655 return err656 }
46 unmodified lines