From 27428b69a0a5d9c09cf09bccb0f153b807d0b50c Mon Sep 17 00:00:00 2001 From: Sascha Lustenberger | techlan gmbh Date: Tue, 24 Feb 2026 11:12:17 +0100 Subject: [PATCH] fix(netbird): query customer before use in stop/start/restart In stop_customer, start_customer and restart_customer the local variable 'customer' was referenced on the instance_dir line before it was assigned (it was only queried after the docker compose call). This caused an UnboundLocalError (HTTP 500) on every stop/start/restart action. Fix: move the customer query to the top of each function alongside the deployment and config queries. Co-Authored-By: Claude Sonnet 4.6 --- app/services/netbird_service.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/app/services/netbird_service.py b/app/services/netbird_service.py index 41dc07b..247924c 100644 --- a/app/services/netbird_service.py +++ b/app/services/netbird_service.py @@ -484,17 +484,16 @@ async def undeploy_customer(db: Session, customer_id: int) -> dict[str, Any]: async def stop_customer(db: Session, customer_id: int) -> dict[str, Any]: """Stop containers for a customer.""" deployment = db.query(Deployment).filter(Deployment.customer_id == customer_id).first() + customer = db.query(Customer).filter(Customer.id == customer_id).first() config = get_system_config(db) - if not deployment or not config: - return {"success": False, "error": "Deployment or config not found."} + if not deployment or not config or not customer: + return {"success": False, "error": "Deployment, customer or config not found."} instance_dir = os.path.join(config.data_dir, customer.subdomain) ok = await docker_service.compose_stop(instance_dir, deployment.container_prefix) if ok: deployment.deployment_status = "stopped" - customer = db.query(Customer).filter(Customer.id == customer_id).first() - if customer: - customer.status = "inactive" + customer.status = "inactive" db.commit() _log_action(db, customer_id, "stop", "success", "Containers stopped.") else: @@ -505,17 +504,16 @@ async def stop_customer(db: Session, customer_id: int) -> dict[str, Any]: async def start_customer(db: Session, customer_id: int) -> dict[str, Any]: """Start containers for a customer.""" deployment = db.query(Deployment).filter(Deployment.customer_id == customer_id).first() + customer = db.query(Customer).filter(Customer.id == customer_id).first() config = get_system_config(db) - if not deployment or not config: - return {"success": False, "error": "Deployment or config not found."} + if not deployment or not config or not customer: + return {"success": False, "error": "Deployment, customer or config not found."} instance_dir = os.path.join(config.data_dir, customer.subdomain) ok = await docker_service.compose_start(instance_dir, deployment.container_prefix) if ok: deployment.deployment_status = "running" - customer = db.query(Customer).filter(Customer.id == customer_id).first() - if customer: - customer.status = "active" + customer.status = "active" db.commit() _log_action(db, customer_id, "start", "success", "Containers started.") else: @@ -526,17 +524,16 @@ async def start_customer(db: Session, customer_id: int) -> dict[str, Any]: async def restart_customer(db: Session, customer_id: int) -> dict[str, Any]: """Restart containers for a customer.""" deployment = db.query(Deployment).filter(Deployment.customer_id == customer_id).first() + customer = db.query(Customer).filter(Customer.id == customer_id).first() config = get_system_config(db) - if not deployment or not config: - return {"success": False, "error": "Deployment or config not found."} + if not deployment or not config or not customer: + return {"success": False, "error": "Deployment, customer or config not found."} instance_dir = os.path.join(config.data_dir, customer.subdomain) ok = await docker_service.compose_restart(instance_dir, deployment.container_prefix) if ok: deployment.deployment_status = "running" - customer = db.query(Customer).filter(Customer.id == customer_id).first() - if customer: - customer.status = "active" + customer.status = "active" db.commit() _log_action(db, customer_id, "restart", "success", "Containers restarted.") else: