Skip to content

Harden the crash-handler protocol and skip waits when nothing is live - #139

Open
summeroff wants to merge 3 commits into
streamlabsfrom
fix/lifecycle-protocol-hardening
Open

summeroff wants to merge 3 commits into
streamlabsfrom
fix/lifecycle-protocol-hardening

Conversation

@summeroff

Copy link
Copy Markdown
Contributor

Problem: Short or garbage messages were read past the buffer, a null checker thread could be joined, a forced Desktop shutdown showed a misleading crash dialog, and exit messages on one global pipe could reach a leftover obs64 from another session.

Change: Message is bounds-checked; invalid messages are dropped and logged. New actions OUTPUT_STATE=6 (obs64) and SHUTDOWN_INTENT=7 (Desktop, via markShutdownIntent(pid), replacing terminateCrashHandler). Exit messages go to a per-session pipe, with legacy-pipe fallback for obs64 that never sent OUTPUT_STATE.

Effect: After a shutdown intent, no crash dialog. If nothing is live, obs64 gets the exit message immediately; the restart prompt is unchanged.

Ships with the obs-studio-node orphan-policy PR; Desktop calls markShutdownIntent in a separate PR.

Tests

  • crash-handler-process.exe builds (RelWithDebInfo); clang-format 13.0.1 and 18.1.3 clean
  • Harness 6/6: bad messages dropped, dead PID, nothing-live exit in ~430 ms, legacy fallback, no legacy ack for new protocol, shutdown intent shows no dialog

Not verified: .node module build, macOS, Application Verifier. Known gap: if obs64 exits while the restart prompt is up, it stays until clicked.

🤖 Generated with Claude Code

Messages were not bounds-checked, so short or garbage input read past the buffer, and a null checker thread could be joined. A forced Desktop shutdown showed a misleading crash dialog, and exit messages went to one global pipe that a leftover obs64 from another session could receive.

Messages are now validated and dropped when invalid. terminateCrashHandler is replaced by markShutdownIntent, and obs64 reports output state so the handler can skip waits when nothing is live. Exit messages use a per-session pipe, falling back to the legacy pipe only for obs64 builds that never report output state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved exit-handling defects can terminate live outputs before user confirmation, target a leftover obs64 instance through a shared pipe, and suppress the post-confirmation retry after failed delivery.

4 open issues
What changed in this PR

This PR hardens crash-handler messaging and adds output-aware shutdown handling for Desktop and obs64, alongside the companion obs-studio-node orphan-policy changes.

Changes:

  • Adds bounds checks, malformed-message logging, and null-safe checker-thread joins.
  • Introduces output-state and shutdown-intent messages to support early exit and suppress intentional-shutdown dialogs.
  • Derives Windows exit-pipe names from the session pipe, with conditional legacy fallback.
File Description
crash-handler-process/​socket.hpp Adds a legacy exit-write interface.
crash-handler-process/​process-manager.hpp Declares shutdown-intent and output-state tracking.
crash-handler-process/​process-manager.cpp Handles new messages and output-aware exit decisions.
crash-handler-process/​platforms/​socket-win.hpp Declares legacy pipe storage and shared write helper.
crash-handler-process/​platforms/​socket-win.cpp Derives exit-pipe names and returns actual received bytes.
crash-handler-process/​platforms/​process-win.cpp Guards checker-thread joins against null pointers.
crash-handler-process/​message.hpp Defines new actions and message-validation interfaces.
crash-handler-process/​message.cpp Adds bounds checks for primitive and string reads.
crash-handler-module/​main.js Replaces terminateCrashHandler with markShutdownIntent.

Review effort: Balanced

…shutdown intent synchronous

An early exit now needs every critical process to have reported inactive, and is
only counted as sent when a write succeeded, so the post-dialog send still runs
after a failure. markShutdownIntent writes the pipe synchronously because Desktop
calls app.exit right after it and an async connect never delivered the message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread crash-handler-module/main.js Fixed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The macOS transport can still send exit commands to another session’s shared pipe, while shutdown-intent writes can be lost or remain unprocessed and trigger a false crash dialog.

3 open issues
4 resolved since last review

Review effort: Balanced

Comment on lines +351 to +352
if (this->socket->write(true, buffer) > 0)
return true;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Declining for now. The concern is real, but obs64 on macOS (obs-studio-node#1852) only listens on the single legacy exit FIFO, so a per-session mac path would drop the exit message. Tracked in #140 (obs-studio-node listener first, then crash-handler).

Comment thread crash-handler-module/main.js Outdated
const attempts = 5;
for (let i = 0; i < attempts; i++) {
try {
const fd = fs.openSync(socket_name, 'r+');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 4467f1b. On non-Windows the FIFO is now opened O_WRONLY|O_NONBLOCK, so a missing reader gives ENXIO and retries; 'r+' stays on Windows. Retry sleep is 25 ms to cover the poll gap.

} finally {
fs.closeSync(fd);
}
return true;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Accepted as best-effort, no code change. The window is the final poll of at most 50 ms around Desktop exiting, and the worst case is the existing crash dialog. Closing it needs an ack, which is a protocol change in both repos.

With 'r+' the sender supplies its own reader, so a write could succeed while
the crash handler had the FIFO closed between polls and the message was lost.
ENXIO now triggers the retry; the sleep covers the ~50 ms poll gap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
try {
// Write-only + nonblocking on POSIX so a missing FIFO reader fails with ENXIO and retries.
const flags = process.platform === 'win32' ? 'r+' : fs.constants.O_WRONLY | fs.constants.O_NONBLOCK;
const fd = fs.openSync(socket_name, flags);
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