Skip to content

Suppress INVALID_CLIENT_ID disconnect during R session restart - #17141

Merged
kevinushey merged 2 commits into
mainfrom
bugfix/suppress-rpc-disconnect-during-restart
Mar 2, 2026
Merged

kevinushey merged 2 commits into
mainfrom
bugfix/suppress-rpc-disconnect-during-restart

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Summary

During a deliberate restart, in-flight RPC requests (e.g. the event polling request from RemoteServerEventListener) may return INVALID_CLIENT_ID after the R session resets. Previously this triggered disconnect() and fired ClientDisconnectedEvent, which:

  1. Set the disconnected_ flag, silently breaking the ping loop in waitForSessionRestart that detects when the new session is ready.
  2. Showed the "R Session Disconnected" dialog (a separate bug addressed in Suppress 'R Session Disconnected' dialog on Desktop #17139 / Suppress 'R Session Disconnected' dialog on Desktop and during restart #17140).

Fix

Track restart state in RemoteServer by subscribing to RestartStatusEvent. When a restart is in progress, suppress the disconnect() call and ClientDisconnectedEvent for INVALID_CLIENT_ID errors, since they are expected and transient.

This is a root-cause fix that complements the handler-level guards in #17139 / #17140.

Testing

  • Restart R session and verify no spurious disconnect events are fired
  • Verify waitForSessionRestart ping loop continues operating through the restart window
  • Verify genuine INVALID_CLIENT_ID errors (outside of restart) still trigger disconnect as before

During a deliberate restart, in-flight RPC requests (e.g. the event
polling request) may return INVALID_CLIENT_ID after the R session
resets. Previously this triggered disconnect() and fired
ClientDisconnectedEvent, which:

1. Set the disconnected_ flag, silently breaking the ping loop
   in waitForSessionRestart that detects when the new session is
   ready.
2. Showed the 'R Session Disconnected' dialog (a separate bug).

Track restart state in RemoteServer by subscribing to
RestartStatusEvent. When a restart is in progress, suppress the
disconnect + event for INVALID_CLIENT_ID errors, since they are
expected and transient.
@posit-snyk-bot

posit-snyk-bot commented Mar 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@gtritchie

Copy link
Copy Markdown
Member

Review

The code is clean — import ordering, handler registration pattern, early-return guard, field placement, comments, and style all match existing conventions. No issues from a code quality perspective.

The silent-failure analysis found some deeper concerns worth discussing:

1. restartInProgress_ can get permanently stuck (CRITICAL in theory, LOW in practice)

If the ping loop in SessionOpener.sendPing exhausts its 50 retries without the VoidServerRequestCallback being invoked, onCompleted is never called → RESTART_COMPLETED never fires → restartInProgress_ stays true forever → all future INVALID_CLIENT_ID errors are silently suppressed, including genuine disconnections.

The chain: ping RPC gets INVALID_CLIENT_ID → handleRpcErrorInternally returns true → sendRequest treats error as "handled" and does NOT call the responseHandler → VoidServerRequestCallback.onFailure() never fires → pingInFlight_ stays true → loop runs 50 empty iterations → stops without calling onCompleted.

In practice, this is unlikely because the ping loop starts after the suspend RPC succeeds, by which point the session is shutting down and pings would get transport-level errors (connection refused), not INVALID_CLIENT_ID responses. The INVALID_CLIENT_ID scenario is primarily about the already-in-flight get_events long-poll.

However, sendPing not calling onCompleted on retry exhaustion is a pre-existing gap worth fixing regardless:

// SessionOpener.java:216-219 — currently:
if (retries_++ > maxRetries)
{
   Debug.logWarning("Error connecting with session.");
   return false;  // onCompleted never called
}
// Should call onCompleted before returning false

2. Event listener left in zombie state after suppression (HIGH)

If the event listener's get_events long-poll gets INVALID_CLIENT_ID suppressed, its ServerRequestCallback is never notified — isListening_ stays true but no HTTP request is active. Server events are missed until the Watchdog timer fires (triggered by an RPC with eventsPending).

This is the most likely scenario to actually occur, since get_events is the long-poll that's in-flight when the restart starts. A simple mitigation: call serverEventListener_.stop() in the suppression block when the scope is events, so isListening_ resets and ensureListening can restart it:

if (restartInProgress_)
{
   serverEventListener_.stop();
   return true;
}

3. No logging of suppressed errors (MEDIUM)

The suppression produces zero runtime evidence. Adding a Debug.log with the RPC method name would make restart debugging much easier:

if (restartInProgress_)
{
   Debug.log("Suppressed INVALID_CLIENT_ID during restart");
   return true;
}

4. No interaction guard between state flags (MEDIUM)

If disconnect() is called for a different reason (e.g., INVALID_CLIENT_VERSION) while restartInProgress_ is true, the flags end up in an untested combination. Consider clearing restartInProgress_ in disconnect():

public void disconnect()
{
   disconnected_ = true;
   restartInProgress_ = false;
   serverEventListener_.stop();
   ...
}

Verdict

The approach is architecturally sound — suppressing disconnect() during restart is the right fix. Issues 2 and 3 are the most actionable: stopping the event listener cleanly and adding a debug log are both low-risk additions. Issue 1 is more of a pre-existing sendPing gap than a problem introduced by this PR.

This PR complements #17140 well — that one guards the UI layer, this one prevents the underlying state corruption.

- Stop the event listener when suppressing INVALID_CLIENT_ID during
  restart, so the get_events long-poll doesn't leave isListening_ in
  a zombie state (ensureListening can restart it when needed).

- Add Debug.log with the RPC method name when suppressing, so there
  is runtime evidence of the suppression for debugging.

- Clear restartInProgress_ in disconnect(), so a real disconnect for
  another reason (e.g. INVALID_CLIENT_VERSION) during restart resets
  the flag and doesn't suppress future INVALID_CLIENT_ID handling.

See: #17141 (comment)

The pre-existing gap in SessionOpener.sendPing (onCompleted not called
on retry exhaustion) is tracked separately in #17143.
@gtritchie gtritchie added this to the Globemaster Allium milestone Mar 1, 2026

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@kevinushey
kevinushey merged commit 12d14f3 into main Mar 2, 2026
8 checks passed
@kevinushey
kevinushey deleted the bugfix/suppress-rpc-disconnect-during-restart branch March 2, 2026 00:34
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.

3 participants