Skip to content

Commit

Permalink
Fix issue #27
Browse files Browse the repository at this point in the history
  • Loading branch information
romis2012 committed Sep 6, 2023
1 parent e3a5ef3 commit f81d5a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 45 deletions.
2 changes: 1 addition & 1 deletion aiohttp_socks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = 'aiohttp-socks'
__version__ = '0.8.2'
__version__ = '0.8.3'

from python_socks import (
ProxyError,
Expand Down
63 changes: 19 additions & 44 deletions aiohttp_socks/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ async def close(self):
pass # pragma: no cover


class RepairedStreamWriter(StreamWriter):
def __del__(self):
pass


def patch_stream(stream):
"""
Fix issue https://github.com/romis2012/aiohttp-socks/issues/27
"""
stream.writer.__class__ = RepairedStreamWriter
while hasattr(stream, '_inner'):
stream = stream._inner # noqa
stream.writer.__class__ = RepairedStreamWriter


class ProxyConnector(TCPConnector):
def __init__(
self,
Expand All @@ -51,9 +66,6 @@ def __init__(
self._rdns = rdns
self._proxy_ssl = proxy_ssl

# self._streams = []
self._writer_del = getattr(StreamWriter, '__del__', None)

# noinspection PyMethodOverriding
async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **kwargs):
proxy = Proxy.create(
Expand All @@ -79,32 +91,15 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **
timeout=connect_timeout,
)

# Fix issue https://github.com/romis2012/aiohttp-socks/issues/27
# On Python 3.11.5
# We need to keep references to the stream.reader/stream.writer so that they
# are not garbage collected and closed while we're still using them.
# See StreamWriter.__del__ method (was added in Python 3.11.5)
# self._streams.append(stream)
#

# Since the solution above leads to potential memory leaks,
# we just remove the StreamWriter's __del__ attribute
if hasattr(stream.writer.__class__, '__del__'):
delattr(stream.writer.__class__, '__del__')

transport: BaseTransport = stream.writer.transport
protocol: ResponseHandler = protocol_factory()

transport.set_protocol(protocol)
protocol.connection_made(transport)

return transport, protocol
patch_stream(stream)

def close(self):
result = super().close()
if self._writer_del is not None:
setattr(StreamWriter, '__del__', self._writer_del)
return result
return transport, protocol

@classmethod
def from_url(cls, url, **kwargs):
Expand Down Expand Up @@ -135,9 +130,6 @@ def __init__(self, proxy_infos: Iterable[ProxyInfo], **kwargs):

self._proxy_infos = proxy_infos

# self._streams = []
self._writer_del = getattr(StreamWriter, '__del__', None)

# noinspection PyMethodOverriding
async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **kwargs):
proxies = []
Expand Down Expand Up @@ -167,32 +159,15 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **
timeout=connect_timeout,
)

# Fix issue https://github.com/romis2012/aiohttp-socks/issues/27
# On Python 3.11.5
# We need to keep references to the stream.reader/stream.writer so that they
# are not garbage collected and closed while we're still using them.
# See StreamWriter.__del__ method (was added in Python 3.11.5)
# self._streams.append(stream)
#

# Since the solution above leads to potential memory leaks,
# we just remove the StreamWriter's __del__ attribute
if hasattr(stream.writer.__class__, '__del__'):
delattr(stream.writer.__class__, '__del__')

transport: BaseTransport = stream.writer.transport
protocol: ResponseHandler = protocol_factory()

transport.set_protocol(protocol)
protocol.connection_made(transport)

return transport, protocol
patch_stream(stream)

def close(self):
result = super().close()
if self._writer_del is not None:
setattr(StreamWriter, '__del__', self._writer_del)
return result
return transport, protocol

@classmethod
def from_urls(cls, urls: Iterable[str], **kwargs):
Expand Down

0 comments on commit f81d5a1

Please sign in to comment.