-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: Support SIGTERM on Win and all unix variants.
Go 1.14 added runtime support for handling the windows CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT events by adding a definition for syscall.SIGTERM to windows and converting the events to that signal. It also added definitions for other common signals, including SIGHUP, and treats them as NOOPs on windows. Consequently, this modifies the logic that deals with conditionally handling SIGTERM and SIGHUP to handle them for windows as well as all unix-like operating systems, including newer ones that are supported by Go since the code was originally written. Although the additional signals could probably just be added unconditionally without too much issue now due to the runtime adding stubs to all operating systems the project officially supports, it is safer to be explicit when dealing with syscall definitions since there is no guarantee that they will exist for newly-added operating systems. Stated more plainly, this change means dcrpool will now be shutdown cleanly on more variants of unix as well as when being terminated by windows itself due to various things such as the user logging off, the window terminal being closed, and the system shutting down. It also pulls in the logic from dcrd so that logging is added when a signal is received so it is clear why the process is being shutdown.
- Loading branch information
1 parent
5bb1641
commit 89a2ed8
Showing
4 changed files
with
64 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2015-2023 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
) | ||
|
||
// interruptSignals defines the default signals to catch in order to do a proper | ||
// shutdown. This may be modified during init depending on the platform. | ||
var interruptSignals = []os.Signal{os.Interrupt} | ||
|
||
// shutdownListener returns a context whose done channel will be closed when OS | ||
// signals such as SIGINT (Ctrl+C) are received along with a cancel function | ||
// that may be used to manually close the channel. | ||
func shutdownListener() (context.Context, context.CancelFunc) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
interruptChannel := make(chan os.Signal, 1) | ||
signal.Notify(interruptChannel, interruptSignals...) | ||
|
||
// Listen for initial shutdown signal and cancel the context or | ||
// fallthrough if the caller manually cancels the context. | ||
select { | ||
case sig := <-interruptChannel: | ||
mpLog.Infof("Received signal (%s). Shutting down...", sig) | ||
cancel() | ||
case <-ctx.Done(): | ||
} | ||
|
||
// Listen for repeated signals and display a message so the user knows | ||
// the shutdown is in progress and the process is not hung. | ||
for { | ||
sig := <-interruptChannel | ||
mpLog.Infof("Received signal (%s). Already shutting down...", sig) | ||
} | ||
}() | ||
|
||
return ctx, cancel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) 2021-2023 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
// | ||
//go:build windows || aix || android || darwin || dragonfly || freebsd || hurd || illumos || ios || linux || netbsd || openbsd || solaris | ||
|
||
package main | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func init() { | ||
interruptSignals = append(interruptSignals, syscall.SIGTERM, syscall.SIGHUP) | ||
} |
This file was deleted.
Oops, something went wrong.