Skip to content

Commit 8b564e8

Browse files
committed
fix: don't hang forever if async connection is closed while querying
Fix #608
1 parent b3e0be9 commit 8b564e8

6 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎docs/news.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Psycopg 3.1.12 (unreleased)
1111
^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13+
- Fix hanging if an async connection is closed while querying (:ticket:`#608`).
1314
- Fix memory leak when `~register_*()` functions are called repeatedly
1415
(:ticket:`#647`).
1516

‎psycopg/psycopg/connection.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ def close(self) -> None:
800800
if self.closed:
801801
return
802802
self._closed = True
803+
804+
# TODO: maybe send a cancel on close, if the connection is ACTIVE?
805+
803806
self.pgconn.finish()
804807

805808
@overload

‎psycopg/psycopg/connection_async.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ async def close(self) -> None:
197197
if self.closed:
198198
return
199199
self._closed = True
200+
201+
# TODO: maybe send a cancel on close, if the connection is ACTIVE?
202+
200203
self.pgconn.finish()
201204

202205
@overload
@@ -343,15 +346,15 @@ async def pipeline(self) -> AsyncIterator[AsyncPipeline]:
343346
assert pipeline is self._pipeline
344347
self._pipeline = None
345348

346-
async def wait(self, gen: PQGen[RV]) -> RV:
349+
async def wait(self, gen: PQGen[RV], timeout: Optional[float] = 0.1) -> RV:
347350
try:
348-
return await waiting.wait_async(gen, self.pgconn.socket)
351+
return await waiting.wait_async(gen, self.pgconn.socket, timeout=timeout)
349352
except (asyncio.CancelledError, KeyboardInterrupt):
350353
# On Ctrl-C, try to cancel the query in the server, otherwise
351354
# the connection will remain stuck in ACTIVE state.
352355
self._try_cancel(self.pgconn)
353356
try:
354-
await waiting.wait_async(gen, self.pgconn.socket)
357+
await waiting.wait_async(gen, self.pgconn.socket, timeout=timeout)
355358
except e.QueryCanceled:
356359
pass # as expected
357360
raise

‎psycopg/psycopg/waiting.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ def wait_conn(gen: PQGenConn[RV], timeout: Optional[float] = None) -> RV:
9797
return rv
9898

9999

100-
async def wait_async(gen: PQGen[RV], fileno: int) -> RV:
100+
async def wait_async(
101+
gen: PQGen[RV], fileno: int, timeout: Optional[float] = None
102+
) -> RV:
101103
"""
102104
Coroutine waiting for a generator to complete.
103105
@@ -134,7 +136,13 @@ def wakeup(state: Ready) -> None:
134136
if writer:
135137
loop.add_writer(fileno, wakeup, READY_W)
136138
try:
137-
await ev.wait()
139+
if timeout is None:
140+
await ev.wait()
141+
else:
142+
try:
143+
await wait_for(ev.wait(), timeout)
144+
except TimeoutError:
145+
pass
138146
finally:
139147
if reader:
140148
loop.remove_reader(fileno)

‎tests/test_concurrency.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,42 @@ def test(arg):
392392
env["PYTHONFAULTHANDLER"] = "1"
393393
out = sp.check_output([sys.executable, "-s", "-c", script], env=env)
394394
assert out.decode().rstrip() == "[1, 1]"
395+
396+
397+
@pytest.mark.slow
398+
@pytest.mark.crdb("skip")
399+
@pytest.mark.skipif(
400+
sys.platform == "win32",
401+
reason="Fails with: An operation was attempted on something that is not a socket",
402+
)
403+
def test_concurrent_close(dsn, conn):
404+
# Verify something similar to the problem in #608, which doesn't affect
405+
# sync connections anyway.
406+
pid = conn.info.backend_pid
407+
conn.autocommit = True
408+
409+
def worker():
410+
try:
411+
conn.execute("select pg_sleep(3)")
412+
except psycopg.OperationalError:
413+
pass # expected
414+
415+
t0 = time.time()
416+
th = threading.Thread(target=worker)
417+
th.start()
418+
time.sleep(0.5)
419+
with psycopg.connect(dsn, autocommit=True) as conn1:
420+
cur = conn1.execute("select query from pg_stat_activity where pid = %s", [pid])
421+
assert cur.fetchone()
422+
conn.close()
423+
th.join()
424+
time.sleep(0.5)
425+
t = time.time()
426+
# TODO: this check can pass if we issue a cancel on close, which is
427+
# a change in behaviour to be considered better.
428+
# cur = conn1.execute(
429+
# "select query from pg_stat_activity where pid = %s",
430+
# [pid],
431+
# )
432+
# assert not cur.fetchone()
433+
assert t - t0 < 2

‎tests/test_concurrency_async.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,47 @@ def run_process():
313313

314314
t1 = time.time()
315315
assert t1 - t0 < 1.0
316+
317+
318+
@pytest.mark.slow
319+
@pytest.mark.crdb("skip")
320+
@pytest.mark.skipif(
321+
sys.platform == "win32",
322+
reason="Fails with: An operation was attempted on something that is not a socket",
323+
)
324+
async def test_concurrent_close(dsn, aconn):
325+
# Test issue #608: concurrent closing shouldn't hang the server
326+
# (although, at the moment, it doesn't cancel a running query).
327+
pid = aconn.info.backend_pid
328+
await aconn.set_autocommit(True)
329+
330+
async def worker():
331+
try:
332+
await aconn.execute("select pg_sleep(3)")
333+
except psycopg.OperationalError:
334+
pass # expected
335+
336+
t0 = time.time()
337+
task = create_task(worker())
338+
await asyncio.sleep(0.5)
339+
340+
async def test():
341+
async with await psycopg.AsyncConnection.connect(dsn, autocommit=True) as conn1:
342+
cur = await conn1.execute(
343+
"select query from pg_stat_activity where pid = %s", [pid]
344+
)
345+
assert await cur.fetchone()
346+
await aconn.close()
347+
await asyncio.gather(task)
348+
await asyncio.sleep(0.5)
349+
t = time.time()
350+
# TODO: this statement can pass only if we send cancel on close
351+
# but because async cancelling is not available in the libpq,
352+
# we'd rather not do it.
353+
# cur = await conn1.execute(
354+
# "select query from pg_stat_activity where pid = %s", [pid]
355+
# )
356+
# assert not await cur.fetchone()
357+
assert t - t0 < 2
358+
359+
await asyncio.wait_for(test(), 5.0)

0 commit comments

Comments
 (0)