Skip to content

Commit

Permalink
Rename signal handlers to be clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 18, 2022
1 parent 8cb830c commit f06d6f0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
globalCancel()
}()
}
hardStop := func(sig os.Signal) {
onHardStop := func(sig os.Signal) {
logger.WithField("sig", sig).Error("Aborting k6 in response to signal, we won't wait for the test to end.")
}
stopSignalHandling := handleTestAbortSignals(c.gs, gracefulStop, hardStop)
stopSignalHandling := handleTestAbortSignals(c.gs, gracefulStop, onHardStop)
defer stopSignalHandling()

et, err := lib.NewExecutionTuple(test.derivedConfig.ExecutionSegment, test.derivedConfig.ExecutionSegmentSequence)
Expand Down
8 changes: 4 additions & 4 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ func printToStdout(gs *globalState, s string) {
}

// Trap Interrupts, SIGINTs and SIGTERMs and call the given.
func handleTestAbortSignals(gs *globalState, firstHandler, secondHandler func(os.Signal)) (stop func()) {
func handleTestAbortSignals(gs *globalState, gracefulStopHandler, onHardStop func(os.Signal)) (stop func()) {
sigC := make(chan os.Signal, 2)
done := make(chan struct{})
gs.signalNotify(sigC, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
select {
case sig := <-sigC:
firstHandler(sig)
gracefulStopHandler(sig)
case <-done:
return
}

select {
case sig := <-sigC:
if secondHandler != nil {
secondHandler(sig)
if onHardStop != nil {
onHardStop(sig)
}
// If we get a second signal, we immediately exit, so something like
// https://github.com/k6io/k6/issues/971 never happens again
Expand Down
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) error {
logger.WithField("sig", sig).Debug("Stopping k6 in response to signal...")
lingerCancel() // stop the test run, metric processing is cancelled below
}
hardStop := func(sig os.Signal) {
onHardStop := func(sig os.Signal) {
logger.WithField("sig", sig).Error("Aborting k6 in response to signal")
globalCancel() // not that it matters, given the following command...
}
stopSignalHandling := handleTestAbortSignals(c.gs, gracefulStop, hardStop)
stopSignalHandling := handleTestAbortSignals(c.gs, gracefulStop, onHardStop)
defer stopSignalHandling()

// Initialize the engine
Expand Down

0 comments on commit f06d6f0

Please sign in to comment.