Skip to content

Commit df383ad

Browse files
authored
ethclient: ensure returned subscription is nil on error (#26976)
1 parent 792d893 commit df383ad

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

ethclient/ethclient.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,14 @@ func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, err
320320
// SubscribeNewHead subscribes to notifications about the current blockchain head
321321
// on the given channel.
322322
func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
323-
return ec.c.EthSubscribe(ctx, ch, "newHeads")
323+
sub, err := ec.c.EthSubscribe(ctx, ch, "newHeads")
324+
if err != nil {
325+
// Defensively prefer returning nil interface explicitly on error-path, instead
326+
// of letting default golang behavior wrap it with non-nil interface that stores
327+
// nil concrete type value.
328+
return nil, err
329+
}
330+
return sub, nil
324331
}
325332

326333
// State Access
@@ -389,7 +396,14 @@ func (ec *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuer
389396
if err != nil {
390397
return nil, err
391398
}
392-
return ec.c.EthSubscribe(ctx, ch, "logs", arg)
399+
sub, err := ec.c.EthSubscribe(ctx, ch, "logs", arg)
400+
if err != nil {
401+
// Defensively prefer returning nil interface explicitly on error-path, instead
402+
// of letting default golang behavior wrap it with non-nil interface that stores
403+
// nil concrete type value.
404+
return nil, err
405+
}
406+
return sub, nil
393407
}
394408

395409
func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {

0 commit comments

Comments
 (0)