Skip to content

tcp_client_connection: fix server closing the connection#2683

Merged
julianoes merged 1 commit intomavlink:mainfrom
bkueng:fix_tcp_client_reconnect
Oct 9, 2025
Merged

tcp_client_connection: fix server closing the connection#2683
julianoes merged 1 commit intomavlink:mainfrom
bkueng:fix_tcp_client_reconnect

Conversation

@bkueng
Copy link
Copy Markdown
Member

@bkueng bkueng commented Oct 9, 2025

recv() returns 0 when the socket is shut down. The previous logic would immediately try again to call recv(), resulting in busy looping, without ever trying to reconnect.
This patch adds a sleep and periodically tries to reconnect.

Also, it closes the socket when the setup of the connection fails.

There's still a bit of spamming during disconnected state:

    [10:40:20|Info ] TCP connection closed, trying to reconnect... (tcp_client_connection.cpp:215)
    [10:40:22|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
    [10:40:22|Error] Sending message failed (mavsdk_impl.cpp:764)
    [10:40:23|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
    [10:40:23|Error] Sending message failed (mavsdk_impl.cpp:764)
    [10:40:23|Info ] heartbeats timed out (system_impl.cpp:370)
    [10:40:24|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
    [10:40:24|Error] Sending message failed (mavsdk_impl.cpp:764)
    [10:40:25|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
    [10:40:25|Error] Sending message failed (mavsdk_impl.cpp:764)
    [10:40:26|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
    [10:40:26|Error] Sending message failed (mavsdk_impl.cpp:764)
    [10:40:27|Debug] Discovered 1 component(s) (system_impl.cpp:607)

recv() returns 0 when the socket is shut down. The previous logic would
immediately try again to call recv(), resulting in busy looping, without
ever trying to reconnect.
This patch adds a sleep and periodically tries to reconnect.

Also, it closes the socket when the setup of the connection fails.

There's still a bit of spamming during disconnected state:
[10:40:20|Info ] TCP connection closed, trying to reconnect... (tcp_client_connection.cpp:215)
[10:40:22|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
[10:40:22|Error] Sending message failed (mavsdk_impl.cpp:764)
[10:40:23|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
[10:40:23|Error] Sending message failed (mavsdk_impl.cpp:764)
[10:40:23|Info ] heartbeats timed out (system_impl.cpp:370)
[10:40:24|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
[10:40:24|Error] Sending message failed (mavsdk_impl.cpp:764)
[10:40:25|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
[10:40:25|Error] Sending message failed (mavsdk_impl.cpp:764)
[10:40:26|Error] Connect error: Connection refused (tcp_client_connection.cpp:103)
[10:40:26|Error] Sending message failed (mavsdk_impl.cpp:764)
[10:40:27|Debug] Discovered 1 component(s) (system_impl.cpp:607)
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Oct 9, 2025

Copy link
Copy Markdown
Collaborator

@julianoes julianoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I think that makes sense.

Any hints how you discovered this, or what sort of system test I could add to cover this?

@bkueng
Copy link
Copy Markdown
Member Author

bkueng commented Oct 9, 2025

I tested against mavlink-router using its default TCP server (using tcpout://127.0.0.1:5790), and then restarted it.
I think the existing TCP server connection in MAVSDK could easily be used for an automated test. Closing the socket should result in the same behavior.

@julianoes julianoes merged commit a58aaa9 into mavlink:main Oct 9, 2025
54 checks passed
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.

2 participants