Skip to content

Add a scoped receive timeout to TCPSocket - #628

Open
guti97 wants to merge 6 commits into
UniversalRobots:masterfrom
guti97:feature/scoped-receive-timeout
Open

guti97 wants to merge 6 commits into
UniversalRobots:masterfrom
guti97:feature/scoped-receive-timeout

Conversation

@guti97

@guti97 guti97 commented Nov 18, 2026

Copy link
Copy Markdown

Several places changed a socket's receive timeout for a single request and restored it by hand. In the G5 dashboard client, commandGenerateFlightReport() and commandGenerateSupportFile() only restored it after a normal return or an UnexpectedResponse. A TimeoutException or UrException from the request left the 180 s / 600 s timeout active for all later commands.

TCPSocket::temporaryReceiveTimeout() now returns a ScopedReceiveTimeout guard that restores the previous timeout when it goes out of scope, including during exception unwinding. If no timeout was set before, the socket has no receive timeout afterwards. The restore does not allocate, as it runs in a destructor. An overload takes a std::chrono duration, and comm::toTimeval() converts durations to timeval.

Changes per component:

  • RTDEClient: the file-local ScopedMinimumReceiveTimeout is replaced by the new guard
  • DashboardClientImplG5: connect() and the long-running commands use the guard, which removes seven manual restores
  • DashboardClientImplX: does not use TCPSocket, so it gets a local RAII guard that replaces the catch (...)/rethrow blocks

Also initialises the timeval in DashboardClientImplG5::getConfiguredReceiveTimeout(), which left tv_usec uninitialised when no timeout was configured.

Tests

  • tcp_socket_tests: the guard restores a previous and an unset timeout, also on exceptions; a moved guard restores once; the chrono overload and toTimeval() work; the timeout is applied to a connected socket
  • dashboard_client_mock_tests: regression test with a fake dashboard server that shuts down during commandGenerateFlightReport(); without this change the timeout stays at 180 s

The G5 integration tests against URSim were not run locally.

Closes #609

Several places changed a socket's receive timeout for a single request and
restored it by hand. In the G5 dashboard client, commandGenerateFlightReport()
and commandGenerateSupportFile() only restored it after a normal return or an
UnexpectedResponse. A TimeoutException or UrException from the request left
the 180 s / 600 s timeout active for all later commands.

TCPSocket::temporaryReceiveTimeout() now returns a ScopedReceiveTimeout guard
that restores the previous timeout when it goes out of scope, including during
exception unwinding. If no timeout was set before, the socket has no receive
timeout afterwards. The restore does not allocate, as it runs in a destructor.
An overload takes a std::chrono duration, and comm::toTimeval() converts
durations for the remaining callers.

Users of the new guard:
- RTDEClient: replaces the file-local ScopedMinimumReceiveTimeout
- DashboardClientImplG5: connect() and the long-running commands, which
  removes seven manual restores
- DashboardClientImplX: does not use TCPSocket, so it gets a local guard that
  replaces the catch(...)/rethrow blocks

Also initialises the timeval in DashboardClientImplG5::
getConfiguredReceiveTimeout(), which left tv_usec uninitialised when no
timeout was configured.

Closes UniversalRobots#609

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012icwri1zuEqdphNtL4XVuT
Copilot AI balanced review requested due to automatic review settings November 18, 2026 07:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@guti97
guti97 marked this pull request as ready for review November 18, 2026 13:40
@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core socket I/O and dashboard/RTDE handshake paths; behavior change is intentional (timeout always restored) with broad new tests, but long-running command timeouts are sensitive to regressions.

Overview
Adds RAII-scoped receive timeouts on TCPSocket via ScopedReceiveTimeout and temporaryReceiveTimeout(), plus comm::toTimeval() for chrono conversion. The guard restores the prior timeout (or “no timeout”) when it is destroyed, including on exceptions; restore uses silent setsockopt so destructors do not log or allocate.

Call sites drop hand-rolled save/restore: G5 dashboard connect() (welcome polling), long generate flight report / generate support file commands; RTDE boot handshake (raiseReceiveTimeout replaces ScopedMinimumReceiveTimeout); PolyScope X gets a local ScopedDashboardReceiveTimeout for commandPowerOn and commandGenerateFlightReport. G5 also zero-inits getConfiguredReceiveTimeout()’s timeval.

Tests cover guard restore (including exceptions, moves, connected sockets, failed setsockopt) and a G5 regression where a failed flight report must not leave a 180 s timeout active.

Reviewed by Cursor Bugbot for commit a081b69. Bugbot is set up for automated code reviews on this repo. Configure here.

@mergify

mergify Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

@urfeex
urfeex requested a review from urrsk November 23, 2026 12:20
As a nested class the guard can reach the socket's private receive
timeout and restore path, so the two friend declarations between the
guard and TCPSocket are no longer needed. The constructor becomes
public; temporaryReceiveTimeout() stays the usual way to create one.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the scoped receive-timeout changes.

No open issues 🎉

Review effort: Balanced

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.54930% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.97%. Comparing base (bbbe4f5) to head (a081b69).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/ur/dashboard_client_implementation_g5.cpp 80.95% 0 Missing and 4 partials ⚠️
src/comm/tcp_socket.cpp 96.96% 0 Missing and 1 partial ⚠️
src/rtde/rtde_client.cpp 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #628      +/-   ##
==========================================
- Coverage   84.05%   83.97%   -0.09%     
==========================================
  Files         128      128              
  Lines        8567     8553      -14     
  Branches     3914     3908       -6     
==========================================
- Hits         7201     7182      -19     
- Misses        955      959       +4     
- Partials      411      412       +1     
Flag Coverage Δ
python_scripts 49.69% <ø> (ø)
start_ursim 88.86% <ø> (-1.55%) ⬇️
unit 64.69% <87.14%> (+0.42%) ⬆️
ur20-latest 81.71% <87.32%> (+0.08%) ⬆️
ur5-3.14.3 81.56% <87.32%> (+0.19%) ⬆️
ur5e-10.11.0 77.65% <85.91%> (+0.49%) ⬆️
ur5e-10.12.0 ?
ur5e-10.7.0 77.33% <85.91%> (+0.51%) ⬆️
ur5e-5.9.4 81.68% <87.32%> (+0.11%) ⬆️
ur7e-10.13.0 78.87% <88.73%> (+0.30%) ⬆️
ur7e-10.14.0 79.15% <88.73%> (+0.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@urrsk

urrsk commented Nov 25, 2026

Copy link
Copy Markdown
Member

Hi @guti97
Thanks for the PR and your contribution.
I have made a small change to the design as what to avoid having friend classes, to avoid complex class structures in the future.
As the ScopedTimeout only is used togher with the TCPSocket, I have moved it and made it a nested class instead.

Run the SO_RCVTIMEO check for an unset timeout on Windows as well, and
add a read that blocks until data arrives after the guard restored an
unset timeout. Also check that a long-running G5 dashboard command
reports its temporary timeout when it times out.
@urrsk urrsk added the enhancement New feature or request label Nov 25, 2026
Copilot AI balanced review requested due to automatic review settings November 25, 2026 14:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The cleanup guard can terminate the process if socket-option restoration triggers an allocation or log-handler exception in its implicitly noexcept destructor, so restoration must use the non-logging socket wrapper.

1 open issue

Review effort: Balanced

Comment thread src/comm/tcp_socket.cpp Resolved
The guard restores the timeout in its destructor, also while an
exception unwinds. setupOptions() logs a warning when a socket option
cannot be set, which allocates and calls a log handler that may throw,
so a failing setsockopt could terminate the process. The restore now
only sets SO_RCVTIMEO and ignores errors, and is noexcept.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 14:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

An exception during timeout setup can prevent the guard from completing construction, leaving the temporary receive timeout stored instead of restoring the previous value.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Restore the previous receive timeout if guard construction throws

src/​comm/​tcp_socket.cpp:641

If a socket-option failure causes warning formatting or a registered log handler to throw, setReceiveTimeout() has already overwritten recv_timeout_. The guard never finishes construction, so its destructor cannot restore the previous timeout and the temporary value remains stored. Catch exceptions from this call, restore previous_ through the non-throwing restoreReceiveTimeout() helper, and rethrow.

1 resolved since last review

Review effort: Balanced

Comment thread tests/test_tcp_socket.cpp Resolved
Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The timeout guard can fail during construction without restoring the previous receive timeout, leaving the temporary value in effect for later requests or reconnects.

1 open issue

Review effort: Balanced

Comment thread src/comm/tcp_socket.cpp
{
previous_ = std::make_unique<timeval>(*socket.recv_timeout_);
}
socket.setReceiveTimeout(timeout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add temporary receive timeout increase to TCPSocket

4 participants