Skip to content

Commit

Permalink
Listen for \q to gracefully shutdown the node (#5389)
Browse files Browse the repository at this point in the history
## Motivation
Issue #5321

Listening for \q will allow smapp to gracefully shutdown the node.

Co-authored-by: Matthias <5011972+fasmat@users.noreply.github.com>
  • Loading branch information
kacpersaw and fasmat committed Jan 17, 2024
1 parent 6f2fd00 commit 28a4d15
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package node

import (
"bufio"
"bytes"
"context"
"encoding/hex"
Expand Down Expand Up @@ -202,6 +203,27 @@ func GetCommand() *cobra.Command {
// os.Interrupt for all systems, especially windows, syscall.SIGTERM is mainly for docker.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()

// Workaround for Windows not being able to `TASKKILL` without `/F`.
// Instead smapp can close the node gracefully by closing stdin (analogous to `CTRL+D`).
// CTRL+C is still supported for other platforms and when the node is running in console on windows.
// see https://github.com/spacemeshos/go-spacemesh/issues/5321
go func() {
for {
select {
case <-ctx.Done():
return
default:
buf := bufio.NewReader(os.Stdin)
_, err := buf.ReadByte()
if err != nil {
cancel()
return
}
}
}
}()

if err := run(ctx); err != nil {
app.log.With().Fatal(err.Error())
}
Expand Down

0 comments on commit 28a4d15

Please sign in to comment.