Skip to content

Updates how interrupts are handled in azd #4898

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 24 additions & 0 deletions cli/azd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"

azcorelog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
Expand All @@ -39,6 +41,7 @@ import (

func main() {
ctx := context.Background()
ctx = withInterruptContext(ctx)

restoreColorMode := colorable.EnableColorsStdout(nil)
defer restoreColorMode()
Expand Down Expand Up @@ -381,3 +384,24 @@ func startBackgroundUploadProcess() error {
err = cmd.Start()
return err
}

// withInterruptContext creates a new context that is cancelled when the user
// sends an interrupt signal (Ctrl+C) or a SIGTERM signal to the process.
func withInterruptContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChan
log.Println("received interrupt signal, cancelling context")
cancel()

time.Sleep(1 * time.Second)
err := fmt.Errorf("Cancelled by user: %w", ctx.Err())
fmt.Println(output.WithErrorFormat("ERROR: %s", err.Error()))
os.Exit(1)
}()

return ctx
}
30 changes: 15 additions & 15 deletions cli/azd/pkg/input/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ type AskerConsole struct {
// holds the last 2 bytes written by message or messageUX. This is used to detect when there is already an empty
// line (\n\n)
last2Byte [2]byte

spinnerStopChan chan struct{}
}

type ConsoleOptions struct {
Expand Down Expand Up @@ -406,7 +408,19 @@ func (c *AskerConsole) ShowSpinner(ctx context.Context, title string, format Spi
// While it is indeed safe to call Start regardless of whether the spinner is running,
// calling Start may result in an additional line of output being written in non-tty scenarios
_ = c.spinner.Start()

go func() {
c.spinnerStopChan = make(chan struct{}, 1)

select {
case <-ctx.Done():
case <-c.spinnerStopChan:
}

_ = c.spinner.Stop()
}()
}

c.spinnerLineMu.Unlock()
}

Expand Down Expand Up @@ -468,7 +482,7 @@ func (c *AskerConsole) StopSpinner(ctx context.Context, lastMessage string, form
lastMessage = c.getStopChar(format) + " " + lastMessage
}

_ = c.spinner.Stop()
c.spinnerStopChan <- struct{}{}
if lastMessage != "" {
// Avoid using StopMessage() as it may result in an extra Message line print in non-tty scenarios
fmt.Fprintln(c.writer, lastMessage)
Expand Down Expand Up @@ -938,19 +952,6 @@ func watchTerminalResize(c *AskerConsole) {
}
}

func watchTerminalInterrupt(c *AskerConsole) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan

// unhide the cursor if applicable
_ = c.spinner.Stop()

os.Exit(1)
}()
}

// Writers that back the underlying console.
type Writers struct {
// The writer to write output to.
Expand Down Expand Up @@ -1015,7 +1016,6 @@ func NewConsole(
if isTerminal {
c.consoleWidth = atomic.NewInt32(consoleWidth())
watchTerminalResize(c)
watchTerminalInterrupt(c)
}

return c
Expand Down
Loading