Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions worker_plan_database/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ def build_postgres_uri_from_env(env: dict[str, str]) -> tuple[str, dict[str, str
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle' : 280, 'pool_pre_ping': True}
db.init_app(app)


def _dispose_db_pool_in_forked_child() -> None:
"""Drop the psycopg2 connection pool inherited from the parent after fork.

Without this, sibling Luigi workers share the parent's open
PostgreSQL sockets — interleaved writes corrupt the protocol
stream AND can route one user's row data into another user's
read buffer.

`close=False` is required: a normal close would send TCP close
packets down the parent's still-live socket and corrupt its
protocol stream. The next access in the child opens a fresh
connection.
"""
try:
with app.app_context():
db.engine.dispose(close=False)
except Exception:
pass


os.register_at_fork(after_in_child=_dispose_db_pool_in_forked_child)


def ensure_plans_table_name() -> None:
"""Rename legacy 'task_item' table (and its indexes) to 'plans' (idempotent).

Expand Down