@@ -48,7 +48,7 @@ mod _ssl {
4848 VirtualMachine ,
4949 builtins:: {
5050 PyBaseExceptionRef , PyByteArray , PyBytesRef , PyListRef , PyStrRef , PyType ,
51- PyTypeRef , PyUtf8StrRef ,
51+ PyTypeRef , PyUtf8StrRef , PyWeak ,
5252 } ,
5353 convert:: IntoPyException ,
5454 function:: {
@@ -1920,7 +1920,12 @@ mod _ssl {
19201920 connection : PyMutex :: new ( None ) ,
19211921 handshake_done : PyMutex :: new ( false ) ,
19221922 session_was_reused : PyMutex :: new ( false ) ,
1923- owner : PyRwLock :: new ( args. owner . into_option ( ) ) ,
1923+ owner : PyRwLock :: new (
1924+ args. owner
1925+ . into_option ( )
1926+ . map ( |o| o. downgrade ( None , vm) )
1927+ . transpose ( ) ?,
1928+ ) ,
19241929 // Filter out Python None objects - only store actual SSLSession objects
19251930 session : PyRwLock :: new ( args. session . into_option ( ) . filter ( |s| !vm. is_none ( s) ) ) ,
19261931 incoming_bio : None ,
@@ -1997,7 +2002,12 @@ mod _ssl {
19972002 connection : PyMutex :: new ( None ) ,
19982003 handshake_done : PyMutex :: new ( false ) ,
19992004 session_was_reused : PyMutex :: new ( false ) ,
2000- owner : PyRwLock :: new ( args. owner . into_option ( ) ) ,
2005+ owner : PyRwLock :: new (
2006+ args. owner
2007+ . into_option ( )
2008+ . map ( |o| o. downgrade ( None , vm) )
2009+ . transpose ( ) ?,
2010+ ) ,
20012011 // Filter out Python None objects - only store actual SSLSession objects
20022012 session : PyRwLock :: new ( args. session . into_option ( ) . filter ( |s| !vm. is_none ( s) ) ) ,
20032013 incoming_bio : Some ( args. incoming ) ,
@@ -2377,7 +2387,7 @@ mod _ssl {
23772387 #[ pytraverse( skip) ]
23782388 session_was_reused : PyMutex < bool > ,
23792389 // Owner (SSLSocket instance that owns this _SSLSocket)
2380- owner : PyRwLock < Option < PyObjectRef > > ,
2390+ owner : PyRwLock < Option < PyRef < PyWeak > > > ,
23812391 // Session for resumption
23822392 session : PyRwLock < Option < PyObjectRef > > ,
23832393 // MemoryBIO mode (optional)
@@ -2734,7 +2744,19 @@ mod _ssl {
27342744 return Ok ( ( ) ) ;
27352745 } ;
27362746
2737- let ssl_sock = self . owner . read ( ) . clone ( ) . unwrap_or_else ( || vm. ctx . none ( ) ) ;
2747+ let ssl_sock = self
2748+ . owner
2749+ . read ( )
2750+ . as_ref ( )
2751+ . and_then ( |owner| owner. upgrade ( ) )
2752+ . ok_or_else ( || {
2753+ super :: compat:: SslError :: create_ssl_error_with_reason (
2754+ vm,
2755+ Some ( "SSL" ) ,
2756+ "CALLBACK_FAILED" ,
2757+ "[SSL: CALLBACK_FAILED] callback failed" ,
2758+ )
2759+ } ) ?;
27382760 let server_name_py: PyObjectRef = match sni_name {
27392761 Some ( name) => vm. ctx . new_str ( name. to_string ( ) ) . into ( ) ,
27402762 None => vm. ctx . none ( ) ,
@@ -3955,12 +3977,13 @@ mod _ssl {
39553977
39563978 #[ pygetset]
39573979 fn owner ( & self ) -> Option < PyObjectRef > {
3958- self . owner . read ( ) . clone ( )
3980+ self . owner . read ( ) . as_ref ( ) . and_then ( |owner| owner . upgrade ( ) )
39593981 }
39603982
39613983 #[ pygetset( setter) ]
3962- fn set_owner ( & self , owner : PyObjectRef , _vm : & VirtualMachine ) {
3963- * self . owner . write ( ) = Some ( owner) ;
3984+ fn set_owner ( & self , owner : PyObjectRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
3985+ * self . owner . write ( ) = Some ( owner. downgrade ( None , vm) ?) ;
3986+ Ok ( ( ) )
39643987 }
39653988
39663989 #[ pygetset]
0 commit comments