Skip to content

Commit 2b9117d

Browse files
nervghalexey-igrychev
authored andcommitted
fix(graceful): reset system signal handling anyway
Signed-off-by: Alexandr Zaytsev <alexandr.zaytsev@flant.com>
1 parent be9a60f commit 2b9117d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

pkg/graceful/graceful.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,28 @@ func (t *termination) run(desc TerminationDescriptor) {
3434
}
3535

3636
// listenSystemSignals
37-
// If system signal is received, it starts termination process translating the signal to termination descriptor.
38-
// If ctx is marked as done, it stops listening the system signals.
37+
// Blocks until ctx is done or system signal (SIGINT, SIGTERM) is received.
38+
// If system signal is received, it starts termination process translating the signal to TerminationDescriptor.
39+
// When it unblocks it resets system signal handler.
3940
func (t *termination) listenSystemSignals(ctx context.Context) {
40-
listenedSignals := make(chan os.Signal, 1)
41-
signal.Notify(listenedSignals, os.Interrupt, syscall.SIGTERM)
41+
listenedSignals := []os.Signal{os.Interrupt, syscall.SIGTERM}
42+
43+
sigChan := make(chan os.Signal, 1)
44+
signal.Notify(sigChan, listenedSignals...)
4245

4346
// Block until ctx is done or signal received.
4447
select {
4548
case <-ctx.Done():
46-
signal.Stop(listenedSignals)
47-
case sig := <-listenedSignals:
49+
// do nothing
50+
case sig := <-sigChan:
4851
t.run(TerminationDescriptor{
4952
err: nil,
5053
exitCode: int(sig.(syscall.Signal)) + 128,
5154
signal: sig,
5255
})
5356
}
57+
58+
signal.Reset(listenedSignals...)
5459
}
5560

5661
type TerminationDescriptor struct {
@@ -115,9 +120,7 @@ func IsTerminating(ctx context.Context) bool {
115120
type ShutdownCallback func(ctx context.Context, desc TerminationDescriptor)
116121

117122
// Shutdown handles termination using terminationCtx. ctx must be the context created WithTermination().
118-
// If system signal is captured, it translates the signal to termination descriptor.
119-
// If panic is happened, it translates the panic to termination descriptor.
120-
// Callback is always called with termination descriptor.
123+
// Callback is always called.
121124
func Shutdown(ctx context.Context, callback ShutdownCallback) {
122125
term, ok := ctx.Value(terminationKey).(*termination)
123126
if !ok {

0 commit comments

Comments
 (0)