Skip to content

Commit 16472bc

Browse files
committed
Attempt to work around a race condition with first admin
1 parent 975ee67 commit 16472bc

File tree

1 file changed

+14
-0
lines changed
  • src/rasenmaeher_api/web/api/firstuser

1 file changed

+14
-0
lines changed

src/rasenmaeher_api/web/api/firstuser/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Firstuser API views."""
2+
import asyncio
23
import logging
34
from fastapi import APIRouter, Request, Body, Depends, HTTPException
45

56
from multikeyjwt.middleware import JWTBearer, JWTPayload
7+
from libadvian.tasks import TaskMaster
68

79
from rasenmaeher_api.web.api.firstuser.schema import (
810
FirstuserCheckCodeIn,
@@ -81,6 +83,18 @@ async def post_admin_add(
8183
# and approve the user
8284
anon_admin_user = await Person.by_callsign(callsign="anon_admin")
8385
new_admin = await enrollment.approve(approver=anon_admin_user)
86+
87+
# FIXME Should tbe TaskMaster feature
88+
async def tms_wait() -> None:
89+
"""Wait for background tasks to avoid race conditions"""
90+
tma = TaskMaster.singleton()
91+
while tma._tasks: # pylint: disable=W0212
92+
await asyncio.sleep(0.1)
93+
94+
try:
95+
await asyncio.wait_for(tms_wait(), timeout=3.0)
96+
except asyncio.TimeoutError:
97+
LOGGER.warning("Timed out while waiting for background tasks, continuing anyway")
8498
role_add_success = await new_admin.assign_role(role="admin")
8599

86100
if role_add_success is False:

0 commit comments

Comments
 (0)