Skip to content

Commit

Permalink
'await connection.close()' returns once all transaction queue items's…
Browse files Browse the repository at this point in the history
… results have been forwarded, including the _STOP_RUNNING_SENTINEL result
  • Loading branch information
davidandreoletti committed Aug 15, 2024
1 parent 463a2e8 commit 31a6336
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions aiosqlite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ def __init__(
DeprecationWarning,
)

def _stop_running(self):
async def _stop_running(self):
self._running = False
# PEP 661 is not accepted yet, so we cannot type a sentinel
self._tx.put_nowait(_STOP_RUNNING_SENTINEL) # type: ignore[arg-type]

function = partial(lambda: _STOP_RUNNING_SENTINEL)
future = asyncio.get_event_loop().create_future()

self._tx.put_nowait((future, function))

return await future

@property
def _conn(self) -> sqlite3.Connection:
Expand Down Expand Up @@ -105,16 +110,16 @@ def run(self) -> None:
# futures)

tx_item = self._tx.get()
if tx_item is _STOP_RUNNING_SENTINEL:
break

future, function = tx_item

try:
LOG.debug("executing %s", function)
result = function()
LOG.debug("operation %s completed", function)
future.get_loop().call_soon_threadsafe(set_result, future, result)

if result is _STOP_RUNNING_SENTINEL:
break
except BaseException as e: # noqa B036
LOG.debug("returning exception %s", e)
future.get_loop().call_soon_threadsafe(set_exception, future, e)
Expand All @@ -139,7 +144,7 @@ async def _connect(self) -> "Connection":
self._tx.put_nowait((future, self._connector))
self._connection = await future
except Exception:
self._stop_running()
await self._stop_running()
self._connection = None
raise

Expand Down Expand Up @@ -180,7 +185,7 @@ async def close(self) -> None:
LOG.info("exception occurred while closing connection")
raise
finally:
self._stop_running()
await self._stop_running()
self._connection = None

@contextmanager
Expand Down

0 comments on commit 31a6336

Please sign in to comment.