Skip to content

Commit

Permalink
Use sync.Once to prevent double closing
Browse files Browse the repository at this point in the history
  • Loading branch information
emlautarom1 committed May 29, 2024
1 parent db9b425 commit c6f4b7f
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions core/safeclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ type SafeEthClient struct {
logger logging.Logger
rpcUrl string
closeC chan struct{}
closed bool
headerTimeout time.Duration
logResubInterval time.Duration
blockChunkSize uint64
blockMaxRange uint64

createClient func(string, logging.Logger) (eth.Client, error)
onceClose sync.Once
}

func NewSafeEthClient(rpcUrl string, logger logging.Logger, opts ...SafeEthClientOption) (*SafeEthClient, error) {
Expand Down Expand Up @@ -301,15 +301,11 @@ func (c *SafeEthClient) SubscribeFilterLogs(ctx context.Context, q ethereum.Filt
}

func (c *SafeEthClient) Close() {
if c.closed {
return
}

close(c.closeC)
c.wg.Wait()
c.logger.Info("SafeEthClient closed")

c.closed = true
c.onceClose.Do(func() {
close(c.closeC)
c.wg.Wait()
c.logger.Info("SafeEthClient closed")
})
}

func (c *SafeEthClient) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
Expand Down

0 comments on commit c6f4b7f

Please sign in to comment.