Kernel watchdog, sensor re-init after gateway reconnect, camera keeps node on ONVIF trouble, ONVIF position limits #26
Merged
ClaudeAI
merged 11 commits from fix/camera-keep-node into master 42 minutes ago
Loading…
Reference in new issue
There is no content yet.
Delete Branch 'fix/camera-keep-node'
Deleting a branch is permanent. It CANNOT be undone. Continue?
Combines the three open fix branches (fix/kernel-watchdog -> fix/sensor-reconnect-reinit -> fix/camera-keep-node). All were tested on the real system.
Kernel watchdog (fix/kernel-watchdog)
systemctl --no-blockand is rate limited, IP UART connect has a timeout, the Ethernet2UART gateway validates itself.Sensor after a gateway reconnect (fix/sensor-reconnect-reinit)
Camera (fix/camera-keep-node)
Generated with Claude Code
https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16watchDogDaemon (kernel.cpp) - Client lines are assembled in a buffer. readLine() on the non-blocking socket dropped the bytes of a line that had only partly arrived. - EOF (client closed or crashed without "-1") ends the monitoring of that client. Before, EOF read as "no data", and after the deadline kill(9) went to a PID that may belong to another process by then. - The handler record is zeroed. "finished" was uninitialized (malloc), so the accept loop could pthread_join() a live handler and block. - Finished handlers are joined, freed and erased correctly. erase() inside the for loop skipped the next one, and the records were never freed. A record whose thread could not be created is freed. pthread_create() returns an error number, not -1. - The bind() result is checked. - The log says which PID is watched or stopped, and for a stall how long ago the last update was ("WD update STALLED! (PID n, last update N ms ago)"). kernelPrototype - Sends getpid() as the PID to kill (was gettid(): the same value only in the main thread). Tested with the rebuilt daemon: the kernel scenarios as before (normal, 6 s step: not killed; hang: killed). A client that closes without "-1": its process isn't killed later. An update split into 3 pieces, 6 times with a 4 s deadline: understood, not killed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16Field: the Ethernet2UART gateway connection dropped and reconnected within a second ("Peer Closes Connection!" -> "Connected to ..."), but the 2D sensor stayed offline until la_mojo was restarted. The sensor was re-initialised (MODE_DETECTOR + handshake) only if its thread saw the transport's short CS_DISCONNECTED. On a fast reconnect it did not, and stayed in idle mode, believing the device to be in detector mode (m_bActualDeviceWM) and only polling getState2. A device that left detector mode while the link was down was never set back. A restart starts from MODE_NONE, which is why it helped. - basicTransport::getConnectEpoch(): +1 on every change to CS_CONNECTED. - device2DSensor remembers the epoch when it binds its port (connectMode). validateCommunicationRessource() (every loop pass above connection mode) runs the connection sequence again when the epoch changed: "Transport reconnected - re-initialising the sensor". - deviceETH2UART: "Peer Closes Connection!" says why: "closed by the gateway" (FIN) or "error: <errno>" (e.g. Connection timed out = sent data not acknowledged within E2UART_TCP_USER_TIMEOUT_MS), plus the TCP_INFO figures. Tested with the real gateway class: a device that goes silent (reconnect after 5 unanswered requests) and a gateway that closes the connection. After each reconnect the epoch changes (1 -> 2), so the sensor would re-initialise, and requests are answered again. The close is logged as "closed by the gateway; TCP: ...". ASan clean. The full 2D sensor can't run here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16Field log: the PTZ re-init now keeps the camera and its stream (15:36), but at 16:02:30 "Video Stalled" restarted the live555 stream, which cut the recording. streamHandler::validate() (module factory, every ~5 s) restarted the stream as soon as one validation saw no new frame, i.e. after a single gap of ~5 s. That is normal now and then for a camera on a mobile link. - The stream is restarted after STREAM_STALL_CHECKS (3) validations in a row without a new frame (~15 s): "Video Stalled (no frame for N s) - restarting the stream". Shorter gaps are logged ("Video: no new frame for N s (k/3)", "Video: frames again after N s"). - m_nValidatorFrameCounter is initialised (it was not). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN1674d7cf9ed8.