Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break connection internal circular reference #774

Merged
merged 1 commit into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ async def _connect(*, loop, timeout, connection_class, record_class, **kwargs):
for addr in addrs:
before = time.monotonic()
try:
con = await _connect_addr(
return await _connect_addr(
addr=addr,
loop=loop,
timeout=timeout,
Expand All @@ -740,8 +740,6 @@ async def _connect(*, loop, timeout, connection_class, record_class, **kwargs):
)
except (OSError, asyncio.TimeoutError, ConnectionError) as ex:
last_error = ex
else:
return con
finally:
timeout -= time.monotonic() - before

Expand Down
11 changes: 10 additions & 1 deletion asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import asyncpg
import collections
import collections.abc
import functools
import itertools
import sys
import time
import traceback
import warnings
import weakref

from . import compat
from . import connect_utils
Expand Down Expand Up @@ -70,7 +72,8 @@ def __init__(self, protocol, transport, loop,
self._stmt_cache = _StatementCache(
loop=loop,
max_size=config.statement_cache_size,
on_remove=self._maybe_gc_stmt,
on_remove=functools.partial(
_weak_maybe_gc_stmt, weakref.ref(self)),
max_lifetime=config.max_cached_statement_lifetime)

self._stmts_to_close = set()
Expand Down Expand Up @@ -2260,4 +2263,10 @@ def _check_record_class(record_class):
)


def _weak_maybe_gc_stmt(weak_ref, stmt):
self = weak_ref()
if self is not None:
self._maybe_gc_stmt(stmt)


_uid = 0
6 changes: 1 addition & 5 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import asyncio
import contextlib
import gc
import ipaddress
import os
import platform
Expand Down Expand Up @@ -1448,14 +1447,11 @@ class TestConnectionGC(tb.ClusterTestCase):

async def _run_no_explicit_close_test(self):
con = await self.connect()
await con.fetchval("select 123")
proto = con._protocol
conref = weakref.ref(con)
del con

gc.collect()
gc.collect()
gc.collect()

self.assertIsNone(conref())
self.assertTrue(proto.is_closed())

Expand Down