Skip to content

needs-restarting: Report each process started before a dependency update - #3097

Open
LUX-ETVERITAS wants to merge 2 commits into
rpm-software-management:mainfrom
LUX-ETVERITAS:fix/needs-restarting-process-start-time
Open

LUX-ETVERITAS wants to merge 2 commits into
rpm-software-management:mainfrom
LUX-ETVERITAS:fix/needs-restarting-process-start-time

Conversation

@LUX-ETVERITAS

Copy link
Copy Markdown
Contributor

dnf5 needs-restarting -p could miss a process after an RPM dependency was upgraded. It read the virtual memory size from /proc/<pid>/stat as the start time and kept only one PID per executable.

Read the start time from field 22, relative to kernel boot, and compare every instance of the executable with the latest install time in its package dependency tree. Skip processes whose stat record can no longer be read. The migration guide now describes the -p option.

Verification

  • Built the CLI and plugin and ran the needs-restarting unit suite. All 12 tests passed, including a regression test for the start-time field.
  • Ran needs-restarting -p --json against an isolated RPM database with two instances started before a dependency upgrade and one after it. The Fedora 45 package (5.4.8.0) reported no processes. The patched command reported both older PIDs and excluded the newer PID.

Fixes #2963

Read field 22 of /proc/<pid>/stat and convert its clock ticks using
the kernel boot time. The old parser read field 23 (virtual memory
size), which made comparisons with RPM install times unreliable.

Keep all PIDs for each executable and compare their individual start
times with the latest install time in the package's dependency tree.
Cover the start-time field in a regression test and document the
process option in the DNF 4 migration guide.

Fixes rpm-software-management#2963
@LUX-ETVERITAS
LUX-ETVERITAS requested a review from a team as a code owner December 4, 2026 05:02
@LUX-ETVERITAS
LUX-ETVERITAS requested review from pkratoch and removed request for a team December 4, 2026 05:02
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

needs-restarting: Report each process started before a dependency update

🐞 Bug fix 🧪 Tests 📝 Documentation 🕐 20-40 Minutes


AI Description

• Reads field 22 (starttime) of /proc//stat instead of field 23 (vsize).
• Keeps every PID per executable and compares each with the latest dependency install time.
• Skips processes whose stat record is unreadable; documents the -p option in the migration guide.
Merge Risk 🟢 Low

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed parser, kernel-time conversion, process grouping, dependency comparison, and CLI output form one bounded diagnostic behavior: reporting processes whose executable package or dependencies were updated.
  • The command's run path invokes this behavior directly, and no changed persistent state or downstream functional capability is introduced.
Sources

dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp[515-535]

std::optional<unsigned long long> read_process_start_ticks(std::istream & stat_file) {
    std::string stat;
    // Read the whole record because comm can contain newlines and parentheses.
    if (!std::getline(stat_file, stat, '\0')) {
        return std::nullopt;
    }
    const auto comm_end = stat.rfind(')');
    if (comm_end == std::string::npos) {
        return std::nullopt;
    }
    std::istringstream fields(stat.substr(comm_end + 1));
    std::string field;
    for (int field_number = 3; field_number < 22; ++field_number) {
        fields >> field;
    }
    unsigned long long start_ticks;
    if (!(fields >> start_ticks)) {
        return std::nullopt;
    }
    return start_ticks;
}
Behavioral complexity 🟡 Medium 🧩 Compatibility cases

  • Before, the process scan parsed one line of /proc/<pid>/stat and derived process time from the previous boot-time path.
  • After, it reads the complete record, finds the final closing parenthesis, skips fields through field 21, extracts field 22, and converts kernel ticks using the kernel boot epoch; dependency timestamps are then shared across instances of one executable.
  • The new correctness condition is that unusual comm values, malformed or truncated records, tick-rate failures, and per-instance comparisons all preserve correct recommendations without shifting the field or time interpretation.
  • The unusual-record cases are newly handled rather than extracted unchanged branches.
Sources

dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp[515-535]

std::optional<unsigned long long> read_process_start_ticks(std::istream & stat_file) {
    std::string stat;
    // Read the whole record because comm can contain newlines and parentheses.
    if (!std::getline(stat_file, stat, '\0')) {
        return std::nullopt;
    }
    const auto comm_end = stat.rfind(')');
    if (comm_end == std::string::npos) {
        return std::nullopt;
    }
    std::istringstream fields(stat.substr(comm_end + 1));
    std::string field;
    for (int field_number = 3; field_number < 22; ++field_number) {
        fields >> field;
    }
    unsigned long long start_ticks;
    if (!(fields >> start_ticks)) {
        return std::nullopt;
    }
    return start_ticks;
}
Potential damage 🟢 Low 🩹 Reversible

  • If the changed behavior regresses, the command may list processes that do not need restarting or omit processes that do.
  • The command only reports results and does not mutate package state, restart processes, or write persistent process state.
  • Reverting the code restores subsequent reports; any operator action already taken from an incorrect report would require manual reversal.
  • Code rollback restores the reporting logic for future invocations.
  • No automatic repair is required for command-owned state because the command creates none; operator actions based on a wrong report are outside automatic recovery.
Sources

dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp[515-535]

std::optional<unsigned long long> read_process_start_ticks(std::istream & stat_file) {
    std::string stat;
    // Read the whole record because comm can contain newlines and parentheses.
    if (!std::getline(stat_file, stat, '\0')) {
        return std::nullopt;
    }
    const auto comm_end = stat.rfind(')');
    if (comm_end == std::string::npos) {
        return std::nullopt;
    }
    std::istringstream fields(stat.substr(comm_end + 1));
    std::string field;
    for (int field_number = 3; field_number < 22; ++field_number) {
        fields >> field;
    }
    unsigned long long start_ticks;
    if (!(fields >> start_ticks)) {
        return std::nullopt;
    }
    return start_ticks;
}
Assessed commit 6a87ab4
Diagram

graph TD
A["/proc/pid/stat"] --> B["read_process_start_ticks"] --> C["Process start time"] --> D["Group PIDs by executable"] --> E["Compare with dependency install time"] --> F["needs-restarting output"]
G[("RPM database")] --> E
Loading
High-Level Assessment

The PR's approach is appropriate. It fixes the wrong field index in place and keeps the existing /proc scan and RPM dependency walk. A procfs library (such as libprocps) would add a new dependency for a single field read. Using /proc/pid/stat mtime or the executable's file timestamp would not reflect the actual process start time. Grouping instances per executable and taking the max dependency install time once is equivalent to the previous early-exit loop but avoids repeating the dependency walk per PID.

Files changed (5) +65 / -44

Bug fix (2) +50 / -44
needs_restarting.cppFix start-time parsing and report every stale process instance +46/-44

Fix start-time parsing and report every stale process instance

• Adds read_process_start_ticks, which skips to field 22 after the last ')' so comm values containing parentheses are handled. processes_need_restarting now keeps a vector of instances per executable, computes the latest dependency install time once per executable, and reports every PID started before it. Unreadable stat records are skipped, and the command fails if boot time or clock tick rate cannot be determined.

dnf5-plugins/needs_restarting_plugin/needs_restarting.cpp

needs_restarting.hppDeclare read_process_start_ticks helper +4/-0

Declare read_process_start_ticks helper

• Exposes read_process_start_ticks to the unit tests and adds the <iosfwd> and <optional> includes it needs.

dnf5-plugins/needs_restarting_plugin/needs_restarting.hpp

Tests (2) +14 / -0
NeedsRestartingTest.cppAdd regression test for starttime field index +12/-0

Add regression test for starttime field index

• Adds test_process_start_ticks, which uses a comm containing spaces and parentheses to verify field 22 is read rather than vsize. It also checks that a truncated record returns no value.

test/dnf5-plugins/needs_restarting_plugin/NeedsRestartingTest.cpp

NeedsRestartingTest.hppRegister test_process_start_ticks in the test suite +2/-0

Register test_process_start_ticks in the test suite

• Adds the CPPUNIT_TEST entry and method declaration for the new start-ticks regression test.

test/dnf5-plugins/needs_restarting_plugin/NeedsRestartingTest.hpp

Documentation (1) +1 / -0
changes_from_dnf4.7.rstDocument needs-restarting -p option in migration guide +1/-0

Document needs-restarting -p option in migration guide

• Adds a bullet describing that -p lists processes whose executable package or any dependency was updated after the process started.

doc/changes_from_dnf4.7.rst

@evan-goode
evan-goode self-requested a review December 4, 2026 05:20
Match the elapsed milliseconds reported by curl on connection failure.
The elapsed time varies even for connections to localhost.

Drain the rest of each RPM payload after cpio reads its trailer so
rpm2cpio can finish under pipefail. Keep extraction errors fatal.
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.

needs-restarting -p does not report a running program whose dependencies changed during execution

1 participant