Skip to content
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
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"os"
"os/exec"
"os/signal"
"runtime/debug"
"strings"

Expand Down Expand Up @@ -41,13 +42,18 @@ func initGotext() {
}
}

func newSignalContext() (context.Context, context.CancelFunc) {
return signal.NotifyContext(context.Background(), os.Interrupt)
}

func main() {
fallbackLog := text.NewLogger(os.Stdout, os.Stderr, os.Stdin, false, "fallback")
var (
err error
ctx = context.Background()
ret = 0
err error
ctx, stop = newSignalContext()
ret = 0
)
defer stop()

defer func() {
if rec := recover(); rec != nil {
Expand Down
26 changes: 26 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"os"
"testing"
"time"
)

func TestInterruptCancelsContext(t *testing.T) {
ctx, stop := newSignalContext()
defer stop()

process, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatal(err)
}
if err := process.Signal(os.Interrupt); err != nil {
t.Fatal(err)
}

select {
case <-ctx.Done():
case <-time.After(time.Second):
t.Fatal("interrupt did not cancel context")
}
}
Loading