Skip to content

Commit 2596070

Browse files
committed
Fix oldest non-terminal tx db query deadlock
1 parent 1ef1887 commit 2596070

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

pkg/txmgr/evm_tx_store.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,10 +1645,15 @@ func (o *evmTxStore) OldestNonTerminalTxAgeSeconds(ctx context.Context, chainID
16451645
ctx, cancel = o.stopCh.Ctx(ctx)
16461646
defer cancel()
16471647
err = o.q.GetContext(ctx, &seconds, `
1648-
SELECT GREATEST(0::float8, COALESCE(EXTRACT(EPOCH FROM (NOW() - MIN(created_at))), 0::float8))
1649-
FROM evm.txes
1650-
WHERE evm_chain_id = $1 AND state NOT IN ('finalized', 'fatal_error')`,
1648+
SELECT GREATEST(0::float8, COALESCE(EXTRACT(EPOCH FROM (NOW() - t.created_at)), 0::float8)) AS seconds
1649+
FROM evm.txes AS t
1650+
WHERE t.evm_chain_id = $1 AND t.state NOT IN ('finalized', 'fatal_error')
1651+
ORDER BY t.created_at ASC NULLS LAST
1652+
LIMIT 1`,
16511653
chainID.String())
1654+
if errors.Is(err, sql.ErrNoRows) {
1655+
return 0, nil
1656+
}
16521657
if err != nil {
16531658
return 0, fmt.Errorf("failed to OldestNonTerminalTxAgeSeconds: %w", err)
16541659
}

pkg/txmgr/finalizer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ var (
3737
const (
3838
processHeadTimeout = 10 * time.Minute
3939
attemptsCacheRefreshThreshold = 5
40+
// Bound metrics query time so a contended shared test DB cannot block the finalizer head path.
41+
oldestNonTerminalTxAgeMetricQueryTimeout = 5 * time.Second
4042
)
4143

4244
type finalizerTxStore interface {
@@ -213,7 +215,9 @@ func (f *evmFinalizer) ProcessHead(ctx context.Context, head *types.Head) error
213215
}
214216

215217
func (f *evmFinalizer) observeOldestNonTerminalTxAge(ctx context.Context) {
216-
age, err := f.txStore.OldestNonTerminalTxAgeSeconds(ctx, f.chainID)
218+
ctxObs, cancel := context.WithTimeout(ctx, oldestNonTerminalTxAgeMetricQueryTimeout)
219+
defer cancel()
220+
age, err := f.txStore.OldestNonTerminalTxAgeSeconds(ctxObs, f.chainID)
217221
if err != nil {
218222
f.lggr.Errorw("Failed to load oldest non-terminal transaction age for metrics", "err", err)
219223
return

0 commit comments

Comments
 (0)