Codebase review fixes: stability, performance, quality improvements
Stability: Add outer try/except/finally with _running=False cleanup to all 6
processing loop methods (live, color_strip, effect, audio, composite, mapped).
Add exponential backoff on consecutive capture errors in live_stream. Move
audio stream.stop() outside lock scope.
Performance: Replace per-pixel Python loop with np.array().tobytes() in
ddp_client. Vectorize pixelate filter with cv2.resize down+up. Vectorize
gradient rendering with np.searchsorted.
Frontend: Add lockBody/unlockBody re-entrancy counter. Add {once:true} to
fetchWithAuth abort listener. Null ws.onclose before ws.close() in LED preview.
Backend: Remove auth token prefix from log messages. Add atomic_write_json
helper (tempfile + os.replace) and update all 10 stores. Add name uniqueness
checks to all update methods. Fix DELETE status codes to 204 in audio_sources
and value_sources. Fix get_source() silent bug in color_strip_sources.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
54
CODEBASE_REVIEW.md
Normal file
54
CODEBASE_REVIEW.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# Codebase Review — 2026-02-26
|
||||
|
||||
Findings from full codebase review. Items ordered by priority within each category.
|
||||
|
||||
## Stability (Critical)
|
||||
|
||||
- [x] **Fatal loop exception leaks resources** — Added outer `try/except/finally` with `self._running = False` to all 10 processing loop methods across `live_stream.py`, `color_strip_stream.py`, `effect_stream.py`, `audio_stream.py`, `composite_stream.py`, `mapped_stream.py`. Also added per-iteration `try/except` where missing.
|
||||
- [x] **`_is_running` flag cleanup** — Fixed via `finally: self._running = False` in all loop methods. *(Race condition via `threading.Event` deferred — current pattern sufficient with the finally block.)*
|
||||
- [x] **`ColorStripStreamManager` thread safety** — **FALSE POSITIVE**: All access is from the async event loop; methods are synchronous with no `await` points, so no preemption is possible.
|
||||
- [x] **Audio `stream.stop()` called under lock** — Moved `stream.stop()` outside lock scope in both `release()` and `release_all()` in `audio_capture.py`.
|
||||
- [x] **WS accept-before-validate** — **FALSE POSITIVE**: All WS endpoints validate auth and resolve configs BEFORE calling `websocket.accept()`.
|
||||
- [x] **Capture error no-backoff** — Added consecutive error counter with exponential backoff (`min(1.0, 0.1 * (errors - 5))`) in `ScreenCaptureLiveStream._capture_loop()`.
|
||||
- [ ] **WGC session close not detected** — Deferred (Windows-specific edge case, low priority).
|
||||
- [x] **`LiveStreamManager.acquire()` not thread-safe** — **FALSE POSITIVE**: Same as ColorStripStreamManager — all access from async event loop, no await in methods.
|
||||
|
||||
## Performance (High Impact)
|
||||
|
||||
- [x] **Per-pixel Python loop in `send_pixels()`** — Replaced per-pixel Python loop with `np.array().tobytes()` in `ddp_client.py`. Hot path already uses `send_pixels_numpy()`.
|
||||
- [ ] **WGC 6MB frame allocation per callback** — Deferred (Windows-specific, requires WGC API changes).
|
||||
- [x] **Gradient rendering O(LEDs×Stops) Python loop** — Vectorized with NumPy: `np.searchsorted` for stop lookup + vectorized interpolation in `_compute_gradient_colors()`.
|
||||
- [x] **`PixelateFilter` nested Python loop** — Replaced with `cv2.resize` down (INTER_AREA) + up (INTER_NEAREST) — pure C++ backend.
|
||||
- [x] **`DownscalerFilter` double allocation** — **FALSE POSITIVE**: Already uses single `cv2.resize()` call (vectorized C++).
|
||||
- [x] **`SaturationFilter` ~25MB temp arrays** — **FALSE POSITIVE**: Already uses pre-allocated scratch buffer and vectorized in-place numpy.
|
||||
- [x] **`FrameInterpolationFilter` copies full image** — **FALSE POSITIVE**: Already uses vectorized numpy integer blending with image pool.
|
||||
- [x] **`datetime.utcnow()` per frame** — **LOW IMPACT**: ~1-2μs per call, negligible at 60fps. Deprecation tracked under Backend Quality.
|
||||
- [x] **Unbounded diagnostic lists** — **FALSE POSITIVE**: Lists are cleared every 5 seconds (~300 entries max at 60fps). Trivial memory.
|
||||
|
||||
## Frontend Quality
|
||||
|
||||
- [x] **`lockBody()`/`unlockBody()` not re-entrant** — Added `_lockCount` reference counter and `_savedScrollY` in `ui.js`. First lock saves scroll, last unlock restores.
|
||||
- [x] **XSS via unescaped engine config keys** — **FALSE POSITIVE**: Both capture template and audio template card renderers already use `escapeHtml()` on keys and values.
|
||||
- [x] **LED preview WS `onclose` not nulled** — Added `ws.onclose = null` before `ws.close()` in `disconnectLedPreviewWS()` in `targets.js`.
|
||||
- [x] **`fetchWithAuth` retry adds duplicate listeners** — Added `{ once: true }` to abort signal listener in `api.js`.
|
||||
- [x] **Audio `requestAnimationFrame` loop continues after WS close** — **FALSE POSITIVE**: Loop already checks `testAudioModal.isOpen` before scheduling next frame, and `_cleanupTest()` cancels the animation frame.
|
||||
|
||||
## Backend Quality
|
||||
|
||||
- [ ] **No thread-safety in `JsonStore`** — Deferred (low risk — all stores are accessed from async event loop).
|
||||
- [x] **Auth token prefix logged** — Removed token prefix from log message in `auth.py`. Now logs only "Invalid API key attempt".
|
||||
- [ ] **Duplicate capture/test code** — Deferred (code duplication, not a bug — refactoring would reduce LOC but doesn't fix a defect).
|
||||
- [x] **Update methods allow duplicate names** — Added name uniqueness checks to `update_template` in `template_store.py`, `postprocessing_template_store.py`, `audio_template_store.py`, `pattern_template_store.py`, and `update_profile` in `profile_store.py`. Also added missing check to `create_profile`.
|
||||
- [ ] **Routes access `manager._private` attrs** — Deferred (stylistic, not a bug — would require adding public accessor methods).
|
||||
- [x] **Non-atomic file writes** — Created `utils/file_ops.py` with `atomic_write_json()` helper (tempfile + `os.replace`). Updated all 10 store files.
|
||||
- [ ] **444 f-string logger calls** — Deferred (performance impact negligible — Python evaluates f-strings very fast; lazy `%s` formatting only matters at very high call rates).
|
||||
- [x] **`get_source()` silent bug** — Fixed: `color_strip_sources.py:_resolve_display_index()` called `picture_source_store.get_source()` which doesn't exist (should be `get_stream()`). Was silently returning `0` for display index.
|
||||
- [ ] **`get_config()` race** — Deferred (low risk — config changes are infrequent user-initiated operations).
|
||||
- [ ] **`datetime.utcnow()` deprecated** — Deferred (functional, deprecation warning only appears in Python 3.12+).
|
||||
- [x] **Inconsistent DELETE status codes** — Changed `audio_sources.py` and `value_sources.py` DELETE endpoints from 200 to 204 (matching all other DELETE endpoints).
|
||||
|
||||
## Architecture (Observations, no action needed)
|
||||
|
||||
**Strengths**: Clean layered design, plugin registries, reference-counted stream sharing, consistent API patterns.
|
||||
|
||||
**Weaknesses**: No backpressure (slow consumers buffer frames), thread count grows linearly, config global singleton, reference counting races.
|
||||
@@ -59,7 +59,7 @@ def verify_api_key(
|
||||
break
|
||||
|
||||
if not authenticated_as:
|
||||
logger.warning(f"Invalid API key attempt: {token[:8]}...")
|
||||
logger.warning("Invalid API key attempt")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid API key",
|
||||
|
||||
@@ -129,7 +129,7 @@ async def stream_capture_test(
|
||||
done_event.set()
|
||||
|
||||
# Start capture in background thread
|
||||
loop = asyncio.get_event_loop()
|
||||
loop = asyncio.get_running_loop()
|
||||
capture_future = loop.run_in_executor(None, _capture_loop)
|
||||
|
||||
start_time = time.perf_counter()
|
||||
@@ -142,6 +142,8 @@ async def stream_capture_test(
|
||||
|
||||
# Check for init error
|
||||
if init_error:
|
||||
stop_event.set()
|
||||
await capture_future
|
||||
await websocket.send_json({"type": "error", "detail": init_error})
|
||||
return
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ async def update_audio_source(
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/api/v1/audio-sources/{source_id}", tags=["Audio Sources"])
|
||||
@router.delete("/api/v1/audio-sources/{source_id}", status_code=204, tags=["Audio Sources"])
|
||||
async def delete_audio_source(
|
||||
source_id: str,
|
||||
_auth: AuthRequired,
|
||||
@@ -143,7 +143,6 @@ async def delete_audio_source(
|
||||
)
|
||||
|
||||
store.delete_source(source_id)
|
||||
return {"status": "deleted", "id": source_id}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ def _resolve_display_index(picture_source_id: str, picture_source_store: Picture
|
||||
if not picture_source_id or depth > 5:
|
||||
return 0
|
||||
try:
|
||||
ps = picture_source_store.get_source(picture_source_id)
|
||||
ps = picture_source_store.get_stream(picture_source_id)
|
||||
except Exception:
|
||||
return 0
|
||||
if isinstance(ps, ScreenCapturePictureSource):
|
||||
|
||||
@@ -152,7 +152,7 @@ async def update_value_source(
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/api/v1/value-sources/{source_id}", tags=["Value Sources"])
|
||||
@router.delete("/api/v1/value-sources/{source_id}", status_code=204, tags=["Value Sources"])
|
||||
async def delete_value_source(
|
||||
source_id: str,
|
||||
_auth: AuthRequired,
|
||||
@@ -171,7 +171,6 @@ async def delete_value_source(
|
||||
)
|
||||
|
||||
store.delete_source(source_id)
|
||||
return {"status": "deleted", "id": source_id}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@@ -222,6 +222,7 @@ class AudioCaptureManager:
|
||||
return
|
||||
|
||||
key = (engine_type, device_index, is_loopback)
|
||||
stream_to_stop = None
|
||||
with self._lock:
|
||||
if key not in self._streams:
|
||||
logger.warning(f"Attempted to release unknown audio capture: {key}")
|
||||
@@ -230,22 +231,27 @@ class AudioCaptureManager:
|
||||
stream, ref_count = self._streams[key]
|
||||
ref_count -= 1
|
||||
if ref_count <= 0:
|
||||
stream.stop()
|
||||
stream_to_stop = stream
|
||||
del self._streams[key]
|
||||
logger.info(f"Removed audio capture {key}")
|
||||
else:
|
||||
self._streams[key] = (stream, ref_count)
|
||||
logger.debug(f"Released audio capture {key} (ref_count={ref_count})")
|
||||
# Stop outside the lock — stream.stop() joins a thread (up to 5s)
|
||||
if stream_to_stop is not None:
|
||||
stream_to_stop.stop()
|
||||
|
||||
def release_all(self) -> None:
|
||||
"""Stop and remove all capture streams. Called on shutdown."""
|
||||
with self._lock:
|
||||
for key, (stream, _) in list(self._streams.items()):
|
||||
streams_to_stop = list(self._streams.items())
|
||||
self._streams.clear()
|
||||
# Stop outside the lock — each stop() joins a thread
|
||||
for key, (stream, _) in streams_to_stop:
|
||||
try:
|
||||
stream.stop()
|
||||
except Exception as e:
|
||||
logger.error(f"Error stopping audio capture {key}: {e}")
|
||||
self._streams.clear()
|
||||
logger.info("Released all audio capture streams")
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -193,12 +193,13 @@ class DDPClient:
|
||||
try:
|
||||
# Send plain RGB — WLED handles per-bus color order conversion
|
||||
# internally when outputting to hardware.
|
||||
# Convert to numpy to avoid per-pixel Python loop
|
||||
bpp = 4 if self.rgbw else 3 # bytes per pixel
|
||||
pixel_bytes = bytearray()
|
||||
for r, g, b in pixels:
|
||||
pixel_bytes.extend((int(r), int(g), int(b)))
|
||||
pixel_array = np.array(pixels, dtype=np.uint8)
|
||||
if self.rgbw:
|
||||
pixel_bytes.append(0) # White channel = 0
|
||||
white = np.zeros((pixel_array.shape[0], 1), dtype=np.uint8)
|
||||
pixel_array = np.hstack((pixel_array, white))
|
||||
pixel_bytes = pixel_array.tobytes()
|
||||
|
||||
total_bytes = len(pixel_bytes)
|
||||
# Align payload to full pixels (multiple of bpp) to avoid splitting
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from wled_controller.core.filters.base import FilterOptionDef, PostprocessingFilter
|
||||
@@ -37,12 +38,12 @@ class PixelateFilter(PostprocessingFilter):
|
||||
|
||||
h, w = image.shape[:2]
|
||||
|
||||
for y in range(0, h, block_size):
|
||||
for x in range(0, w, block_size):
|
||||
y_end = min(y + block_size, h)
|
||||
x_end = min(x + block_size, w)
|
||||
block = image[y:y_end, x:x_end]
|
||||
mean_color = block.mean(axis=(0, 1)).astype(np.uint8)
|
||||
image[y:y_end, x:x_end] = mean_color
|
||||
# Resize down (area averaging) then up (nearest neighbor) —
|
||||
# vectorized C++ instead of per-block Python loop
|
||||
small_w = max(1, w // block_size)
|
||||
small_h = max(1, h // block_size)
|
||||
small = cv2.resize(image, (small_w, small_h), interpolation=cv2.INTER_AREA)
|
||||
pixelated = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
|
||||
np.copyto(image, pixelated)
|
||||
|
||||
return None
|
||||
|
||||
@@ -229,10 +229,12 @@ class AudioColorStripStream(ColorStripStream):
|
||||
"vu_meter": self._render_vu_meter,
|
||||
}
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
try:
|
||||
n = self._led_count
|
||||
|
||||
# Rebuild scratch buffers and pre-computed arrays when LED count changes
|
||||
@@ -283,9 +285,15 @@ class AudioColorStripStream(ColorStripStream):
|
||||
"audio_render_ms": render_ms,
|
||||
"total_ms": read_ms + fft_ms + render_ms,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"AudioColorStripStream render error: {e}")
|
||||
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
time.sleep(max(frame_time - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal AudioColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
# ── Channel selection ─────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -334,6 +334,7 @@ class PictureColorStripStream(ColorStripStream):
|
||||
led_colors = frame_buf
|
||||
return led_colors
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
@@ -473,6 +474,10 @@ class PictureColorStripStream(ColorStripStream):
|
||||
remaining = frame_time - elapsed
|
||||
if remaining > 0:
|
||||
time.sleep(remaining)
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal PictureColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
|
||||
def _compute_gradient_colors(stops: list, led_count: int) -> np.ndarray:
|
||||
@@ -506,30 +511,42 @@ def _compute_gradient_colors(stops: list, led_count: int) -> np.ndarray:
|
||||
c = stop.get("color", [255, 255, 255])
|
||||
return np.array(c if isinstance(c, list) and len(c) == 3 else [255, 255, 255], dtype=np.float32)
|
||||
|
||||
# Vectorized: compute all LED positions at once
|
||||
positions = np.linspace(0, 1, led_count) if led_count > 1 else np.array([0.0])
|
||||
result = np.zeros((led_count, 3), dtype=np.float32)
|
||||
|
||||
for i in range(led_count):
|
||||
p = i / (led_count - 1) if led_count > 1 else 0.0
|
||||
# Extract stop positions and colors into arrays
|
||||
n_stops = len(sorted_stops)
|
||||
stop_positions = np.array([float(s.get("position", 0)) for s in sorted_stops], dtype=np.float32)
|
||||
|
||||
if p <= float(sorted_stops[0].get("position", 0)):
|
||||
result[i] = _color(sorted_stops[0], "left")
|
||||
continue
|
||||
# Pre-compute left/right colors for each stop
|
||||
left_colors = np.array([_color(s, "left") for s in sorted_stops], dtype=np.float32)
|
||||
right_colors = np.array([_color(s, "right") for s in sorted_stops], dtype=np.float32)
|
||||
|
||||
last = sorted_stops[-1]
|
||||
if p >= float(last.get("position", 1)):
|
||||
result[i] = _color(last, "right")
|
||||
continue
|
||||
# LEDs before first stop
|
||||
mask_before = positions <= stop_positions[0]
|
||||
result[mask_before] = left_colors[0]
|
||||
|
||||
for j in range(len(sorted_stops) - 1):
|
||||
a = sorted_stops[j]
|
||||
b = sorted_stops[j + 1]
|
||||
a_pos = float(a.get("position", 0))
|
||||
b_pos = float(b.get("position", 1))
|
||||
if a_pos <= p <= b_pos:
|
||||
# LEDs after last stop
|
||||
mask_after = positions >= stop_positions[-1]
|
||||
result[mask_after] = right_colors[-1]
|
||||
|
||||
# LEDs between stops — vectorized per segment
|
||||
mask_between = ~mask_before & ~mask_after
|
||||
if np.any(mask_between):
|
||||
between_pos = positions[mask_between]
|
||||
# np.searchsorted finds the right stop index for each LED
|
||||
idx = np.searchsorted(stop_positions, between_pos, side="right") - 1
|
||||
idx = np.clip(idx, 0, n_stops - 2)
|
||||
|
||||
a_pos = stop_positions[idx]
|
||||
b_pos = stop_positions[idx + 1]
|
||||
span = b_pos - a_pos
|
||||
t = (p - a_pos) / span if span > 0 else 0.0
|
||||
result[i] = _color(a, "right") + t * (_color(b, "left") - _color(a, "right"))
|
||||
break
|
||||
t = np.where(span > 0, (between_pos - a_pos) / span, 0.0)
|
||||
|
||||
a_colors = right_colors[idx] # A's right color
|
||||
b_colors = left_colors[idx + 1] # B's left color
|
||||
result[mask_between] = a_colors + t[:, np.newaxis] * (b_colors - a_colors)
|
||||
|
||||
return np.clip(result, 0, 255).astype(np.uint8)
|
||||
|
||||
@@ -646,10 +663,12 @@ class StaticColorStripStream(ColorStripStream):
|
||||
_buf_a = _buf_b = None
|
||||
_use_a = True
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
try:
|
||||
anim = self._animation
|
||||
if anim and anim.get("enabled"):
|
||||
speed = float(anim.get("speed", 1.0))
|
||||
@@ -726,10 +745,16 @@ class StaticColorStripStream(ColorStripStream):
|
||||
if colors is not None:
|
||||
with self._colors_lock:
|
||||
self._colors = colors
|
||||
except Exception as e:
|
||||
logger.error(f"StaticColorStripStream animation error: {e}")
|
||||
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
sleep_target = frame_time if anim and anim.get("enabled") else 0.25
|
||||
time.sleep(max(sleep_target - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal StaticColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
|
||||
class ColorCycleColorStripStream(ColorStripStream):
|
||||
@@ -834,10 +859,12 @@ class ColorCycleColorStripStream(ColorStripStream):
|
||||
_buf_a = _buf_b = None
|
||||
_use_a = True
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
try:
|
||||
color_list = self._color_list
|
||||
speed = self._cycle_speed
|
||||
n = self._led_count
|
||||
@@ -865,8 +892,14 @@ class ColorCycleColorStripStream(ColorStripStream):
|
||||
)
|
||||
with self._colors_lock:
|
||||
self._colors = buf
|
||||
except Exception as e:
|
||||
logger.error(f"ColorCycleColorStripStream animation error: {e}")
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
time.sleep(max(frame_time - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal ColorCycleColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
|
||||
class GradientColorStripStream(ColorStripStream):
|
||||
@@ -986,10 +1019,12 @@ class GradientColorStripStream(ColorStripStream):
|
||||
_wave_factors = None # float32 scratch for wave sin result
|
||||
_wave_u16 = None # uint16 scratch for wave int factors
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
try:
|
||||
anim = self._animation
|
||||
if anim and anim.get("enabled"):
|
||||
speed = float(anim.get("speed", 1.0))
|
||||
@@ -1109,7 +1144,13 @@ class GradientColorStripStream(ColorStripStream):
|
||||
if colors is not None:
|
||||
with self._colors_lock:
|
||||
self._colors = colors
|
||||
except Exception as e:
|
||||
logger.error(f"GradientColorStripStream animation error: {e}")
|
||||
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
sleep_target = frame_time if anim and anim.get("enabled") else 0.25
|
||||
time.sleep(max(sleep_target - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal GradientColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
@@ -253,6 +253,7 @@ class CompositeColorStripStream(ColorStripStream):
|
||||
# ── Processing loop ─────────────────────────────────────────
|
||||
|
||||
def _processing_loop(self) -> None:
|
||||
try:
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
@@ -311,3 +312,7 @@ class CompositeColorStripStream(ColorStripStream):
|
||||
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
time.sleep(max(frame_time - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal CompositeColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
@@ -284,11 +284,12 @@ class EffectColorStripStream(ColorStripStream):
|
||||
"aurora": self._render_aurora,
|
||||
}
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
|
||||
try:
|
||||
n = self._led_count
|
||||
if n != _pool_n:
|
||||
_pool_n = n
|
||||
@@ -313,9 +314,15 @@ class EffectColorStripStream(ColorStripStream):
|
||||
|
||||
with self._colors_lock:
|
||||
self._colors = buf
|
||||
except Exception as e:
|
||||
logger.error(f"EffectColorStripStream render error: {e}")
|
||||
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
time.sleep(max(frame_time - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal EffectColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
# ── Fire ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -129,6 +129,8 @@ class ScreenCaptureLiveStream(LiveStream):
|
||||
|
||||
def _capture_loop(self) -> None:
|
||||
frame_time = 1.0 / self._fps if self._fps > 0 else 1.0
|
||||
consecutive_errors = 0
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
@@ -137,17 +139,31 @@ class ScreenCaptureLiveStream(LiveStream):
|
||||
if frame is not None:
|
||||
with self._frame_lock:
|
||||
self._latest_frame = frame
|
||||
consecutive_errors = 0
|
||||
else:
|
||||
# Small sleep when no frame available to avoid CPU spinning
|
||||
time.sleep(0.001)
|
||||
except Exception as e:
|
||||
consecutive_errors += 1
|
||||
logger.error(f"Capture error (display={self._capture_stream.display_index}): {e}")
|
||||
# Backoff on repeated errors to avoid CPU spinning
|
||||
if consecutive_errors > 5:
|
||||
backoff = min(1.0, 0.1 * (consecutive_errors - 5))
|
||||
time.sleep(backoff)
|
||||
continue
|
||||
|
||||
# Throttle to target FPS
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
remaining = frame_time - elapsed
|
||||
if remaining > 0:
|
||||
time.sleep(remaining)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Fatal capture loop error (display={self._capture_stream.display_index}): {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
|
||||
class ProcessedLiveStream(LiveStream):
|
||||
@@ -226,10 +242,11 @@ class ProcessedLiveStream(LiveStream):
|
||||
fps = self.target_fps
|
||||
frame_time = 1.0 / fps if fps > 0 else 1.0
|
||||
|
||||
try:
|
||||
with high_resolution_timer():
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
|
||||
try:
|
||||
source_frame = self._source.get_latest_frame()
|
||||
if source_frame is None or source_frame is cached_source_frame:
|
||||
# Idle tick — run filter chain when any filter requests idle processing
|
||||
@@ -250,9 +267,6 @@ class ProcessedLiveStream(LiveStream):
|
||||
|
||||
# Only publish a new frame when the filter chain produced actual
|
||||
# interpolated output (idle_image advanced past the input buffer).
|
||||
# If every filter passed through, idle_image is still _idle_src_buf —
|
||||
# leave _latest_frame unchanged so consumers that rely on object
|
||||
# identity for deduplication correctly detect no new content.
|
||||
if idle_image is not _idle_src_buf:
|
||||
processed = ScreenCapture(
|
||||
image=idle_image,
|
||||
@@ -299,6 +313,13 @@ class ProcessedLiveStream(LiveStream):
|
||||
)
|
||||
with self._frame_lock:
|
||||
self._latest_frame = processed
|
||||
except Exception as e:
|
||||
logger.error(f"Filter processing error: {e}")
|
||||
time.sleep(0.01)
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal processing loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
|
||||
class StaticImageLiveStream(LiveStream):
|
||||
|
||||
@@ -152,6 +152,7 @@ class MappedColorStripStream(ColorStripStream):
|
||||
# ── Processing loop ─────────────────────────────────────────
|
||||
|
||||
def _processing_loop(self) -> None:
|
||||
try:
|
||||
while self._running:
|
||||
loop_start = time.perf_counter()
|
||||
frame_time = 1.0 / self._fps
|
||||
@@ -210,3 +211,7 @@ class MappedColorStripStream(ColorStripStream):
|
||||
|
||||
elapsed = time.perf_counter() - loop_start
|
||||
time.sleep(max(frame_time - elapsed, 0.001))
|
||||
except Exception as e:
|
||||
logger.error(f"Fatal MappedColorStripStream loop error: {e}", exc_info=True)
|
||||
finally:
|
||||
self._running = False
|
||||
|
||||
@@ -36,7 +36,7 @@ export async function fetchWithAuth(url, options = {}) {
|
||||
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||
const controller = new AbortController();
|
||||
if (fetchOpts.signal) {
|
||||
fetchOpts.signal.addEventListener('abort', () => controller.abort());
|
||||
fetchOpts.signal.addEventListener('abort', () => controller.abort(), { once: true });
|
||||
}
|
||||
const timer = setTimeout(() => controller.abort(), timeout);
|
||||
try {
|
||||
|
||||
@@ -56,17 +56,26 @@ export function setupBackdropClose(modal, closeFn) {
|
||||
modal._backdropCloseSetup = true;
|
||||
}
|
||||
|
||||
let _lockCount = 0;
|
||||
let _savedScrollY = 0;
|
||||
|
||||
export function lockBody() {
|
||||
const scrollY = window.scrollY;
|
||||
document.body.style.top = `-${scrollY}px`;
|
||||
if (_lockCount === 0) {
|
||||
_savedScrollY = window.scrollY;
|
||||
document.body.style.top = `-${_savedScrollY}px`;
|
||||
document.body.classList.add('modal-open');
|
||||
}
|
||||
_lockCount++;
|
||||
}
|
||||
|
||||
export function unlockBody() {
|
||||
const scrollY = parseInt(document.body.style.top || '0', 10) * -1;
|
||||
if (_lockCount <= 0) return;
|
||||
_lockCount--;
|
||||
if (_lockCount === 0) {
|
||||
document.body.classList.remove('modal-open');
|
||||
document.body.style.top = '';
|
||||
window.scrollTo(0, scrollY);
|
||||
window.scrollTo(0, _savedScrollY);
|
||||
}
|
||||
}
|
||||
|
||||
export function openLightbox(imageSrc, statsHtml) {
|
||||
|
||||
@@ -1102,6 +1102,7 @@ function connectLedPreviewWS(targetId) {
|
||||
function disconnectLedPreviewWS(targetId) {
|
||||
const ws = ledPreviewWebSockets[targetId];
|
||||
if (ws) {
|
||||
ws.onclose = null;
|
||||
ws.close();
|
||||
delete ledPreviewWebSockets[targetId];
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ from wled_controller.storage.audio_source import (
|
||||
MonoAudioSource,
|
||||
MultichannelAudioSource,
|
||||
)
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -57,21 +57,14 @@ class AudioSourceStore:
|
||||
|
||||
def _save(self) -> None:
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
sources_dict = {
|
||||
sid: source.to_dict()
|
||||
for sid, source in self._sources.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"audio_sources": sources_dict,
|
||||
"audio_sources": {
|
||||
sid: source.to_dict()
|
||||
for sid, source in self._sources.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save audio sources to {self.file_path}: {e}")
|
||||
raise
|
||||
|
||||
@@ -8,7 +8,7 @@ from typing import Dict, List, Optional
|
||||
|
||||
from wled_controller.core.audio.factory import AudioEngineRegistry
|
||||
from wled_controller.storage.audio_template import AudioCaptureTemplate
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -93,21 +93,14 @@ class AudioTemplateStore:
|
||||
def _save(self) -> None:
|
||||
"""Save all templates to file."""
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
templates_dict = {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"templates": templates_dict,
|
||||
"templates": {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save audio templates to {self.file_path}: {e}")
|
||||
raise
|
||||
@@ -168,6 +161,9 @@ class AudioTemplateStore:
|
||||
template = self._templates[template_id]
|
||||
|
||||
if name is not None:
|
||||
for tid, t in self._templates.items():
|
||||
if tid != template_id and t.name == name:
|
||||
raise ValueError(f"Audio template with name '{name}' already exists")
|
||||
template.name = name
|
||||
if engine_type is not None:
|
||||
template.engine_type = engine_type
|
||||
|
||||
@@ -19,7 +19,7 @@ from wled_controller.storage.color_strip_source import (
|
||||
PictureColorStripSource,
|
||||
StaticColorStripSource,
|
||||
)
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -62,21 +62,14 @@ class ColorStripStore:
|
||||
|
||||
def _save(self) -> None:
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
sources_dict = {
|
||||
sid: source.to_dict()
|
||||
for sid, source in self._sources.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"color_strip_sources": sources_dict,
|
||||
"color_strip_sources": {
|
||||
sid: source.to_dict()
|
||||
for sid, source in self._sources.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save color strip sources to {self.file_path}: {e}")
|
||||
raise
|
||||
|
||||
@@ -8,7 +8,7 @@ from typing import Dict, List, Optional
|
||||
|
||||
from wled_controller.storage.key_colors_picture_target import KeyColorRectangle
|
||||
from wled_controller.storage.pattern_template import PatternTemplate
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -88,21 +88,14 @@ class PatternTemplateStore:
|
||||
def _save(self) -> None:
|
||||
"""Save all templates to file."""
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
templates_dict = {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"pattern_templates": templates_dict,
|
||||
"pattern_templates": {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save pattern templates to {self.file_path}: {e}")
|
||||
raise
|
||||
@@ -180,6 +173,9 @@ class PatternTemplateStore:
|
||||
template = self._templates[template_id]
|
||||
|
||||
if name is not None:
|
||||
for tid, t in self._templates.items():
|
||||
if tid != template_id and t.name == name:
|
||||
raise ValueError(f"Pattern template with name '{name}' already exists")
|
||||
template.name = name
|
||||
if rectangles is not None:
|
||||
template.rectangles = rectangles
|
||||
|
||||
@@ -12,7 +12,7 @@ from wled_controller.storage.picture_source import (
|
||||
ProcessedPictureSource,
|
||||
StaticImagePictureSource,
|
||||
)
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -68,21 +68,14 @@ class PictureSourceStore:
|
||||
def _save(self) -> None:
|
||||
"""Save all streams to file."""
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
streams_dict = {
|
||||
stream_id: stream.to_dict()
|
||||
for stream_id, stream in self._streams.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"picture_sources": streams_dict,
|
||||
"picture_sources": {
|
||||
stream_id: stream.to_dict()
|
||||
for stream_id, stream in self._streams.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save picture sources to {self.file_path}: {e}")
|
||||
raise
|
||||
|
||||
@@ -12,7 +12,7 @@ from wled_controller.storage.key_colors_picture_target import (
|
||||
KeyColorsSettings,
|
||||
KeyColorsPictureTarget,
|
||||
)
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -63,21 +63,14 @@ class PictureTargetStore:
|
||||
def _save(self) -> None:
|
||||
"""Save all targets to file."""
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
targets_dict = {
|
||||
target_id: target.to_dict()
|
||||
for target_id, target in self._targets.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"picture_targets": targets_dict,
|
||||
"picture_targets": {
|
||||
target_id: target.to_dict()
|
||||
for target_id, target in self._targets.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save picture targets to {self.file_path}: {e}")
|
||||
raise
|
||||
|
||||
@@ -10,7 +10,7 @@ from wled_controller.core.filters.filter_instance import FilterInstance
|
||||
from wled_controller.core.filters.registry import FilterRegistry
|
||||
from wled_controller.storage.picture_source import ProcessedPictureSource
|
||||
from wled_controller.storage.postprocessing_template import PostprocessingTemplate
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -92,21 +92,14 @@ class PostprocessingTemplateStore:
|
||||
def _save(self) -> None:
|
||||
"""Save all templates to file."""
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
templates_dict = {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "2.0.0",
|
||||
"postprocessing_templates": templates_dict,
|
||||
"postprocessing_templates": {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save postprocessing templates to {self.file_path}: {e}")
|
||||
raise
|
||||
@@ -189,6 +182,9 @@ class PostprocessingTemplateStore:
|
||||
template = self._templates[template_id]
|
||||
|
||||
if name is not None:
|
||||
for tid, t in self._templates.items():
|
||||
if tid != template_id and t.name == name:
|
||||
raise ValueError(f"Postprocessing template with name '{name}' already exists")
|
||||
template.name = name
|
||||
if filters is not None:
|
||||
# Validate filter IDs
|
||||
|
||||
@@ -7,7 +7,7 @@ from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from wled_controller.storage.profile import Condition, Profile
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -49,18 +49,13 @@ class ProfileStore:
|
||||
|
||||
def _save(self) -> None:
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"profiles": {
|
||||
pid: p.to_dict() for pid, p in self._profiles.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save profiles to {self.file_path}: {e}")
|
||||
raise
|
||||
@@ -81,6 +76,10 @@ class ProfileStore:
|
||||
conditions: Optional[List[Condition]] = None,
|
||||
target_ids: Optional[List[str]] = None,
|
||||
) -> Profile:
|
||||
for p in self._profiles.values():
|
||||
if p.name == name:
|
||||
raise ValueError(f"Profile with name '{name}' already exists")
|
||||
|
||||
profile_id = f"prof_{uuid.uuid4().hex[:8]}"
|
||||
now = datetime.utcnow()
|
||||
|
||||
@@ -116,6 +115,9 @@ class ProfileStore:
|
||||
profile = self._profiles[profile_id]
|
||||
|
||||
if name is not None:
|
||||
for pid, p in self._profiles.items():
|
||||
if pid != profile_id and p.name == name:
|
||||
raise ValueError(f"Profile with name '{name}' already exists")
|
||||
profile.name = name
|
||||
if enabled is not None:
|
||||
profile.enabled = enabled
|
||||
|
||||
@@ -8,7 +8,7 @@ from typing import Dict, List, Optional
|
||||
|
||||
from wled_controller.core.capture_engines.factory import EngineRegistry
|
||||
from wled_controller.storage.template import CaptureTemplate
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -95,23 +95,14 @@ class TemplateStore:
|
||||
def _save(self) -> None:
|
||||
"""Save all templates to file."""
|
||||
try:
|
||||
# Ensure directory exists
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
templates_dict = {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"templates": templates_dict,
|
||||
"templates": {
|
||||
template_id: template.to_dict()
|
||||
for template_id, template in self._templates.items()
|
||||
},
|
||||
}
|
||||
|
||||
# Write to file
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save templates to {self.file_path}: {e}")
|
||||
raise
|
||||
@@ -218,6 +209,9 @@ class TemplateStore:
|
||||
|
||||
# Update fields
|
||||
if name is not None:
|
||||
for tid, t in self._templates.items():
|
||||
if tid != template_id and t.name == name:
|
||||
raise ValueError(f"Template with name '{name}' already exists")
|
||||
template.name = name
|
||||
if engine_type is not None:
|
||||
template.engine_type = engine_type
|
||||
|
||||
@@ -13,7 +13,7 @@ from wled_controller.storage.value_source import (
|
||||
StaticValueSource,
|
||||
ValueSource,
|
||||
)
|
||||
from wled_controller.utils import get_logger
|
||||
from wled_controller.utils import atomic_write_json, get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -59,21 +59,14 @@ class ValueSourceStore:
|
||||
|
||||
def _save(self) -> None:
|
||||
try:
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
sources_dict = {
|
||||
sid: source.to_dict()
|
||||
for sid, source in self._sources.items()
|
||||
}
|
||||
|
||||
data = {
|
||||
"version": "1.0.0",
|
||||
"value_sources": sources_dict,
|
||||
"value_sources": {
|
||||
sid: source.to_dict()
|
||||
for sid, source in self._sources.items()
|
||||
},
|
||||
}
|
||||
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
atomic_write_json(self.file_path, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save value sources to {self.file_path}: {e}")
|
||||
raise
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""Utility functions and helpers."""
|
||||
|
||||
from .file_ops import atomic_write_json
|
||||
from .logger import setup_logging, get_logger
|
||||
from .monitor_names import get_monitor_names, get_monitor_name, get_monitor_refresh_rates
|
||||
from .timer import high_resolution_timer
|
||||
|
||||
__all__ = ["setup_logging", "get_logger", "get_monitor_names", "get_monitor_name", "get_monitor_refresh_rates", "high_resolution_timer"]
|
||||
__all__ = ["atomic_write_json", "setup_logging", "get_logger", "get_monitor_names", "get_monitor_name", "get_monitor_refresh_rates", "high_resolution_timer"]
|
||||
|
||||
34
server/src/wled_controller/utils/file_ops.py
Normal file
34
server/src/wled_controller/utils/file_ops.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""Atomic file write utilities."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def atomic_write_json(file_path: Path, data: dict, indent: int = 2) -> None:
|
||||
"""Write JSON data to file atomically via temp file + rename.
|
||||
|
||||
Prevents data corruption if the process crashes or loses power
|
||||
mid-write. The rename operation is atomic on most filesystems.
|
||||
"""
|
||||
file_path = Path(file_path)
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Write to a temp file in the same directory (same filesystem for atomic rename)
|
||||
fd, tmp_path = tempfile.mkstemp(
|
||||
dir=file_path.parent,
|
||||
prefix=f".{file_path.stem}_",
|
||||
suffix=".tmp",
|
||||
)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=indent, ensure_ascii=False)
|
||||
os.replace(tmp_path, file_path)
|
||||
except BaseException:
|
||||
# Clean up temp file on any error
|
||||
try:
|
||||
os.unlink(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
Reference in New Issue
Block a user