From 4a7f1f06acc286270357d388de5d08692ea3b9b3 Mon Sep 17 00:00:00 2001 From: Mattias Thall Date: Wed, 27 May 2026 01:01:18 +0200 Subject: [PATCH] Fix discovery refresh: open fresh DB session in background task MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The refresh endpoint was passing the request's db session to the background task, which is closed before the task runs — silently doing nothing on every refresh. Co-Authored-By: Claude Sonnet 4.6 --- backend/routers/discovery.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/routers/discovery.py b/backend/routers/discovery.py index 22657bc..8e434bb 100644 --- a/backend/routers/discovery.py +++ b/backend/routers/discovery.py @@ -163,7 +163,17 @@ def refresh_discovery( s = db.query(UserSettings).filter_by(user_id=current_user.id).first() regions_str = (s.discovery_regions if s and s.discovery_regions else "US,SE") regions = [r.strip().upper() for r in regions_str.split(",") if r.strip()] - background_tasks.add_task(run_full_discovery, db, current_user.id, regions) + user_id = current_user.id + + def _run_discovery(): + from ..database import SessionLocal + fresh_db = SessionLocal() + try: + run_full_discovery(fresh_db, user_id, regions) + finally: + fresh_db.close() + + background_tasks.add_task(_run_discovery) from .channels import _enrich_missing_task background_tasks.add_task(_enrich_missing_task, 20) return {"detail": "Discovery refresh started"}