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
ClaudeAI commented 3 hours ago
Collaborator

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)

  • Node deletion: 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.
  • Reconnect: create_SOL() closes the previous socket and I/O thread first. Before, every reconnect leaked a TCP connection, and init() failed while the old thread was still running.
  • Two threads: 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), and assignResource() ignores duplicates.
  • Link health in TCP mode: judged from the TCP connection itself (keepalive, 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).
  • Connect: timeout 1 s -> 5 s, back-off 2 s -> 30 s.
  • Closed connections: recv() == 0 is now detected as the peer closing the connection.
  • Settings check: deviceNode::validate() compares configuration fields only, not online.

Waiting for the device's reply (1fa1f29, 85ca06a)

  • Reply window: the I/O thread waited about 100 ms for a reply. A slower reply was read with the next request and given to that request's parser, so the sensor ran one reply behind. It now waits up to 2.5 s (E2UART_REPLY_TIMEOUT_MS), with 150 ms between the pieces of a split reply.
  • Late data: data arriving between requests goes to the last request's parser.
  • Sensor wait: a 2D sensor bound to a network link (basicTransport::isNetworkLink()) waits 3 s per attempt (LGS_NETWORK_TO_COUNTER). Local serial ports keep 1 s.
  • Casts: dynamic_cast instead of a static_cast through hardwareResourceSerialProvider (deviceNode2D, deviceNodeCamera).

Device silent on a live connection (617e385, 15c4b9b)

  • Dead path: TCP_USER_TIMEOUT 10 s, so a dead network path is dropped instead of looking alive for many minutes.
  • Silence: after 5 requests in a row without any reply byte, the gateway reconnects.
  • No query lost: a sensor query queued while the thread is starting, during a reconnect, or while connecting is kept and sent on the new connection. sendData() refuses only without any connection. At most 16 requests wait, and a failed reconnect clears them.

Diagnostics (2f017a6, off in 6078ca9)

  • Timing logs: DEBUG_COMMUNICATION in deviceNodeEthernet2Serial.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

  • Ping: tested on its own, with and without signals.
  • Gateway I/O thread: linked from the la_mojo objects and tested against a fake device: delays 50 ms to 2.8 s, replies split in 2 TCP pieces, silent device. Old code: missing or shifted replies. New code: each request gets its own reply, the silent device is reconnected, and queries around a reconnect are delivered. AddressSanitizer: clean.
  • Field test: 231/231 and 166/166 requests per minute answered, average 113-205 ms, spikes up to 1.5 s, one slow reply covered by the retry, 0 failed commands.
  • Build: la_mojo Debug86 builds. The only error is 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

## 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) - **Node deletion:** `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. - **Reconnect:** `create_SOL()` closes the previous socket and I/O thread first. Before, every reconnect leaked a TCP connection, and `init()` failed while the old thread was still running. - **Two threads:** `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), and `assignResource()` ignores duplicates. - **Link health in TCP mode:** judged from the TCP connection itself (keepalive, `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`). - **Connect:** timeout 1 s -> 5 s, back-off 2 s -> 30 s. - **Closed connections:** `recv() == 0` is now detected as the peer closing the connection. - **Settings check:** `deviceNode::validate()` compares configuration fields only, not `online`. ### Waiting for the device's reply (1fa1f29, 85ca06a) - **Reply window:** the I/O thread waited about 100 ms for a reply. A slower reply was read with the next request and given to that request's parser, so the sensor ran one reply behind. It now waits up to 2.5 s (`E2UART_REPLY_TIMEOUT_MS`), with 150 ms between the pieces of a split reply. - **Late data:** data arriving between requests goes to the last request's parser. - **Sensor wait:** a 2D sensor bound to a network link (`basicTransport::isNetworkLink()`) waits 3 s per attempt (`LGS_NETWORK_TO_COUNTER`). Local serial ports keep 1 s. - **Casts:** `dynamic_cast` instead of a `static_cast` through `hardwareResourceSerialProvider` (deviceNode2D, deviceNodeCamera). ### Device silent on a live connection (617e385, 15c4b9b) - **Dead path:** `TCP_USER_TIMEOUT` 10 s, so a dead network path is dropped instead of looking alive for many minutes. - **Silence:** after 5 requests in a row without any reply byte, the gateway reconnects. - **No query lost:** a sensor query queued while the thread is starting, during a reconnect, or while connecting is kept and sent on the new connection. `sendData()` refuses only without any connection. At most 16 requests wait, and a failed reconnect clears them. ### Diagnostics (2f017a6, off in 6078ca9) - **Timing logs:** `DEBUG_COMMUNICATION` in `deviceNodeEthernet2Serial.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 - **Ping:** tested on its own, with and without signals. - **Gateway I/O thread:** linked from the la_mojo objects and tested against a fake device: delays 50 ms to 2.8 s, replies split in 2 TCP pieces, silent device. Old code: missing or shifted replies. New code: each request gets its own reply, the silent device is reconnected, and queries around a reconnect are delivered. AddressSanitizer: clean. - **Field test:** 231/231 and 166/166 requests per minute answered, average 113-205 ms, spikes up to 1.5 s, one slow reply covered by the retry, 0 failed commands. - **Build:** la_mojo Debug86 builds. The only error is `videoLive555AVLibSource.cpp:818`, caused by the newer live555 headers in the check environment, not by this change. Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
ClaudeAI added 7 commits 3 hours ago
09f1480805
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check
1fa1f29a35
Ethernet2UART gateway: wait long enough for the device's reply over the Internet
85ca06a8a2
Sensor behind a network gateway: wait 3 s per attempt instead of 1 s
2f017a64d8
Debug: log communication timing of sensors and the Ethernet2UART gateway
617e38583d
Ethernet2UART gateway: reconnect when the device goes silent on a live connection
15c4b9b6a3
Ethernet2UART gateway: never lose a sensor query around a reconnect
6078ca9659
Turn off the communication timing logs (DEBUG_COMMUNICATION)
ClaudeAI merged commit f695aac506 into master 3 hours ago
The pull request has been merged as f695aac506.
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This pull request currently doesn't have any dependencies.

Loading…
There is no content yet.