Skip to content

rsession spins at 100% CPU in an error-logging feedback loop when its original stderr becomes unwritable #18398

Open

Description

@kevinushey

Observed behavior

A (dev-config) rsession was found pinned at ~93% CPU for hours, with the session log rotating continuously. The log was an endless stream of:

ERROR system error 5 (Input/output error); OCCURRED AT ... src/cpp/core/system/PosixOutputCapture.cpp:101

with entries only microseconds apart. sample showed one thread (standardStreamCaptureThread) spending nearly all its time in logError(), and the main thread burning additional CPU regex-annotating the captured output in ClientEventQueue::annotateOutput.

Diagnosis

core::system::secureStreamOutput / output capture redirects the session's stdout/stderr into a pipe, and keeps a dup() of the original stderr so output is still echoed to the launching terminal. In standardStreamCaptureThread (PosixOutputCapture.cpp), the echo path handles write failures like this:

  • EPIPE / EBADF: set a "skip further writes" flag and log once;
  • EAGAIN / EINTR: ignore (transient);
  • anything else: LOG_ERROR on every failed write, and keep trying.

When the terminal (pty) that launched the session goes away, writes to the saved stderr descriptor fail with EIO -- which falls into the last bucket. On its own that would merely be noisy, but when a stderr log destination is active (e.g. log-stderr=1 in the dev config, or stderr logging via logging.conf), each logged error is written to fd 2, which is the redirected capture pipe. The capture thread then reads its own error message, tries to echo it to the dead pty, fails with EIO again, logs again -- a self-sustaining feedback loop that spins at full speed forever:

  1. capture thread reads data from the stderr capture pipe;
  2. echo write() to the saved (dead) stderr fd fails with EIO;
  3. LOG_ERROR writes the error to all log destinations, including stderr;
  4. that write lands in the capture pipe; goto 1.

Root cause of the EIO

The launching terminal's pty went away while the session kept running (closed terminal tab/window hosting the dev server; the same applies to any parent whose stderr becomes unwritable). On macOS, once the pty master is closed, writes to the slave side fail with EIO. lsof on the stuck process showed no file descriptors 0-2 at all, consistent with a revoked tty. The bug is not the EIO itself -- terminals legitimately die before long-lived sessions -- but that output capture turns this normal event into an infinite loop.

Proposed fix

Treat any non-transient write error (not just EPIPE/EBADF) as terminal for the echo: set the skip-write flag and log the error once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions