@@ -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