Improve socket connection robustness in external_control.urscript#446
Improve socket connection robustness in external_control.urscript#446srvald wants to merge 5 commits intoUniversalRobots:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #446 +/- ##
==========================================
- Coverage 76.33% 76.32% -0.01%
==========================================
Files 104 104
Lines 5531 5529 -2
Branches 594 593 -1
==========================================
- Hits 4222 4220 -2
Misses 1010 1010
Partials 299 299
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
urfeex
left a comment
There was a problem hiding this comment.
Thank you for this well-documented change! I'll submit the proposed changes and test it in conjunction with the ROS driver and a looping UR program and then probably merge this.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable autofix in the Cursor dashboard.
|
|
||
| popup("Error connecting sockets", title="Socket error", blocking=True, error=True) | ||
|
|
||
| end |
There was a problem hiding this comment.
Stale socket flags after driver restart during popup
Medium Severity
The connection flags (traj_flag, com_flag) are permanently latched True once socket_open succeeds and are never re-evaluated. When the blocking popup is displayed because a later socket failed, the operator's natural recovery action is to restart the driver, which tears down all server-side sockets and invalidates already-established connections. After clicking Continue, only sockets with False flags are retried — stale sockets are skipped. The script then enters the control loop with a mix of dead and live socket connections, causing silent communication failures on trajectory_socket or script_command_socket operations.
There was a problem hiding this comment.
@srvald I think, that is actually a valid concern. Could you please add closing the sockets that have been opened when showing the dialog?
urfeex
left a comment
There was a problem hiding this comment.
I noticed during testing that when using this with a looping program it can occur that the script program reconnects to the socket before it is properly shutdown on the remote. This way, it keeps hanging in the first read again.
Adding a timeout to the first read would be the obvious way to solve this, but we added that on purpose.
Alternatively, a sleep at the end of the program would help, as well, but also doesn't feel really clean to me.
More documented error popup depending on the socket failing Co-authored-by: Felix Exner <feex@universal-robots.com>
Remove textmsg as external control active message will appear after sockets have been connected successfully Co-authored-by: Felix Exner <feex@universal-robots.com>


Summary
This PR strengthens the socket connection logic in external_control.urscript to make driver startup more reliable and fault‑tolerant.
Issue related: #439
What’s changed
Added a connect_socket_with_retry() helper with configurable attempts and delay.
Enforced the required connection order:
Implemented a loop that retries only the sockets that are not yet connected (no redundant reconnects).
Added a popup notification when any connection attempt fails.
Why
Ensures stable behavior during driver startup under transient network conditions.
Avoids unnecessary reconnection attempts once a socket is successfully established.
Avoids the program to run although the sockets haven't been connected.
Testing
Manual tests were performed using netcat to simulate the three server endpoints:
Notes
The logic explicitly prevents reverse control from opening before the other two sockets are established.
Popup messaging provides clear operator feedback without spamming unnecessary retries.
Note
Medium Risk
Changes startup connection behavior for the three control sockets and can block program start behind a popup/retry loop, so misconfiguration or edge cases could impact driver bring-up.
Overview
Improves
resources/external_control.urscriptstartup robustness by replacing the three one-shotsocket_opencalls with aconnect_socket_with_retry()helper and a loop that enforces ordered connection (trajectory_socket→script_command_socket→reverse_socket).If any socket cannot be established after retries, the script now shows a blocking error popup with per-socket connection status and only retries the sockets that are still disconnected, preventing external control from running with missing connections.
Written by Cursor Bugbot for commit 553e389. This will update automatically on new commits. Configure here.