Skip to content

fix: close file descriptor after writing temp settings file#2272

Open
wishhyt wants to merge 1 commit intoqodo-ai:mainfrom
wishhyt:fix/fd-leak-repo-settings
Open

fix: close file descriptor after writing temp settings file#2272
wishhyt wants to merge 1 commit intoqodo-ai:mainfrom
wishhyt:fix/fd-leak-repo-settings

Conversation

@wishhyt
Copy link
Copy Markdown

@wishhyt wishhyt commented Mar 18, 2026

Summary

  • tempfile.mkstemp() returns an open file descriptor that was never closed after os.write()
  • The finally block removes the temp file with os.remove() but does not close the fd
  • In a long-running server process, each call to apply_repo_settings() leaks one file descriptor, eventually exhausting the system fd limit

Changes

pr_agent/git_providers/utils.py: Added os.close(fd) after os.write(fd, repo_settings)

Test plan

  • Verify that repo settings are still applied correctly after the change
  • Verify no file descriptor leak by monitoring fd count across multiple settings applications

`tempfile.mkstemp()` returns an open file descriptor that was never
closed after `os.write()`. In a long-running server process, each
call to `apply_repo_settings()` leaks one fd, eventually exhausting
the system file descriptor limit. The `finally` block removes the
file but does not close the descriptor.

Made-with: Cursor
@qodo-free-for-open-source-projects
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Close file descriptor after writing temp settings file

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Close file descriptor after writing temp settings file
• Prevents file descriptor leak in long-running server processes
• Fixes exhaustion of system fd limit on repeated settings applications
Diagram
flowchart LR
  A["tempfile.mkstemp returns open fd"] --> B["os.write writes to fd"]
  B --> C["os.close closes fd"]
  C --> D["finally block removes temp file"]
  D --> E["No fd leak"]
Loading

Grey Divider

File Changes

1. pr_agent/git_providers/utils.py 🐞 Bug fix +1/-0

Close file descriptor after write operation

• Added os.close(fd) call after os.write(fd, repo_settings) to properly close the file
 descriptor
• Prevents file descriptor leak in apply_repo_settings() function
• Ensures long-running server processes don't exhaust system file descriptor limits

pr_agent/git_providers/utils.py


Grey Divider

Qodo Logo

@qodo-free-for-open-source-projects
Copy link
Copy Markdown
Contributor

qodo-free-for-open-source-projects bot commented Mar 18, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider


Action required

1. fd not closed on error 📘 Rule violation ⛯ Reliability
Description
The new os.close(fd) is not protected by a finally, so an exception from `os.write(fd,
repo_settings)` can still leak the file descriptor. This does not fully meet the robust
error-handling requirement for cleanup on failure paths.
Code

pr_agent/git_providers/utils.py[R37-39]

                    fd, repo_settings_file = tempfile.mkstemp(suffix='.toml')
                    os.write(fd, repo_settings)
+                    os.close(fd)
Evidence
PR Compliance ID 3 requires anticipating and handling error scenarios; here, os.close(fd) occurs
only after os.write(...) succeeds, so the error path is not handled for descriptor cleanup.

Rule 3: Robust Error Handling
pr_agent/git_providers/utils.py[37-39]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`os.close(fd)` is executed only if `os.write(fd, repo_settings)` succeeds; if `os.write` raises, the file descriptor can still leak.

## Issue Context
This code uses `tempfile.mkstemp()` which returns a raw OS file descriptor that must be closed on all paths (success and exception).

## Fix Focus Areas
- pr_agent/git_providers/utils.py[37-39]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Comment on lines 37 to +39
fd, repo_settings_file = tempfile.mkstemp(suffix='.toml')
os.write(fd, repo_settings)
os.close(fd)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Action required

1. fd not closed on error 📘 Rule violation ⛯ Reliability

The new os.close(fd) is not protected by a finally, so an exception from `os.write(fd,
repo_settings)` can still leak the file descriptor. This does not fully meet the robust
error-handling requirement for cleanup on failure paths.
Agent Prompt
## Issue description
`os.close(fd)` is executed only if `os.write(fd, repo_settings)` succeeds; if `os.write` raises, the file descriptor can still leak.

## Issue Context
This code uses `tempfile.mkstemp()` which returns a raw OS file descriptor that must be closed on all paths (success and exception).

## Fix Focus Areas
- pr_agent/git_providers/utils.py[37-39]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

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.

1 participant