fix(zopk): Thread-safe DB session and connection pool for gthread workers
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

Create separate SessionLocal() in run_search() thread instead of sharing
main thread's session (SQLAlchemy sessions are not thread-safe). Increase
connection pool_size to 10 with pool_pre_ping for gthread worker support.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-02-09 14:26:11 +01:00
parent fae7bfa0b2
commit c7a42c3766
2 changed files with 4 additions and 2 deletions

View File

@ -618,14 +618,16 @@ def api_zopk_search_news_stream():
progress_queue.put((phase, message, current, total))
def run_search():
search_db = SessionLocal()
try:
service = ZOPKNewsService(db)
service = ZOPKNewsService(search_db)
result_holder[0] = service.search_all_sources(
query, user_id=user_id, progress_callback=on_progress
)
except Exception as e:
error_holder[0] = e
finally:
search_db.close()
progress_queue.put(None) # sentinel
thread = threading.Thread(target=run_search, daemon=True)

View File

@ -253,7 +253,7 @@ JSONB = JSONBType
# Create engine
engine = create_engine(DATABASE_URL, echo=False)
engine = create_engine(DATABASE_URL, echo=False, pool_size=10, pool_pre_ping=True)
SessionLocal = sessionmaker(bind=engine)
Base = declarative_base()