@@ -3503,9 +3503,9 @@ mod _ssl {
35033503 }
35043504
35053505 // Check if connection has been shut down
3506- // After unwrap()/ shutdown(), read operations should fail with SSLError
3506+ // Only block after shutdown is COMPLETED, not during shutdown process
35073507 let shutdown_state = * self . shutdown_state . lock ( ) ;
3508- if shutdown_state != ShutdownState :: NotStarted {
3508+ if shutdown_state == ShutdownState :: Completed {
35093509 return Err ( vm
35103510 . new_os_subtype_error (
35113511 PySSLError :: class ( & vm. ctx ) . to_owned ( ) ,
@@ -3677,7 +3677,8 @@ mod _ssl {
36773677 }
36783678
36793679 // Check shutdown state
3680- if * self . shutdown_state . lock ( ) != ShutdownState :: NotStarted {
3680+ // Only block after shutdown is COMPLETED, not during shutdown process
3681+ if * self . shutdown_state . lock ( ) == ShutdownState :: Completed {
36813682 return Err ( vm
36823683 . new_os_subtype_error (
36833684 PySSLError :: class ( & vm. ctx ) . to_owned ( ) ,
@@ -4138,6 +4139,8 @@ mod _ssl {
41384139 }
41394140 }
41404141
4142+ // Set shutdown_state first to ensure atomic visibility
4143+ // This prevents read/write race conditions during shutdown
41414144 * self . shutdown_state . lock ( ) = ShutdownState :: Completed ;
41424145 * self . connection . lock ( ) = None ;
41434146 return Ok ( self . sock . clone ( ) ) ;
@@ -4399,6 +4402,21 @@ mod _ssl {
43994402 }
44004403 }
44014404
4405+ // Clean up SSL socket resources on drop
4406+ impl Drop for PySSLSocket {
4407+ fn drop ( & mut self ) {
4408+ // Clear connection state
4409+ let _ = self . connection . lock ( ) . take ( ) ;
4410+
4411+ // Clear pending buffers
4412+ self . pending_tls_output . lock ( ) . clear ( ) ;
4413+ * self . write_buffered_len . lock ( ) = 0 ;
4414+
4415+ // Reset shutdown state
4416+ * self . shutdown_state . lock ( ) = ShutdownState :: NotStarted ;
4417+ }
4418+ }
4419+
44024420 // MemoryBIO - provides in-memory buffer for SSL/TLS I/O
44034421 #[ pyattr]
44044422 #[ pyclass( name = "MemoryBIO" , module = "ssl" ) ]
0 commit comments