ethclient: ensure returned subscription is nil on error #26976 (#1376)

Co-authored-by: norwnd <112318969+norwnd@users.noreply.github.com>
This commit is contained in:
Daniel Liu 2025-08-22 16:47:37 +08:00 committed by GitHub
parent 0e6d2f4b94
commit 5311a890fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -343,7 +343,14 @@ func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, err
// SubscribeNewHead subscribes to notifications about the current blockchain head
// on the given channel.
func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
return ec.c.EthSubscribe(ctx, ch, "newHeads")
sub, err := ec.c.EthSubscribe(ctx, ch, "newHeads")
if err != nil {
// Defensively prefer returning nil interface explicitly on error-path, instead
// of letting default golang behavior wrap it with non-nil interface that stores
// nil concrete type value.
return nil, err
}
return sub, nil
}
// State Access