Skip to content

Comments

Redesigned database connections#113

Merged
mcmancini merged 1 commit intodevfrom
iss_112_connection_explosion
Oct 1, 2025
Merged

Redesigned database connections#113
mcmancini merged 1 commit intodevfrom
iss_112_connection_explosion

Conversation

@mcmancini
Copy link
Owner

@mcmancini mcmancini commented Oct 1, 2025

This PR addresses Issue #112

Problem
Bulk simulations in the WOFOST API were opening a new database connection for every parcel in parallel runs. When many runs were submitted at once, this caused PostgreSQL connection exhaustion and NoneType / to_crs errors.

Solution

  • Introduced a centralized SQLAlchemy session factory (SessionLocal) in ukwofost.core.utility.db.

  • FastAPI endpoints now inject a scoped session via Depends(get_db) for sequential runs.

  • Simulation functions (Parcel, run_wofost_simulation, run_from_payload) were refactored to accept a db session parameter.

  • For parallel execution, each worker process creates its own session from SessionLocal(), preventing cross-process session sharing and pickling issues.

  • Sequential execution reuses the injected session to minimize connection usage.

…o parallel workers in API as well as locally
@mcmancini mcmancini added the bug Something isn't working label Oct 1, 2025
@mcmancini mcmancini self-assigned this Oct 1, 2025
@mcmancini mcmancini merged commit 956b0b8 into dev Oct 1, 2025
8 checks passed
@mcmancini mcmancini deleted the iss_112_connection_explosion branch October 2, 2025 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant