Skip to content

fix(supervisor): reopen the log only when it is actually rotated - #1358

Merged
kvinwang merged 2 commits into
nextfrom
fix/supervisor-log-reopen
Sep 24, 2026
Merged

kvinwang merged 2 commits into
nextfrom
fix/supervisor-log-reopen

Conversation

@kvinwang

@kvinwang kvinwang commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Problem

The log redirect watcher reopened on is_remove() || is_modify(). On Linux, inotify reports the supervisor's own write_all as Modify(Data), so every read buffer cost an fsync and a reopen with nothing rotating. Measured with strace, 8 MiB through one child: 1145 fsyncs and 1146 opens before, 0 and 1 after.

Fix

Reopen only on Remove(_) or Modify(Name(_)). A rename (logrotate's default) is Modify(Name). Truncate-in-place (copytruncate, dstack-vmm's own rotation) needs no reopen because the fd is O_APPEND. Also fixed the stale comment in vmm/src/app.rs.

Verification

cargo test -p supervisor, fmt and clippy clean.

Split out of #1263.

The logrotate watcher matched `is_remove() || is_modify()`, and on Linux
`recommended_watcher` is inotify, where `is_modify()` also matches
`IN_MODIFY` -- which is what the supervisor's own `write_all` generates.
So a chatty child paid a synchronous `fsync` plus a reopen per read
buffer, on its own output, with nothing rotating anything.

Measured with `strace -e trace=fsync,openat` against the real binary,
8 MiB of child output through one supervised process:

    before: 1145 fsync, 1146 opens of a log nothing had rotated
    after:     0 fsync,    1 open

Match the events that mean the path stopped naming the file we hold: a
remove, or a rename. inotify reports `IN_MOVED_FROM` as `Modify(Name(_))`
rather than as a remove, so `logrotate(8)`'s default mode is still
followed. A truncate-in-place rotation -- what `dstack-vmm`'s own
`logrotate` module does, and `logrotate(8)`'s `copytruncate` -- needs no
reopen: the file is opened `O_APPEND`, so the next write lands at its new
end. That is the requirement the vmm module documents, and the "reopens
them when they change" half of its comment was never what made it hold.
@kvinwang
kvinwang merged commit 6445240 into next Sep 24, 2026
11 checks passed
@kvinwang
kvinwang deleted the fix/supervisor-log-reopen branch September 24, 2026 14:25
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.

1 participant