Skip to content

Scheduling: OR in SCHED_RESET_ON_FORK instead of ANDing it#2046

Merged
BenBE merged 1 commit into
htop-dev:mainfrom
winklemad:fix-reset-on-fork-flag
Jul 18, 2026
Merged

Scheduling: OR in SCHED_RESET_ON_FORK instead of ANDing it#2046
BenBE merged 1 commit into
htop-dev:mainfrom
winklemad:fix-reset-on-fork-flag

Conversation

@winklemad

Copy link
Copy Markdown
Contributor

Description

Scheduling_setPolicy() sets the reset-on-fork flag with &= instead of |=:

#ifdef SCHED_RESET_ON_FORK
if (reset_on_fork)
   policy &= SCHED_RESET_ON_FORK;   // should be |=
#endif

SCHED_RESET_ON_FORK is 0x40000000, and the scheduling policies (SCHED_OTHER=0, FIFO=1, RR=2, BATCH=3, IDLE=5) share no bits with it, so policy &= 0x40000000 is always 0 (SCHED_OTHER) — the chosen policy and the flag are both lost.

So when a user enables "Reset on fork" and selects a real-time policy (FIFO/RR/BATCH/IDLE), the subsequent sched_setscheduler(pid, SCHED_OTHER, {sched_priority > 0}) fails with EINVAL (a nonzero priority is invalid for SCHED_OTHER) and the action silently fails; with priority 0 the process is silently downgraded to SCHED_OTHER. Either way, neither the requested policy nor reset-on-fork is applied.

Fix

Use |= so the flag is OR-ed onto the chosen policy — matching the sibling Scheduling_formatPolicy(), which strips it back out with policy & ~SCHED_RESET_ON_FORK, confirming it's meant to be combined.

Verification

The block is Linux-only (#ifdef SCHED_RESET_ON_FORK inside SCHEDULER_SUPPORT), so I confirmed the bitwise logic with a small harness rather than the live UI: for every policy, the current &= yields 0x00000000 (policy lost) while |= yields the correct 0x40000000 | policy (e.g. FIFO → 0x40000001). htop builds cleanly before/after. A repo-wide grep confirms this is the only &= used to set (rather than clear) a flag.

This fix was prepared with AI assistance (noted via the Assisted-by: commit trailer, per the project's policy); I reviewed the change and verified the logic and the reproduction myself.

Scheduling_setPolicy() set the reset-on-fork flag with
`policy &= SCHED_RESET_ON_FORK`. SCHED_RESET_ON_FORK is 0x40000000 and
the scheduling policies (SCHED_OTHER=0, FIFO=1, RR=2, BATCH=3, IDLE=5)
share no bits with it, so `&=` always zeroes the policy to SCHED_OTHER
and drops the flag. Enabling "Reset on fork" while choosing a real-time
policy therefore either fails with EINVAL (the RT priority no longer
matches SCHED_OTHER) or silently downgrades the process to SCHED_OTHER.

Use `|=` so the flag is combined with the chosen policy, matching the
sibling Scheduling_formatPolicy() which strips it with
`policy & ~SCHED_RESET_ON_FORK`.

Assisted-by: Claude (Anthropic)
Signed-off-by: winklemad <winklemad@outlook.com>
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5617a186-fb8e-419e-947d-2059b83825ca

📥 Commits

Reviewing files that changed from the base of the PR and between a97ad96 and 4dc18e7.

📒 Files selected for processing (1)
  • Scheduling.c

📝 Walkthrough

Walkthrough

Scheduling_setPolicy now uses bitwise OR to apply SCHED_RESET_ON_FORK, preserving all existing scheduling policy bits when reset_on_fork is enabled.

Poem

A policy flag joins the stream,
Existing bits remain supreme.
Reset-on-fork now takes its place,
Without erasing the policy’s face.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@BenBE BenBE added the bug 🐛 Something isn't working label Jul 18, 2026
@BenBE BenBE added this to the 3.6.0 milestone Jul 18, 2026
@BenBE BenBE modified the milestones: 3.6.0, 3.5.2 Jul 18, 2026
@BenBE
BenBE merged commit eb9859a into htop-dev:main Jul 18, 2026
37 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐛 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants