From 1b142b26f22f36d0b9e2652cfd45686870789a18 Mon Sep 17 00:00:00 2001 From: MarekWo Date: Sun, 1 Mar 2026 10:33:07 +0100 Subject: [PATCH] fix(v2): Use Flask current_app for DeviceManager lookup in cli.py The module-level 'from app.main import device_manager' was returning None in Flask request context even though device_manager was set. Now tries current_app.device_manager first (Flask app context), falling back to module import for non-request contexts. Fixes 500 errors on /api/contacts, /api/contacts/pending, /api/contacts/detailed, and /api/channels endpoints. Co-Authored-By: Claude Opus 4.6 --- app/meshcore/cli.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/meshcore/cli.py b/app/meshcore/cli.py index 586954e..c377bbe 100644 --- a/app/meshcore/cli.py +++ b/app/meshcore/cli.py @@ -19,7 +19,15 @@ class MeshCLIError(Exception): def _get_dm(): - """Get the DeviceManager instance (deferred import to avoid circular refs).""" + """Get the DeviceManager instance — try Flask app context first, then module global.""" + try: + from flask import current_app + dm = getattr(current_app, 'device_manager', None) + if dm is not None: + return dm + except RuntimeError: + pass # Outside of Flask request context + from app.main import device_manager if device_manager is None: raise MeshCLIError("DeviceManager not initialized")