Fix send request 500: build response before commit to avoid MissingGreenlet

After db.commit(), all ORM objects are expired. Accessing their attributes
in _build_request_response triggered lazy loads which fail in async
SQLAlchemy with MissingGreenlet. Move response construction before commit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Kyle 2026-03-05 20:48:25 +08:00
parent 416f616457
commit 87d232cbcd

View File

@ -249,8 +249,11 @@ async def send_connection_request(
# Extract ntfy config before commit (avoids detached SA object in background task) # Extract ntfy config before commit (avoids detached SA object in background task)
target_ntfy = extract_ntfy_config(target_settings) if target_settings else None target_ntfy = extract_ntfy_config(target_settings) if target_settings else None
# Build response BEFORE commit — commit expires all ORM objects, and accessing
# their attributes after commit triggers lazy loads → MissingGreenlet in async SA.
response = _build_request_response(conn_request, current_user, sender_settings, target, target_settings)
await db.commit() await db.commit()
await db.refresh(conn_request)
# ntfy push in background (non-blocking) # ntfy push in background (non-blocking)
background_tasks.add_task( background_tasks.add_task(
@ -260,7 +263,7 @@ async def send_connection_request(
"request_received", "request_received",
) )
return _build_request_response(conn_request, current_user, sender_settings, target, target_settings) return response
# ── GET /requests/incoming ────────────────────────────────────────── # ── GET /requests/incoming ──────────────────────────────────────────