From 99114c487ef97834b13448f1091fcc9c1431edce Mon Sep 17 00:00:00 2001 From: tamirms Date: Mon, 6 Feb 2023 21:26:12 +0000 Subject: [PATCH] Fix rollback errors in logs (#397) During ingestion we can see the following logs: WARN[2023-02-06T21:16:22.733+01:00] could not rollback ingest write transactions error="sql: transaction has already been committed or rolled back" pid=78232 This commit will ignore sql.ErrTxDone errors when rolling back a transaction. --- cmd/soroban-rpc/internal/db/db.go | 9 ++++++++- cmd/soroban-rpc/internal/db/ledger_test.go | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/soroban-rpc/internal/db/db.go b/cmd/soroban-rpc/internal/db/db.go index 9918731a1..3019bb616 100644 --- a/cmd/soroban-rpc/internal/db/db.go +++ b/cmd/soroban-rpc/internal/db/db.go @@ -168,7 +168,14 @@ func (w writeTx) Commit(ledgerSeq uint32) error { } func (w writeTx) Rollback() error { - return w.tx.Rollback() + // sql.ErrTxDone is returned when rolling back a transaction which has + // already been committed or rolled back. We can ignore those errors + // because we allow rolling back after commits in defer statements. + if err := w.tx.Rollback(); err == nil || err == sql.ErrTxDone { + return nil + } else { + return err + } } func runMigrations(db *sql.DB, dialect string) error { diff --git a/cmd/soroban-rpc/internal/db/ledger_test.go b/cmd/soroban-rpc/internal/db/ledger_test.go index 7a365ef3e..906ad9ff1 100644 --- a/cmd/soroban-rpc/internal/db/ledger_test.go +++ b/cmd/soroban-rpc/internal/db/ledger_test.go @@ -67,6 +67,8 @@ func TestLedgers(t *testing.T) { assert.NoError(t, err) assert.NoError(t, tx.LedgerWriter().InsertLedger(createLedger(ledgerSequence))) assert.NoError(t, tx.Commit(ledgerSequence)) + // rolling back after a commit is a no-op + assert.NoError(t, tx.Rollback()) } assertLedgerRange(t, reader, 1, 10)