From 8ce36d915fb37a280a708959e745bf3cb6f8e4e9 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Fri, 26 Jul 2024 11:08:46 +0200 Subject: [PATCH 1/2] Change: Retry on deadlocks in sql_x The sql_x function (and thus functions using it like sql_string) now also retries executing the statement in case of a deadlock like the sql function. This addresses an issue with queries of the auth_cache aborting if there are too many concurrent ones. --- src/sql.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/sql.c b/src/sql.c index efae4de7b..72043d14b 100644 --- a/src/sql.c +++ b/src/sql.c @@ -208,7 +208,7 @@ sql_insert (const char *string) * * @return 0 success, 1 gave up (even when retry given), * 2 reserved (lock unavailable), 3 unique constraint violation, - * -1 error. + * 4 deadlock, -1 error. */ int sqlv (int retry, char* sql, va_list args) @@ -274,13 +274,14 @@ sql (char* sql, ...) continue; else if (ret == 4) { - if (deadlock_amount++ > DEADLOCK_THRESHOLD) - { - g_warning("%s: %d deadlocks detected, waiting and retrying %s", __func__, deadlock_amount, sql); - } - gvm_usleep (DEADLOCK_SLEEP); - continue; - } + if (deadlock_amount++ > DEADLOCK_THRESHOLD) + { + g_warning("%s: %d deadlocks detected, waiting and retrying %s", + __func__, deadlock_amount, sql); + } + gvm_usleep (DEADLOCK_SLEEP); + continue; + } else if (ret) abort(); break; @@ -355,6 +356,7 @@ int sql_x (char* sql, va_list args, sql_stmt_t** stmt_return) { int ret; + unsigned int deadlock_amount = 0; assert (stmt_return); @@ -392,6 +394,16 @@ sql_x (char* sql, va_list args, sql_stmt_t** stmt_return) sql_finalize (*stmt_return); continue; } + else if (ret == -5) + { + if (deadlock_amount++ > DEADLOCK_THRESHOLD) + { + g_warning("%s: %d deadlocks detected, waiting and retrying %s", + __func__, deadlock_amount, sql); + } + gvm_usleep (DEADLOCK_SLEEP); + continue; + } break; } assert (ret == 1); From 9ac894698595406c1cd56c721be411a3f07544ca Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Mon, 29 Jul 2024 12:16:49 +0200 Subject: [PATCH 2/2] Remove unnecessary else in sql_x Co-authored-by: Matt Mundell <32057441+mattmundell@users.noreply.github.com> --- src/sql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sql.c b/src/sql.c index 72043d14b..33b0de1ca 100644 --- a/src/sql.c +++ b/src/sql.c @@ -394,7 +394,7 @@ sql_x (char* sql, va_list args, sql_stmt_t** stmt_return) sql_finalize (*stmt_return); continue; } - else if (ret == -5) + if (ret == -5) { if (deadlock_amount++ > DEADLOCK_THRESHOLD) {