From ee3152a92a3f7dbbecbbf5cc2d938941d3f60b73 Mon Sep 17 00:00:00 2001 From: Dan Griffin Date: Thu, 11 Apr 2024 11:54:29 +0200 Subject: [PATCH] Fix leaking connections in fetch_optional (#2647) When using the 'Any' driver with MySQL backend, fetch_optional does not return the connection to the pool if no results are returned from the query. This is due to not all of the packets being read from the underlying stream. This fix continues to read result packets from the stream until they have all been exhausted (just like the normal MySql drivers implementation of fetch_optional). In general, a better refactoring would be to call the MySQL fetch_optional code in the Any driver, rather than re-implementing and duplicating code. --- sqlx-mysql/src/any.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sqlx-mysql/src/any.rs b/sqlx-mysql/src/any.rs index 404c408a9e..2f9f9f8516 100644 --- a/sqlx-mysql/src/any.rs +++ b/sqlx-mysql/src/any.rs @@ -103,8 +103,10 @@ impl AnyConnectionBackend for MySqlConnection { let stream = self.run(query, args, persistent).await?; futures_util::pin_mut!(stream); - if let Some(Either::Right(row)) = stream.try_next().await? { - return Ok(Some(AnyRow::try_from(&row)?)); + while let Some(result) = stream.try_next().await? { + if let Either::Right(row) = result { + return Ok(Some(AnyRow::try_from(&row)?)); + } } Ok(None)