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

GODRIVER-3107 Fix leaking rttMonitor.runHellos() routine. #1587

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions x/mongo/driver/topology/rtt_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type rttMonitor struct {
cfg *rttConfig
ctx context.Context
cancelFn context.CancelFunc
started bool
}

var _ driver.RTTMonitor = &rttMonitor{}
Expand All @@ -83,7 +82,6 @@ func (r *rttMonitor) connect() {
r.connMu.Lock()
defer r.connMu.Unlock()

r.started = true
r.closeWg.Add(1)

go func() {
Expand All @@ -97,10 +95,6 @@ func (r *rttMonitor) disconnect() {
r.connMu.Lock()
defer r.connMu.Unlock()

if !r.started {
return
}

r.cancelFn()

// Wait for the existing connection to complete.
Expand Down
9 changes: 6 additions & 3 deletions x/mongo/driver/topology/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type Server struct {

processErrorLock sync.Mutex
rttMonitor *rttMonitor
monitorOnce *sync.Once
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since Server is always passed as a pointer, this sync.Once can be a non-pointer type (like sync.Mutex above). If it is ever copied, the go vet linter will cause an error.

}

// updateTopologyCallback is a callback used to create a server that should be called when the parent Topology instance
Expand Down Expand Up @@ -168,6 +169,8 @@ func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...Serv
subscribers: make(map[uint64]chan description.Server),
globalCtx: globalCtx,
globalCtxCancel: globalCtxCancel,

monitorOnce: new(sync.Once),
}
s.desc.Store(description.NewDefaultServer(addr))
rttCfg := &rttConfig{
Expand Down Expand Up @@ -285,10 +288,10 @@ func (s *Server) Disconnect(ctx context.Context) error {
close(s.done)
s.cancelCheck()

s.rttMonitor.disconnect()
s.pool.close(ctx)

s.closewg.Wait()
s.rttMonitor.disconnect()
Copy link
Collaborator Author

@qingyang-hu qingyang-hu Mar 29, 2024

Choose a reason for hiding this comment

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

It does not cause any actual impact whether or not this line is moved. I put it here as a "fail-safe" that disconnects after the update() exits, so rttMonitor.connect() is more likely to be called beforehand.

atomic.StoreInt64(&s.state, serverDisconnected)

return nil
Expand Down Expand Up @@ -661,8 +664,8 @@ func (s *Server) update() {
transitionedFromNetworkError := desc.LastError != nil && unwrapConnectionError(desc.LastError) != nil &&
previousDescription.Kind != description.Unknown

if isStreamingEnabled(s) && isStreamable(s) && !s.rttMonitor.started {
s.rttMonitor.connect()
if isStreamingEnabled(s) && isStreamable(s) {
s.monitorOnce.Do(s.rttMonitor.connect)
}

if isStreamable(s) || connectionIsStreaming || transitionedFromNetworkError {
Expand Down
Loading