-
Notifications
You must be signed in to change notification settings - Fork 290
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
fix: Only register signal handlers if user intends to use them #1215
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
4c557a6
to
1fd7e16
Compare
There was a problem hiding this 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() |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) {} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK noted
Thanks for updating the PR @MarcoPolo! Mostly LGTM, just a couple small comments. |
@MarcoPolo or @JacobOaks tell me if the review I made, makes no sense 😅 |
app.receivers.notify = func(c chan<- os.Signal, sig ...os.Signal) { | ||
calledNotify = true | ||
} | ||
app.receivers.stopNotify = func(c chan<- os.Signal) {} |
There was a problem hiding this comment.
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.
Update changelog and `version.go` for a patch release that includes uber-go#1215.
Update changelog and `version.go` for a patch release that includes #1215.
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.