Repository: blackbeam/mysql_async (language: rust)

Issue:
Dropping pooled connection when panicking leads to resource leak
As of now, `Drop` implementation for pooled connections begins with this:

https://github.com/blackbeam/mysql_async/blob/21ff2906e6273ccc89056a84a4c11eb7adec7977/src/conn/pool/mod.rs#L285-L297

This means that if connection is dropped when panicking, it's not released to pool in any way.

I know that panic is not the proper way to handle errors in Rust, but in practice, at least some of the web runtimes/frameworks (Rocket for example) have panic handlers, so they intercept them and return an internal server error. Which means that when an application has a bug that causes panic, it will not crash, but rather eventually (100 crashing requests by default) deadlock on taking new connection from the pool.


Discussion / hints:

Fix patch:
diff --git a/src/conn/pool/mod.rs b/src/conn/pool/mod.rs
index 34303845..16888050 100644
--- a/src/conn/pool/mod.rs
+++ b/src/conn/pool/mod.rs
@@ -285,6 +285,11 @@ impl Pool {
 impl Drop for Conn {
     fn drop(&mut self) {
         if std::thread::panicking() {
+            // Try to decrease the number of existing connections.
+            if let Some(pool) = self.inner.pool.take() {
+                pool.cancel_connection();
+            }
+
             return;
         }
 


Test patch: