diff --git a/core/txpool/locals/journal.go b/core/txpool/locals/journal.go index 7b89693495..fb2376b3c0 100644 --- a/core/txpool/locals/journal.go +++ b/core/txpool/locals/journal.go @@ -112,8 +112,18 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error { } } log.Info("Loaded local transaction journal", "transactions", total, "dropped", dropped) + return failure +} - // Open the journal file for appending +func (journal *journal) setupWriter() error { + if journal.writer != nil { + if err := journal.writer.Close(); err != nil { + return err + } + journal.writer = nil + } + + // Re-open the journal file for appending // Use O_APPEND to ensure we always write to the end of the file sink, err := os.OpenFile(journal.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if err != nil { @@ -121,7 +131,7 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error { } journal.writer = sink - return failure + return nil } // insert adds the specified transaction to the local disk journal. diff --git a/core/txpool/locals/tx_tracker.go b/core/txpool/locals/tx_tracker.go index e08384ce71..91edd88ffd 100644 --- a/core/txpool/locals/tx_tracker.go +++ b/core/txpool/locals/tx_tracker.go @@ -185,6 +185,17 @@ func (tracker *TxTracker) loop() { tracker.TrackAll(transactions) return nil }) + + // Setup the writer for the upcoming transactions. Lock to prevent + // journal.setupWriter <-> journal.insert (via TrackAll) conflicts. + tracker.mu.Lock() + if err := tracker.journal.setupWriter(); err != nil { + log.Error("Failed to setup the journal writer", "err", err) + tracker.mu.Unlock() + return + } + tracker.mu.Unlock() + defer tracker.journal.close() } var (