Commit f749f85d authored by Marcus Hüwe's avatar Marcus Hüwe Committed by Matěj Cepl

Fix SSL.Connection.__del__

Without this change self.m2_ssl_free(self.ssl) is never called,
because m2.bio_noclose is defined as "0". Hence, the if-condition
is always false.  This got broken in commit e2f707b1 ("SSL
package: Port to python3").

Note that these testcases rely on the "fact" (or CPython
implementation detail?) that "del s" calls s' __del__ method.
parent ff96d066
Pipeline #18372192 passed with stage
in 18 minutes
......@@ -41,6 +41,7 @@ class Connection:
m2_bio_free = m2.bio_free
m2_ssl_free = m2.ssl_free
m2_bio_noclose = m2.bio_noclose
def __init__(self, ctx, sock=None, family=socket.AF_INET):
# type: (Context, socket.socket, int) -> None
......@@ -80,10 +81,7 @@ class Connection:
self.m2_bio_free(self.sslbio)
if getattr(self, 'sockbio', None):
self.m2_bio_free(self.sockbio)
# in __del__ method we have to check whether m2.bio_noclose
# exists at all.
if m2 is not None and m2.bio_noclose and \
self.ssl_close_flag == m2.bio_noclose and \
if self.ssl_close_flag == self.m2_bio_noclose and \
getattr(self, 'ssl', None):
self.m2_ssl_free(self.ssl)
self.socket.close()
......
......@@ -877,6 +877,53 @@ class MiscSSLClientTestCase(BaseSSLClientTestCase):
self.stop_server(pid)
self.assertIn('s_server -quiet -www', data)
def test_ssl_connection_free(self):
pid = self.start_server(self.args)
orig_m2_ssl_free = SSL.Connection.m2_ssl_free
def _m2_ssl_free(ssl):
orig_m2_ssl_free(ssl)
_m2_ssl_free.called = True
try:
ctx = SSL.Context()
s = SSL.Connection(ctx)
s.m2_ssl_free = _m2_ssl_free
s.connect(self.srv_addr)
data = self.http_get(s)
s.close()
self.assertFalse(hasattr(_m2_ssl_free, 'called'))
# keep fingers crossed that SSL.Connection.__del__ is called
# by the python interpreter
del s
finally:
self.stop_server(pid)
self.assertIn('s_server -quiet -www', data)
self.assertTrue(getattr(_m2_ssl_free, 'called', False))
def test_ssl_connection_no_free(self):
pid = self.start_server(self.args)
orig_m2_ssl_free = SSL.Connection.m2_ssl_free
def _m2_ssl_free(ssl):
_m2_ssl_free.called = True
orig_m2_ssl_free(ssl)
try:
ctx = SSL.Context()
s = SSL.Connection(ctx)
s.m2_ssl_free = _m2_ssl_free
s.set_ssl_close_flag(m2.bio_close)
s.connect(self.srv_addr)
data = self.http_get(s)
s.close()
self.assertFalse(hasattr(_m2_ssl_free, 'called'))
# keep fingers crossed that SSL.Connection.__del__ is called
# by the python interpreter
del s
finally:
self.stop_server(pid)
self.assertIn('s_server -quiet -www', data)
self.assertFalse(hasattr(_m2_ssl_free, 'called'))
class UrllibSSLClientTestCase(BaseSSLClientTestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment