From 45a8358f1ea163ddf3ed9e5c3c49bc913963b0e9 Mon Sep 17 00:00:00 2001 From: welkin22 Date: Fri, 6 Sep 2024 16:00:53 +0800 Subject: [PATCH 1/5] fix: done chan stuck when close --- op-service/sources/l1_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/op-service/sources/l1_client.go b/op-service/sources/l1_client.go index a0576eac3..14c146553 100644 --- a/op-service/sources/l1_client.go +++ b/op-service/sources/l1_client.go @@ -84,7 +84,7 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con preFetchReceiptsOnce: sync.Once{}, preFetchReceiptsStartBlockChan: make(chan uint64, 1), maxConcurrentRequests: config.MaxConcurrentRequests, - done: make(chan struct{}), + done: make(chan struct{}, 1), }, nil } From dc8fdf04658ab54842c057512a889224306ba993 Mon Sep 17 00:00:00 2001 From: welkin22 Date: Sun, 8 Sep 2024 16:57:38 +0800 Subject: [PATCH 2/5] fix: done chan stuck when close --- op-service/sources/l1_client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/op-service/sources/l1_client.go b/op-service/sources/l1_client.go index 14c146553..e2359a843 100644 --- a/op-service/sources/l1_client.go +++ b/op-service/sources/l1_client.go @@ -84,7 +84,7 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con preFetchReceiptsOnce: sync.Once{}, preFetchReceiptsStartBlockChan: make(chan uint64, 1), maxConcurrentRequests: config.MaxConcurrentRequests, - done: make(chan struct{}, 1), + done: make(chan struct{}), }, nil } @@ -259,6 +259,6 @@ func (s *L1Client) ClearReceiptsCacheBefore(blockNumber uint64) { } func (s *L1Client) Close() { - s.done <- struct{}{} + close(s.done) s.EthClient.Close() } From 1f06dd5cc4e8410fb7b86082564a65cbcacb544c Mon Sep 17 00:00:00 2001 From: welkin22 Date: Sun, 8 Sep 2024 18:04:20 +0800 Subject: [PATCH 3/5] fix: done chan stuck when close --- op-service/sources/l1_client.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/op-service/sources/l1_client.go b/op-service/sources/l1_client.go index e2359a843..64abe56bf 100644 --- a/op-service/sources/l1_client.go +++ b/op-service/sources/l1_client.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" "sync" + "sync/atomic" "time" "github.com/ethereum/go-ethereum" @@ -62,9 +63,12 @@ type L1Client struct { l1BlockRefsCache *caching.LRUCache[common.Hash, eth.L1BlockRef] //ensure pre-fetch receipts only once - preFetchReceiptsOnce sync.Once + preFetchReceiptsOnce sync.Once + isPreFetchReceiptsRunning atomic.Bool //start block for pre-fetch receipts preFetchReceiptsStartBlockChan chan uint64 + preFetchReceiptsClosedChan chan struct{} + //max concurrent requests maxConcurrentRequests int //done chan @@ -140,12 +144,14 @@ func (s *L1Client) GoOrUpdatePreFetchReceipts(ctx context.Context, l1Start uint6 s.preFetchReceiptsStartBlockChan <- l1Start s.preFetchReceiptsOnce.Do(func() { s.log.Info("pre-fetching receipts start", "startBlock", l1Start) + s.isPreFetchReceiptsRunning.Store(true) go func() { var currentL1Block uint64 var parentHash common.Hash for { select { case <-s.done: + s.preFetchReceiptsClosedChan <- struct{}{} s.log.Info("pre-fetching receipts done") return case currentL1Block = <-s.preFetchReceiptsStartBlockChan: @@ -259,6 +265,9 @@ func (s *L1Client) ClearReceiptsCacheBefore(blockNumber uint64) { } func (s *L1Client) Close() { - close(s.done) + if s.isPreFetchReceiptsRunning.Load() { + close(s.done) + <-s.preFetchReceiptsClosedChan + } s.EthClient.Close() } From fa919b305191efea00a0b609f256e7b0bae81a33 Mon Sep 17 00:00:00 2001 From: welkin22 Date: Sun, 8 Sep 2024 18:55:45 +0800 Subject: [PATCH 4/5] fix: done chan stuck when close --- op-service/sources/l1_client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/op-service/sources/l1_client.go b/op-service/sources/l1_client.go index 64abe56bf..e5ea430dc 100644 --- a/op-service/sources/l1_client.go +++ b/op-service/sources/l1_client.go @@ -87,6 +87,7 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con l1BlockRefsCache: caching.NewLRUCache[common.Hash, eth.L1BlockRef](metrics, "blockrefs", config.L1BlockRefsCacheSize), preFetchReceiptsOnce: sync.Once{}, preFetchReceiptsStartBlockChan: make(chan uint64, 1), + preFetchReceiptsClosedChan: make(chan struct{}), maxConcurrentRequests: config.MaxConcurrentRequests, done: make(chan struct{}), }, nil From 812d4ddcf51f68b7b73a4a3e4e5ebca1014e37db Mon Sep 17 00:00:00 2001 From: welkin22 Date: Sun, 8 Sep 2024 22:22:34 +0800 Subject: [PATCH 5/5] fix: done chan stuck when close --- op-service/sources/l1_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/op-service/sources/l1_client.go b/op-service/sources/l1_client.go index e5ea430dc..a1c175e95 100644 --- a/op-service/sources/l1_client.go +++ b/op-service/sources/l1_client.go @@ -152,8 +152,8 @@ func (s *L1Client) GoOrUpdatePreFetchReceipts(ctx context.Context, l1Start uint6 for { select { case <-s.done: - s.preFetchReceiptsClosedChan <- struct{}{} s.log.Info("pre-fetching receipts done") + s.preFetchReceiptsClosedChan <- struct{}{} return case currentL1Block = <-s.preFetchReceiptsStartBlockChan: s.log.Debug("pre-fetching receipts currentL1Block changed", "block", currentL1Block)