Giter Site home page Giter Site logo

Deadlock in `signalReceivers.Stop` about fx HOT 6 OPEN

ogaca-dd avatar ogaca-dd commented on August 19, 2024
Deadlock in `signalReceivers.Stop`

from fx.

Comments (6)

JacobOaks avatar JacobOaks commented on August 19, 2024 1

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:

  • Not manually call shutdown when you receive a signal
  • Use 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.

from fx.

ogaca-dd avatar ogaca-dd commented on August 19, 2024 1

Thanks for your quick reply and for the workarounds.

from fx.

JacobOaks avatar JacobOaks commented on August 19, 2024

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?

from fx.

ogaca-dd avatar ogaca-dd commented on August 19, 2024

@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.

from fx.

JacobOaks avatar JacobOaks commented on August 19, 2024

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.

from fx.

ogaca-dd avatar ogaca-dd commented on August 19, 2024

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:

  • The frequency of the issue depends on the OS
  • Adding debugging logs decreases significantly the occurence of the issue which make it harder to catch.

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.

from fx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.