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 race condition (also a regression of the PR 19139) #19221

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ahrtr
Copy link
Member

@ahrtr ahrtr commented Jan 17, 2025

Fix #19172

Please review this PR commit by commit.

Three high level thoughts,

  • There are multiple levels of goroutines. The grandparent ( StartEtcd ) creates multiple child goroutines ( client listeners, peer listeners and metrics listeners). The client listeners creates some grandson goroutines (see the first commit). Each one should only manage their immediate children.
  • For sync.WaitGroup, we should always call wg.Add and wg.Wait in the same goroutine.
  • When stopping etcd, we should close all listeners and context firstly, afterwards close the etcdserver.

cc @serathius @fuweid @ivanvc @jmhbnz @joshuazh-x

Copy link

codecov bot commented Jan 17, 2025

Codecov Report

Attention: Patch coverage is 77.27273% with 5 lines in your changes missing coverage. Please review.

Project coverage is 68.81%. Comparing base (0dcd015) to head (201568a).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
server/embed/serve.go 81.25% 1 Missing and 2 partials ⚠️
server/embed/etcd.go 66.66% 2 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
server/embed/etcd.go 76.19% <66.66%> (+0.33%) ⬆️
server/embed/serve.go 59.38% <81.25%> (+1.58%) ⬆️

... and 21 files with indirect coverage changes

@@            Coverage Diff             @@
##             main   #19221      +/-   ##
==========================================
- Coverage   68.82%   68.81%   -0.02%     
==========================================
  Files         420      420              
  Lines       35649    35664      +15     
==========================================
+ Hits        24536    24541       +5     
- Misses       9692     9697       +5     
- Partials     1421     1426       +5     

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0dcd015...201568a. Read the comment docs.

@ahrtr ahrtr force-pushed the race-20250117 branch 2 times, most recently from c76bbeb to b1e5ebc Compare January 17, 2025 19:29
@ahrtr
Copy link
Member Author

ahrtr commented Jan 17, 2025

@fuweid @ivanvc @jmhbnz @serathius

This PR fixed a regression caused by #19139. So let's get this merged and backport to 3.5 and probably 3.4. We need to get it included in 3.5.18

@ahrtr
Copy link
Member Author

ahrtr commented Jan 17, 2025

/test pull-etcd-integration-1-cpu-arm64

@@ -411,6 +411,16 @@ func (e *Etcd) Close() {
close(e.stopc)
})

for i := range e.Clients {
if e.Clients[i] != nil {
Copy link
Member

Choose a reason for hiding this comment

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

The Clients here is about net listener. If we close it at the beginning, all the connections will be closed, and it seems like we are unable to drain all the inflight requests gracefully. The stopServers function is used to gracefully shutdown.

Copy link
Member Author

Choose a reason for hiding this comment

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

The Clients here is about net listener. If we close it at the beginning, all the connections will be closed,

It's intentional.

  • We should stop accepting any new connections immediately.
  • It won't close the already established connections

Copy link
Member

Choose a reason for hiding this comment

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

I saw you reverts that change. would you mind update the description as well? thanks

When stopping etcd, we should close all listeners and context firstly, afterwards close the etcdserver.

Copy link
Member Author

Choose a reason for hiding this comment

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

The description has already been updated. Where did you get the description?

Copy link
Member

Choose a reason for hiding this comment

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

In the pull request's description

@serathius
Copy link
Member

Hard to review without loading a lot of context, it's not the first time we are having problems with shutdown. I think the problem is lack of high level vision on shutdown protocol for server, and what sub routines should do to follow it, and why everything works together.

@ahrtr could you add a comment describing the shutdown protocol you have in mind? It should make it easier to review and be useful for the future.

@ahrtr
Copy link
Member Author

ahrtr commented Jan 20, 2025

@ahrtr could you add a comment describing the shutdown protocol you have in mind? It should make it easier to review and be useful for the future.

Done. Please see the last commit. cc @fuweid @ivanvc @jmhbnz @serathius

@ahrtr
Copy link
Member Author

ahrtr commented Jan 20, 2025

/test pull-etcd-integration-1-cpu-arm64

ahrtr added 2 commits January 21, 2025 10:52
…te before it returns

Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
… the errc

Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
server/embed/etcd.go Outdated Show resolved Hide resolved
Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
Copy link
Member

@fuweid fuweid left a comment

Choose a reason for hiding this comment

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

LGTM

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahrtr, fuweid

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Race condition when closing the embedded etcd
4 participants