From e7a2f3231ef91656b61e097161e69b0a8af2c957 Mon Sep 17 00:00:00 2001 From: Thibault bui Koechlin Date: Wed, 3 Jun 2020 09:46:08 +0200 Subject: [PATCH] don't fatal is DB is locked. Attempt to fix #60 - but was not able to reproduce --- pkg/sqlite/commit.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/sqlite/commit.go b/pkg/sqlite/commit.go index dfdbd0e4a..16b125699 100644 --- a/pkg/sqlite/commit.go +++ b/pkg/sqlite/commit.go @@ -5,6 +5,8 @@ import ( "sync/atomic" "time" + sqlite3 "github.com/mattn/go-sqlite3" + "github.com/crowdsecurity/crowdsec/pkg/types" log "github.com/sirupsen/logrus" ) @@ -15,6 +17,13 @@ func (c *Context) Flush() error { ret := c.tx.Commit() if ret.Error != nil { + /*if the database is locked, don't overwrite the current transaction*/ + if ret.Error == sqlite3.ErrLocked { + log.Errorf("sqlite commit : db is locked : %s", ret.Error) + return ret.Error + } + /*if it's another error, create a new transaction to avoid locking ourselves in a bad state ?*/ + c.tx = c.Db.Begin() return fmt.Errorf("failed to commit records : %v", ret.Error) } c.tx = c.Db.Begin() @@ -37,7 +46,7 @@ func (c *Context) AutoCommit() { if atomic.LoadInt32(&c.count) != 0 && (atomic.LoadInt32(&c.count)%100 == 0 || time.Since(c.lastCommit) >= 500*time.Millisecond) { if err := c.Flush(); err != nil { - log.Fatalf("failed to flush : %s", err) + log.Errorf("failed to flush : %s", err) } } }