Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only register signal handlers if user intends to use them #1215

Merged
merged 3 commits into from
Jun 25, 2024

Conversation

MarcoPolo
Copy link
Contributor

@MarcoPolo MarcoPolo commented Jun 17, 2024

closes #1212, and fixes a regression from #989. Previously we would only register signal handlers if the user intended to use them. #989 changed this behavior here.

This regression meant that if you only used app.Start()/app.Stop(), fx would register signal handlers for no reason as the user didn't use app.Done/app.Wait.

Copy link

codecov bot commented Jun 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.41%. Comparing base (d0d12e9) to head (34bdbf2).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1215   +/-   ##
=======================================
  Coverage   98.41%   98.41%           
=======================================
  Files          34       34           
  Lines        2908     2909    +1     
=======================================
+ Hits         2862     2863    +1     
  Misses         38       38           
  Partials        8        8           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

app_internal_test.go Outdated Show resolved Hide resolved
app.go Outdated Show resolved Hide resolved
app_internal_test.go Outdated Show resolved Hide resolved
app_internal_test.go Outdated Show resolved Hide resolved
@MarcoPolo MarcoPolo force-pushed the marco/opt-disable-signal-handler branch from 4c557a6 to 1fd7e16 Compare June 24, 2024 17:23
@MarcoPolo MarcoPolo changed the title feat: Add option to disable signal handler fix: Only register signal handlers if user intends to use them Jun 24, 2024
Copy link

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a few things that surprised me. I might be wrong. But I prefer to raise the points

@@ -2331,7 +2331,9 @@ func TestHookConstructors(t *testing.T) {
func TestDone(t *testing.T) {
t.Parallel()

done := fxtest.New(t).Done()
app := fxtest.New(t)
defer app.RequireStop()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In test t.Cleanup is preferred over defer

@@ -102,7 +102,7 @@ func (recv *signalReceivers) running() bool {
return recv.shutdown != nil && recv.finished != nil
}

func (recv *signalReceivers) Start(ctx context.Context) {
func (recv *signalReceivers) Start() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the context was not used ?

err := s.Shutdown(ExitCode(1337))
require.NoError(t, err)

shutdownSig := <-app.Wait()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of GitHub workflow, I cannot comment the Wait method, so I'm commenting here.

I would expect to use the context as argument

This way Wait could stop when ctx is done.
The same way Stop does.

It's useless in the unit tests, but meaningful in the real app

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure but I would expect the same from app.Done

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last remark, I'm surprised that code in Stop are not closing the channels when leaving on ctx.Done()

app.receivers.stopNotify = func(c chan<- os.Signal) {}

app.Start(context.Background())
defer app.Stop(context.Background())
Copy link

@ccoVeille ccoVeille Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not testing it then I think. I mean the test code is not doing what you expect

When defer is called it's too late, no?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Stop should be after the wait.

Or you use a t.Cleanup(func() {
app.Stop()
require.True(t, stopCalled)
})

app.receivers.notify = func(c chan<- os.Signal, sig ...os.Signal) {
calledNotify = true
}
app.receivers.stopNotify = func(c chan<- os.Signal) {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should define a variable stopCalled bool and set it to true when called.

And then test its value after calling Stop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is relevant for this test. #1198 already includes testing that stopNotify gets called when an app stops. I think this is testing that notify does not get called when an app starts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. The only reason I'm setting .stopNotify is because I consider it bad form to call the default signal.Stop func with a channel that was never registered in the first place.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK noted

@JacobOaks
Copy link
Contributor

JacobOaks commented Jun 25, 2024

Thanks for updating the PR @MarcoPolo! Mostly LGTM, just a couple small comments.

@ccoVeille
Copy link

@MarcoPolo or @JacobOaks tell me if the review I made, makes no sense 😅

app_test.go Outdated Show resolved Hide resolved
app_test.go Outdated Show resolved Hide resolved
app_test.go Show resolved Hide resolved
app_test.go Outdated Show resolved Hide resolved
app.receivers.notify = func(c chan<- os.Signal, sig ...os.Signal) {
calledNotify = true
}
app.receivers.stopNotify = func(c chan<- os.Signal) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is relevant for this test. #1198 already includes testing that stopNotify gets called when an app stops. I think this is testing that notify does not get called when an app starts.

@JacobOaks JacobOaks merged commit 38e64ec into uber-go:master Jun 25, 2024
12 checks passed
JacobOaks added a commit to JacobOaks/fx that referenced this pull request Jun 25, 2024
Update changelog and `version.go` for a patch release
that includes uber-go#1215.
JacobOaks added a commit that referenced this pull request Jun 25, 2024
Update changelog and `version.go` for a patch release that includes
#1215.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Opt-out of signal handling
3 participants