Fix rebuild blocking FastAPI event loop

Switch /rebuild endpoint from subprocess.run (blocks event loop) to
asyncio.create_subprocess_exec, preventing auth failures on concurrent
requests during build. Update site submodule pointer.
This commit is contained in:
brutsalvadi
2026-04-23 21:37:53 +02:00
parent bf9b182c6f
commit 875ac4ca51
2 changed files with 17 additions and 13 deletions
+16 -12
View File
@@ -2,11 +2,11 @@
from __future__ import annotations
import asyncio
import os
import re
import secrets
import sqlite3
import subprocess
import time
from contextlib import contextmanager
from pathlib import Path
@@ -248,21 +248,25 @@ async def delete_story(slug: str, user: dict = Depends(require_auth)) -> JSONRes
@app.post("/rebuild")
async def rebuild(user: dict = Depends(require_auth)) -> JSONResponse:
"""Trigger an astro build of the site."""
"""Trigger an astro build of the site (non-blocking)."""
try:
result = subprocess.run(
["npm", "run", "build"],
proc = await asyncio.create_subprocess_exec(
"npm", "run", "build",
cwd=site_dir,
capture_output=True,
text=True,
timeout=120,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=120)
except asyncio.TimeoutError:
proc.kill()
raise HTTPException(504, "Build timed out after 120s")
return JSONResponse({
"success": result.returncode == 0,
"stdout": result.stdout[-3000:] if result.stdout else "",
"stderr": result.stderr[-3000:] if result.stderr else "",
"success": proc.returncode == 0,
"stdout": stdout.decode()[-3000:] if stdout else "",
"stderr": stderr.decode()[-3000:] if stderr else "",
})
except subprocess.TimeoutExpired:
raise HTTPException(504, "Build timed out after 120s")
except HTTPException:
raise
except Exception as e:
raise HTTPException(500, str(e))
+1 -1
Submodule site updated: 422ffe76fa...c3573b09a3