Skip to content

Commit f8cc62c

Browse files
committed
Async wait_for shim raise cancellation right away
Deferring the cancellation makes it much harder to reason about and might make the Python driver behave in a surprising way when cancelled.
1 parent 05fe946 commit f8cc62c

File tree

5 files changed

+9
-11
lines changed

5 files changed

+9
-11
lines changed

neo4j/_async/work/session.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def __init__(self, pool, session_config):
9090
super().__init__(pool, session_config)
9191
assert isinstance(session_config, SessionConfig)
9292
self._bookmarks = self._prepare_bookmarks(session_config.bookmarks)
93-
self._cancelled = False
9493

9594
async def __aenter__(self):
9695
return self
@@ -140,7 +139,6 @@ def _collect_bookmark(self, bookmark):
140139
self._bookmarks = bookmark,
141140

142141
def _handle_cancellation(self, message="General"):
143-
self._cancelled = True
144142
self._transaction = None
145143
self._auto_result = None
146144
connection = self._connection
@@ -178,7 +176,6 @@ async def close(self):
178176
This will release any borrowed resources, such as connections, and will
179177
roll back any outstanding transactions.
180178
"""
181-
# if self._closed or self._cancelled:
182179
if self._closed:
183180
return
184181
if self._connection:

neo4j/_async_compat/shims/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ async def wait_for(fut, timeout):
9393
# We got cancelled, but we are already done. Therefore,
9494
# we defer the cancellation until next time the task yields
9595
# to the event loop.
96-
asyncio.current_task().cancel()
96+
raise
9797
# [/PATCH]
98-
return fut.result()
9998
else:
10099
fut.remove_done_callback(cb)
101100
# We must ensure that the task is not running

neo4j/_sync/work/session.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def __init__(self, pool, session_config):
9090
super().__init__(pool, session_config)
9191
assert isinstance(session_config, SessionConfig)
9292
self._bookmarks = self._prepare_bookmarks(session_config.bookmarks)
93-
self._cancelled = False
9493

9594
def __enter__(self):
9695
return self
@@ -140,7 +139,6 @@ def _collect_bookmark(self, bookmark):
140139
self._bookmarks = bookmark,
141140

142141
def _handle_cancellation(self, message="General"):
143-
self._cancelled = True
144142
self._transaction = None
145143
self._auto_result = None
146144
connection = self._connection
@@ -178,7 +176,6 @@ def close(self):
178176
This will release any borrowed resources, such as connections, and will
179177
roll back any outstanding transactions.
180178
"""
181-
# if self._closed or self._cancelled:
182179
if self._closed:
183180
return
184181
if self._connection:

tests/integration/mixed/test_async_cancellation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async def work(tx, i=1):
4848
assert isinstance(summary, neo4j.ResultSummary)
4949
assert len(records) == 1
5050
assert list(records[0]) == [i]
51-
except asyncio.CancelledError as e:
51+
except asyncio.CancelledError:
5252
work_cancelled = True
5353
raise
5454

tests/unit/mixed/async_compat/test_concurrency.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,14 @@ async def test_async_r_lock_acquire_cancellation(waits):
176176
async def acquire_task():
177177
while True:
178178
count = lock._count
179-
await lock.acquire(timeout=0.1)
180-
assert lock._count == count + 1
181179
try:
180+
await lock.acquire(timeout=0.1)
181+
assert lock._count == count + 1
182+
except asyncio.CancelledError:
183+
assert lock._count == count
184+
raise
185+
try:
186+
# we're also ok with a deferred cancellation
182187
await asyncio.sleep(0)
183188
except asyncio.CancelledError:
184189
raise

0 commit comments

Comments
 (0)