Browse Source

Fix NeoPixel SEGV crash loop by checking /dev/mem permissions

The rpi_ws281x C library segfaults when it can't access /dev/mem,
which Python try/except can't catch. Guard with an os.access() check
so the service degrades gracefully (no LEDs) instead of crash-looping.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tuanchris 4 months ago
parent
commit
1620cbc102
1 changed files with 9 additions and 0 deletions
  1. 9 0
      modules/led/dw_led_controller.py

+ 9 - 0
modules/led/dw_led_controller.py

@@ -2,6 +2,7 @@
 Dune Weaver LED Controller - Embedded NeoPixel LED controller for Raspberry Pi
 Dune Weaver LED Controller - Embedded NeoPixel LED controller for Raspberry Pi
 Provides direct GPIO control of WS2812B LED strips with beautiful effects
 Provides direct GPIO control of WS2812B LED strips with beautiful effects
 """
 """
+import os
 import threading
 import threading
 import time
 import time
 import logging
 import logging
@@ -59,6 +60,14 @@ class DWLEDController:
         if self._initialized:
         if self._initialized:
             return True
             return True
 
 
+        # Guard: the rpi_ws281x C library will SEGV if it can't access /dev/mem.
+        # Python try/except can't catch a segfault, so check permissions first.
+        if os.path.exists('/dev/mem') and not os.access('/dev/mem', os.R_OK | os.W_OK):
+            error_msg = "Cannot access /dev/mem — NeoPixel requires root permissions. Run the service with sudo."
+            self._init_error = error_msg
+            logger.warning(error_msg)
+            return False
+
         # Try standard NeoPixel library first (works on Pi 4 and earlier)
         # Try standard NeoPixel library first (works on Pi 4 and earlier)
         # If that fails, fall back to Pi 5-specific library
         # If that fails, fall back to Pi 5-specific library
         neopixel_module = None
         neopixel_module = None