Skip to content

Commit

Permalink
Fix leaking connections in fetch_optional (launchbadge#2647) (launchb…
Browse files Browse the repository at this point in the history
…adge#3194)

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.
  • Loading branch information
danjpgriffin authored and jayy-lmao committed Jun 6, 2024
1 parent 8422652 commit 207ed99
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sqlx-mysql/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 207ed99

Please sign in to comment.