Open ogaca-dd opened 2 weeks ago
Hey @ogaca-dd thanks for the report, let me look into this.
Can I ask what kinds of situations you're experiencing where a shutdown signal and an OS signal are being sent at roughly the same time?
@JacobOaks Thanks for your quick answer.
In the situation described above, I am experiencing a dead lock meaning the application try to stop but freeze forever.
Hey @ogaca-dd - I understand. My question is why would an application call shutdowner.Shutdown()
after it gets killed? The example you provide seems pretty contrived so I'm just trying to better understand the real-world impact here.
The following code is the simplest code I found to reproduce the issue.
func main() {
fx.New(fx.Invoke(func(shutdowner fx.Shutdowner) {
go func() {
_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
_ = shutdowner.Shutdown()
}()
})).Run()
}
Our real code looks like
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
for signo := range sigChan {
switch signo {
case syscall.SIGINT, syscall.SIGTERM:
log.Infof("Received signal %d (%v)", signo, signo)
shutdowner.Shutdown()
return
}
// Some code here
}
}
I know that fx already handles signals and this code should be written differently (This code was not updated during our migration to fx).
Anyway, the code I pointed contains a dead lock which was tricky to find. In our case, this freeze happens randomly:
I understand it may not be a correct usage of fx but as a user I don't expect to have my application freeze randomly in a such case.
Gotcha. Yeah I was able to reproduce this locally and I will try to figure out the best way to update the signal handlers to prevent this, but yes - as you mentioned - using app.Run
will cause Fx to respond to signals automatically. So if you want to workaround this for now you can either:
app.Start
and then call app.Stop
when a signal comes into sigChan
. Using Start/Stop instead of Run in this way will no longer cause Fx to register its own signal handlers as of v1.22.1.Thanks for your quick reply and for the workarounds.
Describe the bug There is a deadlock in signalReceivers.Stop:
syscall.SIGINT
is sent to the processShutdowner.Shutdown()
is called to stop the applicationm
as signalReceivers.StopThere is a deadlock.
To Reproduce Run the following code:
Expected behavior No deadlock