From b0131aa5662d1f2b3614a9a99ebf5844b1de8cd1 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Sun, 12 Apr 2026 00:15:41 +0000 Subject: [PATCH] fix: stop simulation always updates DB status - Status was only updated if engine was in memory (race condition) - Now always sets status to 'stopped' in DB - Returns 'stopped' instead of 'stopping' - Cleaned up 3 stale running simulations in DB --- src/backend/app/api/simulate.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/backend/app/api/simulate.py b/src/backend/app/api/simulate.py index ed7725d..1eca813 100644 --- a/src/backend/app/api/simulate.py +++ b/src/backend/app/api/simulate.py @@ -272,10 +272,15 @@ def stop_simulation( status_code=status.HTTP_404_NOT_FOUND, detail="Simulation not found" ) + # Always update status to stopped, even if engine is not in memory + simulation.status = "stopped" + + # Try to stop the engine if it's still in memory if run_id in running_simulations: engine = running_simulations[run_id] engine.stop() - simulation.status = "stopped" - db.commit() + del running_simulations[run_id] + + db.commit() - return {"status": "stopping", "run_id": run_id} + return {"status": "stopped", "run_id": run_id}