Skip to content

Commit b4b8ceb

Browse files
committed
Merge branch 'fix-608' into maint-3.1
2 parents 125f93c + ae43e63 commit b4b8ceb

7 files changed

Lines changed: 186 additions & 23 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 possible hanging if a 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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,17 @@ def cancel(self) -> None:
311311
"cancel() cannot be used with a prepared two-phase transaction"
312312
)
313313

314-
c = self.pgconn.get_cancel()
315-
c.cancel()
314+
self._try_cancel(self.pgconn)
315+
316+
@classmethod
317+
def _try_cancel(cls, pgconn: "PGconn") -> None:
318+
try:
319+
# Can fail if the connection is closed
320+
c = pgconn.get_cancel()
321+
except Exception as ex:
322+
logger.warning("couldn't try to cancel query: %s", ex)
323+
else:
324+
c.cancel()
316325

317326
def add_notice_handler(self, callback: NoticeHandler) -> None:
318327
"""
@@ -791,6 +800,9 @@ def close(self) -> None:
791800
if self.closed:
792801
return
793802
self._closed = True
803+
804+
# TODO: maybe send a cancel on close, if the connection is ACTIVE?
805+
794806
self.pgconn.finish()
795807

796808
@overload
@@ -959,8 +971,7 @@ def wait(self, gen: PQGen[RV], timeout: Optional[float] = 0.1) -> RV:
959971
except KeyboardInterrupt:
960972
# On Ctrl-C, try to cancel the query in the server, otherwise
961973
# the connection will remain stuck in ACTIVE state.
962-
c = self.pgconn.get_cancel()
963-
c.cancel()
974+
self._try_cancel(self.pgconn)
964975
try:
965976
waiting.wait(gen, self.pgconn.socket, timeout=timeout)
966977
except e.QueryCanceled:

‎psycopg/psycopg/connection_async.py‎

Lines changed: 7 additions & 5 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,16 +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.
352-
c = self.pgconn.get_cancel()
353-
c.cancel()
355+
self._try_cancel(self.pgconn)
354356
try:
355-
await waiting.wait_async(gen, self.pgconn.socket)
357+
await waiting.wait_async(gen, self.pgconn.socket, timeout=timeout)
356358
except e.QueryCanceled:
357359
pass # as expected
358360
raise

‎psycopg/psycopg/waiting.py‎

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import sys
1414
import select
1515
import selectors
16-
from typing import Dict, Optional
16+
from typing import Optional
1717
from asyncio import get_event_loop, wait_for, Event, TimeoutError
1818
from selectors import DefaultSelector
1919

@@ -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)
@@ -210,6 +218,8 @@ def wakeup(state: Ready) -> None:
210218
def wait_select(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) -> RV:
211219
"""
212220
Wait for a generator using select where supported.
221+
222+
BUG: on Linux, can't select on FD >= 1024. On Windows it's fine.
213223
"""
214224
try:
215225
s = next(gen)
@@ -238,16 +248,15 @@ def wait_select(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) ->
238248
return rv
239249

240250

241-
poll_evmasks: Dict[Wait, int]
242-
243251
if hasattr(selectors, "EpollSelector"):
244-
poll_evmasks = {
245-
WAIT_R: select.EPOLLONESHOT | select.EPOLLIN,
246-
WAIT_W: select.EPOLLONESHOT | select.EPOLLOUT,
247-
WAIT_RW: select.EPOLLONESHOT | select.EPOLLIN | select.EPOLLOUT,
252+
_epoll_evmasks = {
253+
WAIT_R: select.EPOLLONESHOT | select.EPOLLIN | select.EPOLLERR,
254+
WAIT_W: select.EPOLLONESHOT | select.EPOLLOUT | select.EPOLLERR,
255+
WAIT_RW: select.EPOLLONESHOT
256+
| (select.EPOLLIN | select.EPOLLOUT | select.EPOLLERR),
248257
}
249258
else:
250-
poll_evmasks = {}
259+
_epoll_evmasks = {}
251260

252261

253262
def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) -> RV:
@@ -258,6 +267,13 @@ def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) ->
258267
strategy is `epoll` then this function will be used instead of `wait`.
259268
260269
See also: https://linux.die.net/man/2/epoll_ctl
270+
271+
BUG: if the connection FD is closed, `epoll.poll()` hangs. Same for
272+
EpollSelector. For this reason, wait_poll() is currently preferable.
273+
To reproduce the bug:
274+
275+
export PSYCOPG_WAIT_FUNC=wait_epoll
276+
pytest tests/test_concurrency.py::test_concurrent_close
261277
"""
262278
try:
263279
s = next(gen)
@@ -268,7 +284,7 @@ def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) ->
268284
timeout = int(timeout * 1000.0)
269285

270286
with select.epoll() as epoll:
271-
evmask = poll_evmasks[s]
287+
evmask = _epoll_evmasks[s]
272288
epoll.register(fileno, evmask)
273289
while True:
274290
fileevs = None
@@ -282,14 +298,61 @@ def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) ->
282298
ready |= READY_W
283299
# assert s & ready
284300
s = gen.send(ready)
285-
evmask = poll_evmasks[s]
301+
evmask = _epoll_evmasks[s]
286302
epoll.modify(fileno, evmask)
287303

288304
except StopIteration as ex:
289305
rv: RV = ex.args[0] if ex.args else None
290306
return rv
291307

292308

309+
if hasattr(selectors, "PollSelector"):
310+
_poll_evmasks = {
311+
WAIT_R: select.POLLIN,
312+
WAIT_W: select.POLLOUT,
313+
WAIT_RW: select.POLLIN | select.POLLOUT,
314+
}
315+
else:
316+
_poll_evmasks = {}
317+
318+
319+
def wait_poll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) -> RV:
320+
"""
321+
Wait for a generator using poll where supported.
322+
323+
Parameters are like for `wait()`.
324+
"""
325+
try:
326+
s = next(gen)
327+
328+
if timeout is None or timeout < 0:
329+
timeout = 0
330+
else:
331+
timeout = int(timeout * 1000.0)
332+
333+
poll = select.poll()
334+
evmask = _poll_evmasks[s]
335+
poll.register(fileno, evmask)
336+
while True:
337+
fileevs = None
338+
while not fileevs:
339+
fileevs = poll.poll(timeout)
340+
ev = fileevs[0][1]
341+
ready = 0
342+
if ev & ~select.POLLOUT:
343+
ready = READY_R
344+
if ev & ~select.POLLIN:
345+
ready |= READY_W
346+
# assert s & ready
347+
s = gen.send(ready)
348+
evmask = _poll_evmasks[s]
349+
poll.modify(fileno, evmask)
350+
351+
except StopIteration as ex:
352+
rv: RV = ex.args[0] if ex.args else None
353+
return rv
354+
355+
293356
if _psycopg:
294357
wait_c = _psycopg.wait_c
295358

@@ -321,8 +384,10 @@ def wait_epoll(gen: PQGen[RV], fileno: int, timeout: Optional[float] = None) ->
321384
# On Windows, SelectSelector should be the default.
322385
wait = wait_select
323386

324-
elif selectors.DefaultSelector is getattr(selectors, "EpollSelector", None):
325-
wait = wait_epoll
387+
elif hasattr(selectors, "PollSelector"):
388+
# On linux, EpollSelector is the default. However, it hangs if the fd is
389+
# closed while polling.
390+
wait = wait_poll
326391

327392
else:
328393
wait = wait_selector

‎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+
# Test issue #608: concurrent closing shouldn't hang the server
405+
# (although, at the moment, it doesn't cancel a running query).
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)

‎tests/test_waiting.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
pytest.param(
2323
"wait_epoll", marks=pytest.mark.skipif("not hasattr(select, 'epoll')")
2424
),
25+
pytest.param("wait_poll", marks=pytest.mark.skipif("not hasattr(select, 'poll')")),
2526
pytest.param("wait_c", marks=pytest.mark.skipif("not psycopg._cmodule._psycopg")),
2627
]
2728

0 commit comments

Comments
 (0)