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

Serve() should not return error on Stop() or GracefulStop() #1485

Merged
merged 3 commits into from
Oct 18, 2017

Conversation

sunshangpp
Copy link
Contributor

As #1372 reported, currently Serve() function returns non-nil error when Stop() or GracefulStop() is called.

This is because both Stop and GracefulStop close the listener (net.Listener) directly, which is still being blocked on the Accept() call in Serve(). We can use a quit channel to indicate that the grpc server is being stopped on demand and return nil instead of an error.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If your company signed a CLA, they designated a Point of Contact who decides which employees are authorized to participate. You may need to contact the Point of Contact for your company and ask to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

@sunshangpp
Copy link
Contributor Author

I signed it!

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

@googlebot
Copy link

CLAs look good, thanks!

@menghanl
Copy link
Contributor

Sorry for the late reply, and thanks for the change.

The changes in this PR look good.
But #1372 is actually about making Serve() not return when GracefulStop() is called, and block until the server is really stopped.
It's not just to make Serve() return nil error or not.
Do you want to try to solve this in another PR?

@sunshangpp
Copy link
Contributor Author

Hi @menghanl thanks for the reply!

Originally I hesitated to block Serve because this can be easily done in the app code so we have more flexibility. Also, as soon as server.lis is closed, the gRPC server stops Serving new requests, so semantically it's not really serving anymore. But I can totally see the benefits of blocking as well, I'll make the change.

I think it'll be easier for me to do it in the same PR, I will make a new commit!

@sunshangpp sunshangpp force-pushed the gracefulstop-without-error branch 2 times, most recently from b8d6e06 to fbf482e Compare September 26, 2017 05:04
Copy link
Contributor

@menghanl menghanl left a comment

Choose a reason for hiding this comment

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

Can you add some tests for this new behavior?
Thanks!


// If Stop or GracefulStop is called, block until they are done and return nil
select {
case <-s.quit:
Copy link
Contributor

Choose a reason for hiding this comment

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

A server can actually Serve on multiple listeners. This only works in the case of one Serve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah good catch, will fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@menghanl Fixed by using a sync.Once to close the channels, this way when s.quit or s.done is closed, all go-routines running Serve() will be unblocked.


err = server.Serve(lis)
if err != nil {
t.Fatalf("Serve() returned non-nil error on GracefulStop: %v", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hey @menghanl I only added test for testing Serve() should return nil on GracefulStop().

I found it tricky to actually test the order of Serve and GracefulStop, the only way I can think of is to make both functions send a message to a shared channel when they return, and compare the order of the messages in the channel, but this requires modifying Serve and GracefulStop themselves.

Copy link
Contributor

Choose a reason for hiding this comment

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

One way to test this is to make GracefulStop() block by having a pending streaming RPC, and check that Serve() doesn't return.
We have end2end tests that does similar things.

@sunshangpp sunshangpp force-pushed the gracefulstop-without-error branch 3 times, most recently from f756b42 to e2c3696 Compare October 10, 2017 01:21
@sunshangpp
Copy link
Contributor Author

hey @menghanl mind taking another look at this? thanks!

@thelinuxfoundation
Copy link

Thank you for your pull request. Before we can look at your contribution, we need to ensure all contributors are covered by a Contributor License Agreement.

After the following items are addressed, please respond with a new comment here, and the automated system will re-verify.

Regards,
The Linux Foundation CLA GitHub bot

@sunshangpp
Copy link
Contributor Author

I signed

Copy link
Contributor

@menghanl menghanl left a comment

Choose a reason for hiding this comment

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

LGTM.


err = server.Serve(lis)
if err != nil {
t.Fatalf("Serve() returned non-nil error on GracefulStop: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

One way to test this is to make GracefulStop() block by having a pending streaming RPC, and check that Serve() doesn't return.
We have end2end tests that does similar things.

@menghanl menghanl merged commit 473b142 into grpc:master Oct 18, 2017
@menghanl
Copy link
Contributor

Thanks for the changes. I'm merging this PR as it has been delayed for a long time.
If you are still interested, you can add the test I mentioned in another PR.
Thanks!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants