Skip to content

RSDK-9327: Dynamically check safety_heartbeat_monitored RPC option to decide if Sessions should be used #5011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 11, 2025

Conversation

aldenh-viam
Copy link
Contributor

@aldenh-viam aldenh-viam commented May 28, 2025

The current client implementation checks a blacklist on each RPC call:

var exemptFromSession = map[string]bool{
"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo": true,
"/proto.rpc.webrtc.v1.SignalingService/Call": true,
"/proto.rpc.webrtc.v1.SignalingService/CallUpdate": true,
"/proto.rpc.webrtc.v1.SignalingService/OptionalWebRTCConfig": true,
"/proto.rpc.v1.AuthService/Authenticate": true,
"/proto.rpc.v1.ExternalAuthService/AuthenticateTo": true,
"/viam.robot.v1.RobotService/ResourceNames": true,
"/viam.robot.v1.RobotService/ResourceRPCSubtypes": true,
"/viam.robot.v1.RobotService/StartSession": true,
"/viam.robot.v1.RobotService/SendSessionHeartbeat": true,
}
, and then tries StartSession for everything else:
startResp, err := rc.client.StartSession(
reqCtx,
&startReq,
grpc_retry.WithMax(5),
grpc_retry.WithCodes(codes.InvalidArgument),
)
if err != nil {
if s, ok := status.FromError(err); ok && s.Code() == codes.Unimplemented {
falseVal := false
rc.sessionsSupported = &falseVal
rc.logger.CInfow(ctx, "sessions unsupported; will not try again")

This change, based on viamrobotics/viam-python-sdk#857, tests ex-ante if the safety_heartbeat_monitored RPC option is set (example), and will only start try StartSession if so (true = session; false/unspecified = no session). Reminder: one session at most per client (it initiates with the first RPC call that requires one).

Note, useSessionInRequest (and thus the new isSafetyHeartbeatMonitored) is appears to be called more times than necessary. It's called inside both the Unary (every time) and Stream (on first msg) interceptors before they call sessionMetadata that also calls it. I can look into reworking this if needed.

@aldenh-viam aldenh-viam requested a review from a team May 28, 2025 13:49
@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label May 28, 2025
@aldenh-viam aldenh-viam marked this pull request as draft May 28, 2025 14:08
@aldenh-viam
Copy link
Contributor Author

Tests are failing because MachineStatus no longer starts a session (as expected with this change). Working on updating the tests now.

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@aldenh-viam aldenh-viam marked this pull request as ready for review June 5, 2025 18:37
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 5, 2025
@aldenh-viam aldenh-viam requested a review from a team June 5, 2025 20:23
Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me! Just some questions and nits.

@@ -139,7 +151,7 @@ func (m *SessionManager) UnaryServerInterceptor(
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
if exemptFromSession[info.FullMethod] {
if _, _, isMonitored := m.safetyMonitoredTypeAndMethod(info.FullMethod); !isMonitored {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just use IsSafetyHeartbeatMonitored so you don't have to ignore the first two return values?

Copy link
Contributor Author

@aldenh-viam aldenh-viam Jun 9, 2025

Choose a reason for hiding this comment

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

safetyMonitoredTypeAndMethod retrieves the method descriptor from the Robot (via a registry we manage ourselves), while the other gets it from protobuf's registry (populated as side effects of importing). It should be functionally the same, but the rest of the Interceptors code (m.safetyMonitoredResourceFromUnary/m.safetyMonitoredResourceFromStream) reaches into the Robot as well, so if there are no strong opinions either way, I'll leave it for consistency.

Copy link
Member

@cheukt cheukt left a comment

Choose a reason for hiding this comment

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

generally looks good, can you also update session/doc.go to reflect current behavior?

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 9, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 9, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jun 9, 2025
Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

LGTM. Per offline discussion: do we need sessionStreamClientInterceptor at all anymore? /Are we interested in attaching sessions to streams? Seems like we have no streaming APIs that need "safety" for actuation.

@aldenh-viam
Copy link
Contributor Author

LGTM. Per offline discussion: do we need sessionStreamClientInterceptor at all anymore? /Are we interested in attaching sessions to streams? Seems like we have no streaming APIs that need "safety" for actuation.

Not for now (the only streaming RPC that currently has safety_heartbeat_monitored is viam.component.testecho.v1.TestEchoService/EchoMultiple). But it's possible in the future - imagine a streaming RPC with infrequent messages, but perhaps staying connected is critical?

Copy link
Member

@cheukt cheukt left a comment

Choose a reason for hiding this comment

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

LGTM!

@aldenh-viam aldenh-viam merged commit 73cbfbe into viamrobotics:main Jun 11, 2025
18 checks passed
@aldenh-viam aldenh-viam deleted the RSDK-9327 branch June 11, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants