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

Cut BaseProtocol circular reference on close. #1049

Merged
merged 4 commits into from
Oct 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions asyncpg/protocol/protocol.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ cdef class BaseProtocol(CoreProtocol):
bint return_extra
object create_future
object timeout_handle
object timeout_callback
object completed_callback
object conref
type record_class
bint is_reading
Expand Down
7 changes: 3 additions & 4 deletions asyncpg/protocol/protocol.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ cdef class BaseProtocol(CoreProtocol):
self.writing_allowed.set()

self.timeout_handle = None
self.timeout_callback = self._on_timeout
self.completed_callback = self._on_waiter_completed

self.queries_count = 0

Expand Down Expand Up @@ -584,6 +582,7 @@ cdef class BaseProtocol(CoreProtocol):
self._handle_waiter_on_connection_lost(None)
self._terminate()
self.transport.abort()
self.transport = None

@cython.iterable_coroutine
async def close(self, timeout):
Expand Down Expand Up @@ -754,8 +753,8 @@ cdef class BaseProtocol(CoreProtocol):
self.waiter = self.create_future()
if timeout is not None:
self.timeout_handle = self.loop.call_later(
timeout, self.timeout_callback, self.waiter)
self.waiter.add_done_callback(self.completed_callback)
timeout, self._on_timeout, self.waiter)
self.waiter.add_done_callback(self._on_waiter_completed)
return self.waiter

cdef _on_result__connect(self, object waiter):
Expand Down
30 changes: 22 additions & 8 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import asyncio
import contextlib
import gc
import ipaddress
import os
import pathlib
Expand Down Expand Up @@ -1846,14 +1847,27 @@ async def worker():
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

self.assertIsNone(conref())
self.assertTrue(proto.is_closed())
gc_was_enabled = gc.isenabled()
gc.disable()
try:
con = await self.connect()
await con.fetchval("select 123")
proto = con._protocol
conref = weakref.ref(con)
del con

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

# tick event loop; asyncio.selector_events._SelectorSocketTransport
# needs a chance to close itself and remove its reference to proto
await asyncio.sleep(0)
protoref = weakref.ref(proto)
del proto
self.assertIsNone(protoref())
finally:
if gc_was_enabled:
gc.enable()

async def test_no_explicit_close_no_debug(self):
olddebug = self.loop.get_debug()
Expand Down