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 5, 2023
1 parent 7ad35e5 commit e1541cc
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions aiohttp_socks/connector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import socket
import typing
from typing import Iterable
from asyncio import BaseTransport
from asyncio import BaseTransport, StreamWriter

from aiohttp import TCPConnector
from aiohttp.abc import AbstractResolver
Expand Down Expand Up @@ -51,7 +51,8 @@ def __init__(
self._rdns = rdns
self._proxy_ssl = proxy_ssl

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

# noinspection PyMethodOverriding
async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **kwargs):
Expand Down Expand Up @@ -83,9 +84,14 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **
# 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)
# 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()

Expand All @@ -94,6 +100,12 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **

return transport, protocol

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

@classmethod
def from_url(cls, url, **kwargs):
proxy_type, host, port, username, password = parse_proxy_url(url)
Expand Down Expand Up @@ -123,7 +135,8 @@ def __init__(self, proxy_infos: Iterable[ProxyInfo], **kwargs):

self._proxy_infos = proxy_infos

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

# noinspection PyMethodOverriding
async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **kwargs):
Expand Down Expand Up @@ -159,9 +172,14 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **
# 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)
# 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()

Expand All @@ -170,6 +188,12 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **

return transport, protocol

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

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

0 comments on commit e1541cc

Please sign in to comment.