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: panic on exit #2908

Merged
merged 2 commits into from
Oct 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions internal/terminal/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
const interactivePrompt = "\033[32m>\033[0m "

var _ readline.AutoCompleter = &FTLCompletion{}
var errExitTrap = errors.New("exit trap")

type KongContextBinder func(ctx context.Context, kctx *kong.Context) context.Context

Expand Down Expand Up @@ -53,7 +52,19 @@ func RunInteractiveConsole(ctx context.Context, k *kong.Kong, binder KongContext
})
l.CaptureExitSignal()
// Overload the exit function to avoid exiting the process
k.Exit = func(i int) { panic(errExitTrap) }
existing := k.Exit
k.Exit = func(i int) {
if i != 0 {
_ = l.Close()
if existing == nil {
// Should not happen, but no harm being cautious
os.Exit(i)
}
existing(i)
}
// For a normal exit from an interactive command we just ignore it
// This usually comes from --help or --version
}
for {
line, err := l.Readline()
if errors.Is(err, readline.ErrInterrupt) {
Expand All @@ -77,14 +88,6 @@ func RunInteractiveConsole(ctx context.Context, k *kong.Kong, binder KongContext
continue
}
func() {
defer func() {
// Catch Exit() and continue the loop
if r := recover(); r != nil {
if r == errExitTrap { //nolint:errorlint
return
}
}
}()
kctx, err := k.Parse(args)
if err != nil {
errorf("%s", err)
Expand All @@ -99,6 +102,7 @@ func RunInteractiveConsole(ctx context.Context, k *kong.Kong, binder KongContext
}
}()
}
_ = l.Close()
return nil
}

Expand Down
17 changes: 7 additions & 10 deletions internal/terminal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type terminalStatusManager struct {
write *os.File
closed atomic.Value[bool]
totalStatusLines int
statusLock sync.RWMutex
statusLock sync.Mutex
lines []*terminalStatusLine
moduleLine *terminalStatusLine
moduleStates map[string]BuildState
Expand All @@ -118,7 +118,7 @@ func NewStatusManager(ctx context.Context) StatusManager {
if err != nil {
return &noopStatusManager{}
}
sm := &terminalStatusManager{statusLock: sync.RWMutex{}, moduleStates: map[string]BuildState{}, height: height, width: width, exitWait: sync.WaitGroup{}}
sm := &terminalStatusManager{statusLock: sync.Mutex{}, moduleStates: map[string]BuildState{}, height: height, width: width, exitWait: sync.WaitGroup{}}
sm.exitWait.Add(1)
sm.old = os.Stdout
sm.oldErr = os.Stderr
Expand Down Expand Up @@ -257,10 +257,6 @@ func IsANSITerminal(ctx context.Context) bool {
return ok
}

func (r *terminalStatusManager) gotoCoords(line int, col int) {
r.underlyingWrite(fmt.Sprintf("\033[%d;%dH", line, col))
}

func (r *terminalStatusManager) clearStatusMessages() {
if r.totalStatusLines == 0 {
return
Expand Down Expand Up @@ -359,8 +355,8 @@ func (r *terminalStatusManager) Close() {
}

func (r *terminalStatusManager) writeLine(s string, last bool) {
r.statusLock.RLock()
defer r.statusLock.RUnlock()
r.statusLock.Lock()
defer r.statusLock.Unlock()
if !last {
s += "\n"
}
Expand All @@ -371,7 +367,9 @@ func (r *terminalStatusManager) writeLine(s string, last bool) {
}
r.clearStatusMessages()
r.underlyingWrite("\r" + s)
r.redrawStatus()
if !last {
r.redrawStatus()
}

}
func (r *terminalStatusManager) redrawStatus() {
Expand Down Expand Up @@ -476,7 +474,6 @@ type terminalStatusLine struct {
func (r *terminalStatusLine) Close() {
r.manager.statusLock.Lock()
defer r.manager.statusLock.Unlock()

for i := range r.manager.lines {
if r.manager.lines[i] == r {
r.manager.lines = append(r.manager.lines[:i], r.manager.lines[i+1:]...)
Expand Down
Loading