Skip to content

Commit 3ca58f4

Browse files
committed
fix(odbc): implement Drop for OdbcConnection to properly shut down worker threads
This prevents worker threads from hanging when ODBC connections are dropped, allowing tests to exit properly instead of hanging indefinitely.
1 parent 832caea commit 3ca58f4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

sqlx-core/src/odbc/connection/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,10 @@ impl Connection for OdbcConnection {
6464
false
6565
}
6666
}
67+
68+
impl Drop for OdbcConnection {
69+
fn drop(&mut self) {
70+
// Send shutdown command to worker thread to prevent resource leak
71+
self.worker.shutdown_sync();
72+
}
73+
}

sqlx-core/src/odbc/connection/worker.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ impl ConnectionWorker {
8080
send_command_and_await(&self.command_tx, Command::Shutdown { tx }, rx).await
8181
}
8282

83+
pub(crate) fn shutdown_sync(&mut self) {
84+
// Send shutdown command without waiting for response
85+
// Use try_send to avoid any potential blocking in Drop
86+
let (tx, _rx) = oneshot::channel();
87+
let _ = self.command_tx.try_send(Command::Shutdown { tx });
88+
89+
// Don't aggressively drop the channel to avoid SendError panics
90+
// The worker thread will exit when it processes the Shutdown command
91+
}
92+
8393
pub(crate) async fn begin(&mut self) -> Result<(), Error> {
8494
let (tx, rx) = oneshot::channel();
8595
send_transaction_command(&self.command_tx, Command::Begin { tx }, rx).await
@@ -156,11 +166,12 @@ fn worker_thread_main(
156166
}
157167

158168
// Process commands
159-
for cmd in rx {
169+
while let Ok(cmd) = rx.recv() {
160170
if !process_command(cmd, &conn) {
161171
break;
162172
}
163173
}
174+
// Channel disconnected or shutdown command received, worker thread exits
164175
}
165176

166177
fn establish_connection(options: &OdbcConnectOptions) -> Result<OdbcConnection, Error> {

0 commit comments

Comments
 (0)