Skip to content

fix(interactive)!: let a managed arf remove its IPC socket and session registration when its agent closes - #1991

Merged
eitsupi merged 3 commits into
mainfrom
fix/interactive-managed-arf-graceful-exit
Nov 22, 2026
Merged

eitsupi merged 3 commits into
mainfrom
fix/interactive-managed-arf-graceful-exit

Conversation

@eitsupi

@eitsupi eitsupi commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

When an agent closed, it sent SIGKILL to the arf it had started. arf then could not remove its IPC socket and its session registration, which were left behind. This happened when the agent closed after its last client left, when it received SIGTERM or SIGINT, and when it failed to start.

Closing now ends a managed arf as Stop does:

  • arf is asked to shut down over IPC, on a connection that reaches the arf that was started: every attach must report the PID of the started process. If arf is still starting, closing waits briefly for its endpoint.
  • If IPC cannot be used, arf receives SIGTERM on Linux and macOS. On Windows, where Node can only terminate a process forcibly, the IPC shutdown is the only way for arf to exit cleanly.
  • An arf that has not exited after its grace period is killed. The whole sequence runs within one deadline, and a Stop in progress is shared rather than repeated.
  • The backend removes its runtime files only after the exit of arf is confirmed, and keeps them otherwise.
  • An agent that receives SIGTERM or SIGINT again while it is closing keeps waiting instead of exiting at once.

An adopted arf is still never asked to shut down or sent a signal when its agent closes.

Supported arf executables

r.interactive.arfPath must name arf itself, or a script that replaces itself with arf using exec. A launcher that starts arf as another process and waits for it is not supported, and a managed session now refuses it: when arf reports another PID than the process that was started, the session fails to start with an error that asks to change the setting, and its disposal reports that the exit of that arf is unconfirmed. Signals and exit confirmation concern only the started process.

This is a breaking change for such launchers, which is acceptable while R Interactive is experimental. On main such a launcher could start a session, but closing then waited for the launcher rather than arf. Following a second process instead would require deferring the launcher's exit, probing a PID that may have been reused, and deciding when Stop may be retried, which is not worth supporting. The setting's description and the R Interactive guide state the supported forms.

… registration when its agent closes

Closing an agent sent SIGKILL to the arf it had started, so arf could
not remove its IPC socket and its session registration. This happened
when the agent closed after its last client left, on SIGTERM or SIGINT,
and when it failed to start.

Closing now ends a managed arf as Stop does, within one deadline. arf is
asked to shut down over IPC on a connection that reaches the arf that
was started, waiting briefly for its endpoint if arf is still starting.
If IPC cannot be used, arf receives SIGTERM on Linux and macOS, where
Node can only terminate it forcibly on Windows. arf that has not exited
after its grace period is killed. The backend removes its runtime files
only after arf has exited, and an agent that receives SIGTERM or SIGINT
again while closing keeps waiting. An adopted arf is still never asked
to shut down or sent a signal when its agent closes.
@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 15:20
@eitsupi
eitsupi marked this pull request as ready for review November 22, 2026 15:20

@renkun-ken renkun-ken 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.

The direct-launch cleanup tests passed on macOS with arf 0.7.2. I’m requesting changes because I reproduced a managed R process surviving agent close with a configured launcher, and the repeated-signal test also passed with the old one-shot handler restored. The inline comments cover the process identity, cleanup ordering, and test changes.

Comment thread src/interactive/backends/process.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/backends/sessBackend.ts Resolved
…firm its exit before removing its files

A managed arf was verified on its first attach only by its session ID,
which that attach recorded, so a socket path served by another arf at
that moment could be accepted, and closing would ask that arf to shut
down. The PID in arf's readiness record now identifies it: every attach,
including one made while closing during startup, must report that PID.
This also lets closing shut down over IPC an arf that the configured
executable launches as its child.

The exit of R is confirmed only once the started process has exited and
the PID that arf reported no longer exists. The exit of a launcher alone
no longer ends the session or completes Stop or closing. Signals still go
only to the started process, since a PID it did not start may have been
reused.

A backend whose managed R exit cannot be confirmed now keeps its runtime
files, which that R may still use.

The test of a second SIGTERM during closing now holds R in a cell until
both signals were sent, and probes arf before R is busy, so that it
fails with a one-shot signal handler.
@eitsupi
eitsupi requested a review from renkun-ken November 22, 2026 15:54

@renkun-ken renkun-ken 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.

The readiness PID now identifies the responder on the first attach, the backend retains its directory when disposal rejects, and the repeated-signal test fails with the old one-shot handler restored. All 77 backend tests and five focused real-R tests passed locally. I’m requesting changes because I could still make close report success while a wrapped arf was starting, and I could leave a running session stuck in stopping after its IPC endpoint became available again.

Comment thread src/interactive/backends/process.ts Resolved
Comment thread src/interactive/backends/arf.ts Resolved
Comment thread src/interactive/backends/process.ts Resolved
Supporting a launcher that starts arf as another process meant tracking
a second PID: deferring the launcher's exit, probing a PID that may have
been reused, and deciding when Stop may be retried. Each of these had
its own races and kept adding cases.

r.interactive.arfPath must now be arf itself, or a script that replaces
itself with arf using exec. When arf reports another PID than the
process that was started, the managed session fails to start with an
error that says so, and its disposal reports that the exit of that arf is
unconfirmed, keeping the runtime files. Signals and exit confirmation
concern only the started process, and every attach must report its PID.
The documentation of the setting and the R Interactive guide say which
executables are supported.

BREAKING CHANGE: a launcher that starts arf as another process, such as
a shell script that runs arf without exec, can no longer start a managed
session.
@eitsupi

eitsupi commented Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

@renkun-ken Launcher support is removed in 4796db6 instead of being patched further.

All three findings in this review come from following a second process: the launcher exiting before readiness, reuse of the launched arf's PID, and Stop recovery through a wrapper. Each fix in that direction has exposed another race, and supporting it would keep this lifecycle code a source of bugs. It should not be supported.

r.interactive.arfPath must now be arf itself, or a script that replaces itself with arf using exec. A launcher that starts arf as another process is refused at startup with an error that says so, and its disposal reports the exit as unconfirmed and keeps the runtime files. Signals and exit confirmation concern only the started process, so none of the three cases arises. The setting's description and the R Interactive guide document the supported forms.

Launcher configurations are out of scope, here and in follow-ups. Please limit further review of this PR to the direct-launch behavior.

@eitsupi eitsupi changed the title fix(interactive): let a managed arf remove its IPC socket and session registration when its agent closes fix(interactive)!: let a managed arf remove its IPC socket and session registration when its agent closes Nov 22, 2026

@renkun-ken renkun-ken 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.

Approved at 4796db6. Requiring managed sessions to start arf itself, directly or through exec, resolves my earlier launcher findings. The setting and guide document the compatibility change, and the regression test checks the diagnostic for a launcher that reports a different PID.

I tested both supported forms on macOS with arf 0.7.2. Closing an idle session, closing after its IPC socket became unavailable, and closing while R was still in its startup profile all terminated the owned PID and left its registry empty. I also verified that closing an adopted session leaves arf running. All 75 backend tests and five focused real-R tests passed locally.

@eitsupi
eitsupi merged commit d49d387 into main Nov 22, 2026
16 checks passed
@eitsupi
eitsupi deleted the fix/interactive-managed-arf-graceful-exit branch November 22, 2026 16:53
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