@@ -298,13 +298,14 @@ def send_wled_command(data, timeout=2):
298298 global wled_connected , status_message , last_error , last_errno , in_flight
299299 if in_flight :
300300 return False
301- in_flight = True
302301 if not wifi_connected or not wlan or not wlan .isconnected ():
303- in_flight = False
304302 return False
305303 if not WLED_HOST :
306- in_flight = False
307304 return False
305+ # Set in_flight only after passing all early-return preconditions.
306+ # This keeps the flag logic simple (no need to reset it in those paths)
307+ # and ensures that once marked, it is always cleared by the finally block.
308+ in_flight = True
308309 try :
309310 import json
310311 url = f"http://{ WLED_HOST } /json/state"
@@ -314,29 +315,27 @@ def send_wled_command(data, timeout=2):
314315 success = resp .status_code in (200 , 201 )
315316 # Ensure we release underlying resources early.
316317 resp .close ()
317- in_flight = False
318318 status_message = "Command sent" if success else f"HTTP { resp .status_code } "
319319 return success
320320 except Exception as e :
321- in_flight = False
322321 last_error = str (e )
323322 status_message = f"Cmd err: { str (e )[:10 ]} "
324323 return False
324+ finally :
325+ in_flight = False
325326
326327
327328def http_request (path , timeout = 1 ):
328329 """Minimal GET helper with adjustable timeout. Returns JSON or None."""
329330 global wled_connected , wifi_connected , status_message , last_error , last_errno , in_flight
330331 if in_flight :
331332 return None
332- in_flight = True
333333 if not wifi_connected or not wlan or not wlan .isconnected ():
334334 wifi_connected = False
335- in_flight = False
336335 return None
337336 if not WLED_HOST : # no IP configured
338- in_flight = False
339337 return None
338+ in_flight = True
340339 try :
341340 url = f"http://{ WLED_HOST } { path } "
342341 resp = rq .get (url , timeout = timeout )
@@ -347,13 +346,11 @@ def http_request(path, timeout=1):
347346 import json
348347 data = json .loads (raw_text )
349348 wled_connected = True
350- in_flight = False
351349 return data
352350 except Exception as e :
353351 resp .close ()
354352 wled_connected = False
355353 status_message = f"JSON parse: { str (e )[:10 ]} "
356- in_flight = False
357354 return None
358355 resp .close ()
359356 wled_connected = False
@@ -374,7 +371,8 @@ def http_request(path, timeout=1):
374371 last_error = str (e )
375372 last_errno = None
376373 status_message = f"Err { truncate_message (last_error )} "
377- in_flight = False
374+ finally :
375+ in_flight = False
378376 return None
379377
380378
0 commit comments