Skip to content

Comments

Fix startup instability and remove CWD dependency in MUIO v5#20

Open
parthdagia05 wants to merge 1 commit intoOSeMOSYS:masterfrom
parthdagia05:fix/startup-and-path-robustness
Open

Fix startup instability and remove CWD dependency in MUIO v5#20
parthdagia05 wants to merge 1 commit intoOSeMOSYS:masterfrom
parthdagia05:fix/startup-and-path-robustness

Conversation

@parthdagia05
Copy link

Bug Fix: Backend Path Hardening and Case Initialization Stability 🛡️

Closes: #2

Summary

This PR resolves startup instability and model creation failures in MUIO v5. It eliminates Current Working Directory (CWD) dependencies, guards import-time filesystem operations, and hardens the case initialization logic.

Prior to these changes, fresh installations often failed to configure new models due to path fragility and template resolution errors. This PR ensures a "plug-and-play" experience across different execution environments.


Root Causes Identified

The following blockers were identified during the investigation:

  • Relative Path Fragility: Config.py relied on relative paths (e.g., Path("WebAPP", ...)), causing failures if the server was started outside the API/ directory.
  • Unguarded Filesystem Ops: os.chmod(DATA_STORAGE) was executed at import time. This crashed if the directory didn't exist or if running on Windows.
  • Template Resolution: Flask used os.path.abspath('WebAPP'), which resolved incorrectly relative to the CWD, leading to TemplateNotFound: index.html.
  • Parameter Group Lookup: createCase() utilized unprotected lookups in Config.DEFAULT_F, causing silent failures when encountering unknown groups.

Changes Implemented

1. Absolute Path Resolution

All paths in Config.py are now dynamically derived from __file__. This ensures consistent directory resolution regardless of whether the app is started from the repository root, the API/ folder, or an external orchestrator.

2. Robust Filesystem Initialization

  • Auto-Provisioning: The DataStorage/ directory is now automatically created at startup if it is missing.
  • OS-Aware Permissions: os.chmod operations are now skipped on Windows and wrapped in a try/except block to prevent startup crashes.

3. Flask & Case Logic Hardening

  • Template Alignment: Flask is now explicitly configured to use Config.WebAPP_PATH for both template and static assets.
  • Defensive Mapping: Modified createCase() to skip unknown parameter groups instead of raising a KeyError, allowing for more resilient model initialization.

Manual Verification

The following scenarios were verified in a fresh environment:

  • Directory Agnostic Startup: Confirmed server starts successfully from both the repository root and the API/ directory.
  • Fresh Install Simulation: Verified that deleting WebAPP/DataStorage/ no longer causes a crash; the system regenerates it on launch.
  • End-to-End Workflow: * Templates (index.html) load correctly on the first request.
    • New models successfully created via the /AddCase endpoint.
    • Case directories and JSON payloads are generated with correct pathing.

Scope & Impact

  • Backend Only: This PR focuses exclusively on backend stability and path hardening.
  • Zero Regression: No modifications were made to solver execution logic, JSON schema formats, or frontend components.
  • Result: MUIO is now more portable, allowing users to extract and run the application from any directory without manual preparation.

- Derive all paths in Config.py from __file__ to eliminate CWD dependency
- Auto-create DataStorage directory on startup
- Guard os.chmod to prevent Windows/import-time crash
- Fix Flask template resolution using absolute Config.WebAPP_PATH
- Add guard in createCase() to skip unknown parameter groups

Resolves server startup crashes and model creation failures when running from different directories.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant