Skip to content

Commit

Permalink
Change: Retry on deadlocks in sql_x
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timopollmeier authored and a-h-abdelsalam committed Aug 21, 2024
1 parent 57728f5 commit 8ce36d9
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8ce36d9

Please sign in to comment.