Ethernet2UART gateway and sensors over the Internet: reconnect, reply timing, no lost queries #24
Merged
ClaudeAI
merged 7 commits from fix/eth2uart-reconnect into master 3 hours ago
Loading…
Reference in new issue
There is no content yet.
Delete Branch 'fix/eth2uart-reconnect'
Deleting a branch is permanent. It CANNOT be undone. Continue?
Summary
Fixes the communication with sensors behind an Ethernet-to-serial gateway reached over the Internet (
deviceETH2UART, field case 2.54.237.28:8234 on a mobile link).Reconnect loop, leaked connections, ping (
09f1480)deviceNodeFactory::deleteNode()now deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. Nodes are deleted after the validation loop instead of inside it.create_SOL()closes the previous socket and I/O thread first. Before, every reconnect leaked a TCP connection, andinit()failed while the old thread was still running.create()/validate()/reconnect()are serialised, because they run on both the node factory thread and the hardware manager thread. The gateway registers with the hardware manager only once (the second registration deadlocked on its non-recursive mutex), andassignResource()ignores duplicates.SO_ERROR) instead of ICMP ping. In UDP modes the ping makes 3 tries x 2 s, accepts only the gateway's own reply, and keeps waiting through a signal (EINTR).recv() == 0is now detected as the peer closing the connection.deviceNode::validate()compares configuration fields only, notonline.Waiting for the device's reply (
1fa1f29,85ca06a)E2UART_REPLY_TIMEOUT_MS), with 150 ms between the pieces of a split reply.basicTransport::isNetworkLink()) waits 3 s per attempt (LGS_NETWORK_TO_COUNTER). Local serial ports keep 1 s.dynamic_castinstead of astatic_castthroughhardwareResourceSerialProvider(deviceNode2D, deviceNodeCamera).Device silent on a live connection (
617e385,15c4b9b)TCP_USER_TIMEOUT10 s, so a dead network path is dropped instead of looking alive for many minutes.sendData()refuses only without any connection. At most 16 requests wait, and a failed reconnect clears them.Diagnostics (
2f017a6, off in6078ca9)DEBUG_COMMUNICATIONindeviceNodeEthernet2Serial.cpp/deviceNodeSensor.cpp. They show no reply (with request bytes and TCP_INFO), slow and late replies, and one-minute summaries. They are commented out now and can be enabled again for diagnosis.Testing
videoLive555AVLibSource.cpp:818, caused by the newer live555 headers in the check environment, not by this change.Generated with Claude Code
https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16Field log (sensor behind 2.54.237.28:8234): normal replies avg 129 ms, spikes up to 2.6 s handled. Then no reply at all to 19 requests in a row while the TCP connection stayed "up" (sending never failed). The sensor failed after 17 attempts and kept using the dead connection. A dead mobile/NAT path isn't reported by the kernel for many minutes, and keepalive only probes idle connections. - TCP_USER_TIMEOUT 10 s (E2UART_TCP_USER_TIMEOUT_MS): sent data not acknowledged by the gateway drops the connection. - After 5 requests in a row (E2UART_NO_REPLY_RECONNECT) without a single reply byte, the I/O thread ends ("No reply to 5 requests in a row - reconnecting to the gateway"). validate() sees the lost link and opens a new connection. Late data resets the count. - closePort() frees the requests still queued for the closed connection. - "NO reply" log lines (DEBUG_COMMUNICATION) and the reconnect error show TCP_INFO: unacked data, retransmits, rtt, last ACK / last data received. Unacked data and an old last ACK mean the network path is dead; ACKs that keep coming mean the gateway/device side doesn't answer. Tested with a fake device that goes silent after 3 replies while keeping the TCP connection open: the reconnect came after the 5th unanswered request, and requests 9-12 were answered on the new connection (ASan clean). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16The field test is done: replies avg 113-205 ms, spikes up to 1.5 s, one slow reply covered by the retry, 0 failed commands. The #define in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp is commented out; uncomment it to get the timing lines again. The reconnect error ("No reply to N requests in a row") is always logged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16f695aac506.