Skip to content

amd_s2idle: fix fchown called on closed file descriptor#43

Merged
superm1 merged 1 commit into
masterfrom
copilot/fix-indentation-error
May 28, 2026
Merged

amd_s2idle: fix fchown called on closed file descriptor#43
superm1 merged 1 commit into
masterfrom
copilot/fix-indentation-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 28, 2026

os.fchown was called after the with os.fdopen(fd, ...) block exited, at which point fd is already closed — raising OSError: [Errno 9] Bad file descriptor whenever SUDO_UID/SUDO_GID were set and a report filename was provided.

Changes

  • src/amd_debug/sleep_report.py — Move fchown call inside the with os.fdopen(...) block so fd remains valid:
# Before — fchown executes after fd is closed
with os.fdopen(fd, "w", encoding="utf-8") as f:
    f.write(template.render(context))
if "SUDO_UID" in os.environ:
    os.fchown(fd, ...)  # OSError: bad file descriptor

# After — fchown executes while fd is still open
with os.fdopen(fd, "w", encoding="utf-8") as f:
    f.write(template.render(context))
    if "SUDO_UID" in os.environ:
        os.fchown(fd, ...)
  • src/test_sleep_report.py — Add test_build_template_fchown_called_while_fd_open: sets SUDO_UID/SUDO_GID, calls build_template with a real temp path, and asserts fchown is invoked with the correct uid/gid.

os.fchown(fd, ...) was called after the `with os.fdopen(fd, ...)` block
exited, at which point fd is already closed. This caused OSError (Bad file
descriptor) when SUDO_UID/SUDO_GID were set. Move the fchown call inside
the with block so fd is still valid.

Add test_build_template_fchown_called_while_fd_open to verify fchown is
invoked with the correct uid/gid while the fd is open.
@superm1 superm1 marked this pull request as ready for review May 28, 2026 15:32
@superm1 superm1 merged commit aef4016 into master May 28, 2026
8 checks passed
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.33%. Comparing base (eb5705c) to head (e84d638).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/amd_debug/sleep_report.py 50.00% 0 Missing and 1 partial ⚠️
src/test_sleep_report.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #43      +/-   ##
==========================================
+ Coverage   85.19%   85.33%   +0.14%     
==========================================
  Files          36       36              
  Lines        8119     8136      +17     
  Branches      766      767       +1     
==========================================
+ Hits         6917     6943      +26     
+ Misses        980      971       -9     
  Partials      222      222              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

2 participants