tuna: Print warning if setting affinity results in EPERM and continue
PATCH 1/2 tuna: Print warning if setting affinity results in EPERM and continue
When trying to isolate a CPU, if setting the affinity on a process fails with EPERM (Operation not permitted), this can occur due to security restrictions, namespace isolation, or lack of capabilities.
This is commonly seen with PID 1 (systemd/init) even when running as root, particularly when using 'su -c' which may not grant full capabilities, or when SELinux/AppArmor policies restrict the operation.
tuna should print a warning that this pid could not be moved, and continue processing other processes, similar to how EBUSY errors are handled.
--- tuna/tuna.py | 4 ++++ 1 file changed, 4 insertions(+)
tuna/tuna.py +4 -0
@@ -370,6 +370,10 @@ def isolate_cpus(cpus, nr_cpus): comm = ps[pid].stat["comm"] print(f'Warning: Unable to isolate pid {pid} [{comm}]') continue + if err.args[0] == errno.EPERM: + comm = ps[pid].stat["comm"] + print(f'Warning: Unable to isolate pid {pid} [{comm}]') + continue raise err if "threads" not in ps[pid]:
Signature · 1 line
2.54.0
PATCH 2/2 tuna: Add testing infrastructure using unittest framework
Create a clean testing infrastructure for tuna using Python's unittest framework. This provides a foundation for unit tests and future test expansion.
Structure:
- tests/ directory with unittest-based tests
- tests/run_tests.sh runner script
- Makefile targets for easy test execution
- Comprehensive README.md with examples and documentation
Testing Infrastructure:
- Runner script (run_tests.sh) that uses unittest discovery
* Automatically finds all test_*.py files
* Provides clean output
* Changes to repo root before running
- Makefile targets:
* make tests / make unit-tests - Run all unit tests
* make test-eperm - Run specific EPERM test
- Multiple execution methods supported:
* Via make (recommended for users)
* Via runner script (./tests/run_tests.sh)
* Via unittest directly (python3 -m unittest discover)
Initial Test:
- test_eperm_handling.py with 6 test methods
- Validates EPERM error handling in isolate_cpus()
- Tests verify the fix from commit 625edd878154
- All tests run without root privileges or special configuration
The infrastructure is designed for easy expansion - simply add new test_*.py files and they're automatically discovered and run.
--- Makefile | 9 +++ tests/README.md | 147 +++++++++++++++++++++++++++++++++++ tests/run_tests.sh | 25 ++++++ tests/test_eperm_handling.py | 87 +++++++++++++++++++++ 4 files changed, 268 insertions(+) create mode 100644 tests/README.md create mode 100755 tests/run_tests.sh create mode 100644 tests/test_eperm_handling.py
Makefile +9 -0
@@ -20,3 +20,12 @@ cleanlogs: .PHONY: clean clean: pyclean + +.PHONY: tests unit-tests +tests: unit-tests + +unit-tests: + @./tests/run_tests.sh + +test-eperm: + @python3 -m unittest tests.test_eperm_handling -v
tests/README.md new file +147 -0
147 lines not on this page · raw message
tests/run_tests.sh new file +25 -0
@@ -0,0 +1,25 @@ +#!/bin/bash +# Copyright (C) 2026 John Kacur +# SPDX-License-Identifier: GPL-2.0-only +# +# run_tests.sh - Run tuna unit tests +# +# This script runs all unit tests in the tests/ directory using Python's +# unittest framework. + +set -e + +# Change to repository root +cd "$(dirname "$0")/.." + +echo "Running tuna unit tests..." +echo "" + +# Run unittest discovery +# -s tests: start directory +# -p "test_*.py": pattern for test files +# -v: verbose output +python3 -m unittest discover -s tests -p "test_*.py" -v + +echo "" +echo "All tests passed!"
tests/test_eperm_handling.py new file +87 -0
87 lines not on this page · raw message
Signature · 1 line
2.54.0