Skip to content

Recover Adalight/Ardulight serial devices on disconnect and TX stalls (#576) - #665

Open
psieg wants to merge 2 commits into
masterfrom
fix/576-serial-error-recovery
Open

psieg wants to merge 2 commits into
masterfrom
fix/576-serial-error-recovery

Conversation

@psieg

@psieg psieg commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Fixes #576

Summary

When a serial LED controller (such as WLED or an Arduino/USB-serial adapter) is power-cycled or disconnected without a system suspend event, QSerialPort::isOpen() could remain true in a wedged state where bytesToWrite() > 0 caused writeBuffer() and writeLastWill() to skip every frame and never trigger device recovery in LedDeviceManager.

This applies symmetric serial error and TX-stall recovery to both LedDeviceAdalight and LedDeviceArdulight:

  • Single-ownership QSerialPort setup: Allocate QSerialPort once with the device as parent and connect QSerialPort::errorOccurred and QSerialPort::bytesWritten once in initSerialPort() instead of recreating/reconnecting on every open().
  • Safe open() / close() lifecycle: Guard open() with m_isOpening so synchronous open failures only emit openDeviceSuccess(false) (never duplicate ioDeviceSuccess(false)), close the port in open() if setBaudRate() or setDataBits() fails, and stop both m_txStallTimer and m_lastWillTimer on open() and close() (guarded by m_isClosing to avoid re-entrant error emission).
  • Fatal runtime serial error handling: Connect QSerialPort::errorOccurred; on ResourceError, WriteError, DeviceNotFoundError, PermissionError, OpenError, or NotOpenError outside open()/close(), stop timers, close and clear error state on the port, and emit ioDeviceSuccess(false) so LedDeviceManager schedules device recreation and state replay.
  • TX-stall watchdog (2000ms single-shot): Arm m_txStallTimer when a write leaves bytesToWrite() > 0, stop/restart it in onBytesWritten() as bytes drain, and if it fires while isOpen() && bytesToWrite() > 0, log a warning, stop m_lastWillTimer, close the port, clear error state, and emit ioDeviceSuccess(false).

Testing

  • Built Prismatik and LightpackTests with qmake6 -r && make -j2.
  • Ran QT_QPA_PLATFORM=offscreen ./bin/LightpackTests: all 8 test suites passed (GrabCalculationTest, SettingsMigrationTest, LightpackMathTest, LightpackApiTest, AppVersionTest, LightpackCommandLineParserTest, LedDeviceManagerTest [19 passed, 0 failed], PluginHardeningTest).
  • Added unit tests in Software/tests/LedDeviceManagerTest.{hpp,cpp} covering:
    • Async ioDeviceSuccess(false) recovery and state replay (SetColors and StatusOff / OffLeds).
    • LedDeviceAdalight and LedDeviceArdulight open() failure suppression, non-fatal vs fatal QSerialPort::errorOccurred handling, guard checks, and timer cleanup.
    • Linux PTY-backed tests verifying port closure on setBaudRate(-1) failure, watchdog stop/restart on drain, TX-stall watchdog closure + ioDeviceSuccess(false) + reopen recovery, and end-to-end LedDeviceManager disconnect/backoff/reconnect/frame replay.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adalight, doesn't re-intialise the serial after LED power cycle even when switching lights off/on.

1 participant