From 3c2471e2dc259a7072494e97324354589817dc84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Gon=C3=A7alves?= Date: Wed, 11 Oct 2023 20:03:53 -0300 Subject: [PATCH] fix: sqlx::macro db cleanup race condition by adding a margin to current timestamp (#2640) * fix: sqlx::macro db cleanup race condition by adding a margin to current timestamp * feat: increase margin to 2 seconds --- sqlx-mysql/src/testing/mod.rs | 4 +++- sqlx-postgres/src/testing/mod.rs | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/sqlx-mysql/src/testing/mod.rs b/sqlx-mysql/src/testing/mod.rs index 0f7e27ada3..6c25100f0d 100644 --- a/sqlx-mysql/src/testing/mod.rs +++ b/sqlx-mysql/src/testing/mod.rs @@ -176,11 +176,13 @@ async fn test_context(args: &TestArgs) -> Result, Error> { } async fn do_cleanup(conn: &mut MySqlConnection, created_before: Duration) -> Result { + // since SystemTime is not monotonic we added a little margin here to avoid race conditions with other threads + let created_before_as_secs = created_before.as_secs() - 2; let delete_db_ids: Vec = query_scalar( "select db_id from _sqlx_test_databases \ where created_at < from_unixtime(?)", ) - .bind(&created_before.as_secs()) + .bind(&created_before_as_secs) .fetch_all(&mut *conn) .await?; diff --git a/sqlx-postgres/src/testing/mod.rs b/sqlx-postgres/src/testing/mod.rs index 1dfacecbe4..2d2f203b23 100644 --- a/sqlx-postgres/src/testing/mod.rs +++ b/sqlx-postgres/src/testing/mod.rs @@ -183,7 +183,8 @@ async fn test_context(args: &TestArgs) -> Result, Error> { } async fn do_cleanup(conn: &mut PgConnection, created_before: Duration) -> Result { - let created_before = i64::try_from(created_before.as_secs()).unwrap(); + // since SystemTime is not monotonic we added a little margin here to avoid race conditions with other threads + let created_before = i64::try_from(created_before.as_secs()).unwrap() - 2; let delete_db_names: Vec = query_scalar( "select db_name from _sqlx_test.databases \