Skip to content

fix(main): use tempfile.mkstemp for collision-safe writable directory check#1349

Merged
amilcarlucas merged 2 commits intorefactor_mainfrom
copilot/sub-pr-1347
Mar 4, 2026
Merged

fix(main): use tempfile.mkstemp for collision-safe writable directory check#1349
amilcarlucas merged 2 commits intorefactor_mainfrom
copilot/sub-pr-1347

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 4, 2026

_is_directory_writable used a fixed .write_test.tmp filename, causing potential collisions and flaky results when multiple processes/threads run the check concurrently in the same directory. Cleanup also only happened on the happy path.

Changes

  • Unique temp file: replaced hardcoded path / ".write_test.tmp" with tempfile.mkstemp(dir=path), producing a distinct file per call
  • Guaranteed cleanup: moved unlink + close into a finally block so the temp file is always removed, even if the write raises mid-way
# Before
test_file = path / ".write_test.tmp"
with open(test_file, "w", encoding="utf-8") as tmp:
    tmp.write("test")
test_file.unlink(missing_ok=True)

# After
fd, tmp_path = tempfile.mkstemp(dir=path)
try:
    os.write(fd, b"test")
    return True
finally:
    os.close(fd)
    Path(tmp_path).unlink(missing_ok=True)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…leanup in _is_directory_writable

Co-authored-by: amilcarlucas <24453563+amilcarlucas@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on code refactor for testability and readability improvements fix(main): use tempfile.mkstemp for collision-safe writable directory check Mar 4, 2026
@amilcarlucas amilcarlucas marked this pull request as ready for review March 4, 2026 00:53
@amilcarlucas amilcarlucas self-requested a review as a code owner March 4, 2026 00:53
@amilcarlucas amilcarlucas merged commit d6fedc7 into refactor_main Mar 4, 2026
21 of 23 checks passed
@amilcarlucas amilcarlucas deleted the copilot/sub-pr-1347 branch March 4, 2026 00:54
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 4, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
11564 10528 91% 89% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
ardupilot_methodic_configurator/main.py 75% 🟢
TOTAL 75% 🟢

updated for commit: f5b8ec7 by action🐍

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