From 0ae2f7f92758d468a24f05dfe95ffaa97ed4513c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 26 Oct 2015 14:02:41 +0000 Subject: [PATCH] Avoid endless loop when cannot open sqlite db. --- src/libstat/backends/sqlite3_backend.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index a978f5753..47318d766 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -443,6 +443,8 @@ rspamd_sqlite3_opendb (rspamd_mempool_t *pool, gpointer tk_conf; gsize sz = 0; gchar *tok_conf_encoded; + gint ret, ntries = 0; + const gint max_tries = 100; struct timespec sleep_ts = { .tv_sec = 0, .tv_nsec = 1000000 @@ -471,11 +473,21 @@ rspamd_sqlite3_opendb (rspamd_mempool_t *pool, /* Check tokenizer configuration */ - while (rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, - RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL) != SQLITE_OK) { + while ((ret = rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, + RSPAMD_STAT_BACKEND_TRANSACTION_START_EXCL)) == SQLITE_BUSY && + ++ntries <= max_tries) { nanosleep (&sleep_ts, NULL); } + if (ret != SQLITE_OK) { + msg_err_pool ("failed to stard transaction: %d, %s", ret, + sqlite3_errmsg (bk->sqlite)); + sqlite3_close (bk->sqlite); + g_slice_free1 (sizeof (*bk), bk); + + return NULL; + } + if (rspamd_sqlite3_run_prstmt (pool, bk->sqlite, bk->prstmt, RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz, &tk_conf) != SQLITE_OK || sz == 0) { -- 2.39.5