Skip to content

Fix debug logging for CPU affinity bitmask #3191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2025

Conversation

saku3
Copy link
Contributor

@saku3 saku3 commented Jul 5, 2025

Description

This is an improvement addressing a potential bug in the cpu_affinity feature.

  • Only logs when the DEBUG level is enabled. (It follows the behavior of runc.)
  • The expression (1 << i) may interpret 1 as an i32, which could lead to a bitwise overflow.
    You can see that the original code is incorrect by running the following code:
fn main() {
    let mask = (0..usize::BITS as usize)
        .fold(0, |mask, i| mask | (1 << i));
    println!("{}", mask);   
}

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test updates
  • CI/CD related changes
  • Other (please describe):

Testing

  • Added new unit tests
  • Added new integration tests
  • Ran existing test suite
  • Tested manually (please provide steps)

Related Issues

Fixes #

Additional Context

Signed-off-by: Yusuke Sakurai <[email protected]>
@saku3 saku3 changed the title FIx debug logging for CPU affinity bitmask Fix debug logging for CPU affinity bitmask Jul 5, 2025
@utam0k utam0k added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Jul 6, 2025
@utam0k utam0k requested a review from Copilot July 6, 2025 07:07
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves the log_cpu_affinity function by only emitting logs when the DEBUG level is enabled and by fixing potential overflow in the CPU bitmask computation.

  • Added a guard (enabled!(Level::DEBUG)) to skip logging unless DEBUG is active.
  • Changed the fold initial value and shift operands to usize to avoid bitwise overflow.
  • Imported the enabled macro and Level from tracing.
Comments suppressed due to low confidence (2)

crates/libcontainer/src/process/cpu_affinity.rs:84

  • Add a unit test for log_cpu_affinity to verify that it logs the correct mask when DEBUG level is enabled and remains silent otherwise.
    if !enabled!(Level::DEBUG) {

crates/libcontainer/src/process/cpu_affinity.rs:89

  • Consider handling errors from cpuset.is_set(i) instead of using unwrap_or(false), which silently ignores potential failures. You could propagate the error with ? or log it for better visibility.
        .filter(|&i| cpuset.is_set(i).unwrap_or(false))

@utam0k utam0k merged commit 90a96f8 into youki-dev:main Jul 6, 2025
30 of 31 checks passed
@github-actions github-actions bot mentioned this pull request Jul 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants