Skip to content

Commit

Permalink
CBG-4462: Avoid leaking ISGR StatsReporter goroutine underneath recon…
Browse files Browse the repository at this point in the history
…necting replicator (#7355)

* Move StatsReporter goroutine lifecycle up into Replicator start instead of replicator connect

* Address PR feedback - pass context explicitly into StatusReporter
  • Loading branch information
bbrks committed Feb 5, 2025
1 parent 34d1b16 commit 1023c98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions db/active_replicator_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (a *activeReplicatorCommon) _disconnect() error {
return nil
}

// _stop aborts any replicator processes that run outside of a running replication (e.g: async reconnect handling)
// _stop aborts any replicator processes that run outside of a running replication connection (e.g: async reconnect handling, statsreporter)
func (a *activeReplicatorCommon) _stop() {
if a.ctxCancel != nil {
base.TracefCtx(a.ctx, base.KeyReplicate, "cancelling context on activeReplicatorCommon in _stop()")
Expand Down Expand Up @@ -369,8 +369,8 @@ func (a *activeReplicatorCommon) _publishStatus() {
}
}

func (arc *activeReplicatorCommon) startStatusReporter() error {
go func(ctx context.Context) {
func (arc *activeReplicatorCommon) startStatusReporter(ctx context.Context) error {
go func() {
ticker := time.NewTicker(arc.config.CheckpointInterval)
defer ticker.Stop()
for {
Expand All @@ -386,7 +386,7 @@ func (arc *activeReplicatorCommon) startStatusReporter() error {
return
}
}
}(arc.ctx)
}()
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions db/active_replicator_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (apr *ActivePullReplicator) Start(ctx context.Context) error {
logCtx := base.CorrelationIDLogCtx(ctx, apr.config.ID+"-"+string(ActiveReplicatorTypePull))
apr.ctx, apr.ctxCancel = context.WithCancel(logCtx)

if err := apr.startStatusReporter(apr.ctx); err != nil {
return err
}

err := apr._connect()
if err != nil {
_ = apr.setError(err)
Expand Down Expand Up @@ -91,10 +95,6 @@ func (apr *ActivePullReplicator) _connect() error {
base.ErrorfCtx(apr.ctx, "Pull replicator ID:%s running with revocations enabled but target does not support revocations. Sync Gateway 3.0 required.", apr.config.ID)
}

if err := apr.startStatusReporter(); err != nil {
return err
}

apr.setState(ReplicationStateRunning)

return nil
Expand Down
8 changes: 4 additions & 4 deletions db/active_replicator_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (apr *ActivePushReplicator) Start(ctx context.Context) error {
apr.config.ID+"-"+string(ActiveReplicatorTypePush))
apr.ctx, apr.ctxCancel = context.WithCancel(logCtx)

if err := apr.startStatusReporter(apr.ctx); err != nil {
return err
}

err := apr._connect()
if err != nil {
_ = apr.setError(err)
Expand Down Expand Up @@ -94,10 +98,6 @@ func (apr *ActivePushReplicator) _connect() error {
}
}

if err := apr.startStatusReporter(); err != nil {
return err
}

apr.setState(ReplicationStateRunning)
return nil
}
Expand Down

0 comments on commit 1023c98

Please sign in to comment.