From 275eca80172c3e1633817db05b9e905dde49b5f2 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 19 Jan 2026 01:35:56 +0900 Subject: [PATCH] Drop for PySSLSocket --- crates/stdlib/src/ssl.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/stdlib/src/ssl.rs b/crates/stdlib/src/ssl.rs index 6c62a24397b..27d606358a1 100644 --- a/crates/stdlib/src/ssl.rs +++ b/crates/stdlib/src/ssl.rs @@ -3503,9 +3503,9 @@ mod _ssl { } // Check if connection has been shut down - // After unwrap()/shutdown(), read operations should fail with SSLError + // Only block after shutdown is COMPLETED, not during shutdown process let shutdown_state = *self.shutdown_state.lock(); - if shutdown_state != ShutdownState::NotStarted { + if shutdown_state == ShutdownState::Completed { return Err(vm .new_os_subtype_error( PySSLError::class(&vm.ctx).to_owned(), @@ -3677,7 +3677,8 @@ mod _ssl { } // Check shutdown state - if *self.shutdown_state.lock() != ShutdownState::NotStarted { + // Only block after shutdown is COMPLETED, not during shutdown process + if *self.shutdown_state.lock() == ShutdownState::Completed { return Err(vm .new_os_subtype_error( PySSLError::class(&vm.ctx).to_owned(), @@ -4138,6 +4139,8 @@ mod _ssl { } } + // Set shutdown_state first to ensure atomic visibility + // This prevents read/write race conditions during shutdown *self.shutdown_state.lock() = ShutdownState::Completed; *self.connection.lock() = None; return Ok(self.sock.clone()); @@ -4399,6 +4402,21 @@ mod _ssl { } } + // Clean up SSL socket resources on drop + impl Drop for PySSLSocket { + fn drop(&mut self) { + // Clear connection state + let _ = self.connection.lock().take(); + + // Clear pending buffers + self.pending_tls_output.lock().clear(); + *self.write_buffered_len.lock() = 0; + + // Reset shutdown state + *self.shutdown_state.lock() = ShutdownState::NotStarted; + } + } + // MemoryBIO - provides in-memory buffer for SSL/TLS I/O #[pyattr] #[pyclass(name = "MemoryBIO", module = "ssl")]