INNER CODE UNIT · Python
_release_leader_lease_within
Soju06/codex-lb · app/main.py:213
async def _release_leader_lease_within(timeout: float) -> bool:
"""Release the scheduler leader lease without ever pinning shutdown.
``release()`` uses a background DB session whose rollback/close shield and
await their own cleanup, so wrapping it in ``asyncio.wait_for`` would only
cancel the awaiting wrapper while a wedged database call keeps unwinding —
shutdown could still hang past the deadline. Run the release as a task and,
if it does not finish within ``timeout``, abandon it (logging its eventual
outcome from a done callback) so shutdown always proceeds within the
deadline; the lease then expires after its TTL, which is acceptable.
"""
release_task: asyncio.Task[bool] = asyncio.ensure_future(get_leader_election().release())
done, _ = await asyncio.wait({release_task}, timeout=timeout)
if release_task not in done:
logger.warning(
"Scheduler leader lease release did not finish within %.1fs; abandoning it so "
"shutdown can proceed (the lease will expire after its TTL)",
timeout,