-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 a race condition in the TCP input #13038
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6367711
Fix a race condition in the TCP input
ph f9e6499
Add changelog
ph 04f7b61
add pr number
ph cac1647
Use a CloseRef instead of handling close inside the client.
ph a01c6b0
use handler instead of client which represent the actual work done.
ph 45030d8
Make CloseRef public, some code reorg to make a new package.
ph 7e328bc
missing header
ph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package tcp | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// CloserFunc is the function called by the Closer on `Close()`. | ||
type CloserFunc func() | ||
|
||
// ErrClosed is returned when the Closer is closed. | ||
var ErrClosed = errors.New("closer is closed") | ||
|
||
// CloseRef implements a subset of the context.Context interface and it's use to synchronize | ||
// the shutdown of multiple go-routines. | ||
type CloseRef interface { | ||
Done() <-chan struct{} | ||
Err() error | ||
} | ||
|
||
// Closer implements a shutdown strategy when dealing with multiples go-routines, it creates a tree | ||
// of Closer, when you call `Close()` on a parent the `Close()` method will be called on the current | ||
// closer and any of the childs it may have and will remove the current node from the parent. | ||
// | ||
// NOTE: The `Close()` is reentrant but will propage the close only once. | ||
type Closer struct { | ||
mu sync.Mutex | ||
done chan struct{} | ||
err error | ||
parent *Closer | ||
children map[*Closer]struct{} | ||
callback CloserFunc | ||
} | ||
|
||
// Close closes the closes and propagates the close to any child, on close the close callback will | ||
// be called, this can be used for custom cleanup like closing a TCP socket. | ||
func (c *Closer) Close() { | ||
c.mu.Lock() | ||
if c.err != nil { | ||
c.mu.Unlock() | ||
return | ||
} | ||
|
||
if c.callback != nil { | ||
c.callback() | ||
} | ||
|
||
close(c.done) | ||
|
||
// propagate close to children. | ||
if c.children != nil { | ||
for child := range c.children { | ||
child.Close() | ||
} | ||
c.children = nil | ||
} | ||
|
||
c.err = ErrClosed | ||
c.mu.Unlock() | ||
|
||
if c.parent != nil { | ||
c.removeChild(c) | ||
} | ||
} | ||
|
||
// Done returns the synchronization channel, the channel will be closed if `Close()` was called on | ||
// the current node or any parent it may have. | ||
func (c *Closer) Done() <-chan struct{} { | ||
return c.done | ||
} | ||
|
||
// Err returns an error if the Closer was already closed. | ||
func (c *Closer) Err() error { | ||
c.mu.Lock() | ||
err := c.err | ||
c.mu.Unlock() | ||
return err | ||
} | ||
|
||
func (c *Closer) removeChild(child *Closer) { | ||
c.mu.Lock() | ||
delete(c.children, child) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *Closer) addChild(child *Closer) { | ||
c.mu.Lock() | ||
if c.children == nil { | ||
c.children = make(map[*Closer]struct{}) | ||
} | ||
c.children[child] = struct{}{} | ||
c.mu.Unlock() | ||
} | ||
|
||
// WithCloser wraps a new closer into a child of an existing closer. | ||
func WithCloser(parent *Closer, fn CloserFunc) *Closer { | ||
child := &Closer{ | ||
done: make(chan struct{}), | ||
parent: parent, | ||
callback: fn, | ||
} | ||
parent.addChild(child) | ||
return child | ||
} | ||
|
||
// NewCloser creates a new Closer. | ||
func NewCloser(fn CloserFunc) *Closer { | ||
return &Closer{ | ||
done: make(chan struct{}), | ||
callback: fn, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't see any code that can set
c.err
besides close. That is, one could replace the mutex with a simpleatomic.Bool
and implement the body like:if !c.active.Load() { return ErrClosed }; return nil
.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.
We still have to lock the children map though? I can revisit if when we move it to external repo.