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(core/listener): wait listener to shutdown before exit #3775

Merged
merged 2 commits into from
Sep 25, 2024
Merged
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
18 changes: 16 additions & 2 deletions core/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Listener struct {

listenerTimeout time.Duration
cancel context.CancelFunc
closed chan struct{}
}

func NewListener(
Expand Down Expand Up @@ -86,6 +87,7 @@ func NewListener(
listenerTimeout: 5 * blocktime,
metrics: metrics,
chainID: p.chainID,
closed: make(chan struct{}),
}, nil
}

Expand Down Expand Up @@ -115,12 +117,24 @@ func (cl *Listener) Stop(ctx context.Context) error {

cl.cancel()
cl.cancel = nil
return cl.metrics.Close()

select {
case <-cl.closed:
case <-ctx.Done():
return ctx.Err()
}

err = cl.metrics.Close()
if err != nil {
log.Warnw("listener: closing metrics", "err", err)
}
return nil
}

// runSubscriber runs a subscriber to receive event data of new signed blocks. It will attempt to
// resubscribe in case error happens during listening of subscription
func (cl *Listener) runSubscriber(ctx context.Context, sub <-chan types.EventDataSignedBlock) {
defer close(cl.closed)
for {
err := cl.listen(ctx, sub)
if ctx.Err() != nil {
Expand All @@ -129,7 +143,7 @@ func (cl *Listener) runSubscriber(ctx context.Context, sub <-chan types.EventDat
}
if errors.Is(err, errInvalidSubscription) {
// stop node if there is a critical issue with the block subscription
log.Fatalf("listener: %v", err)
log.Fatalf("listener: %v", err) //nolint:gocritic
}

log.Warnw("listener: subscriber error, resubscribing...", "err", err)
Expand Down
Loading