Make temporary_cd() thread-safe by not calling os.chdir() in the no-argument form#147
Draft
Make temporary_cd() thread-safe by not calling os.chdir() in the no-argument form#147
temporary_cd() thread-safe by not calling os.chdir() in the no-argument form#147Conversation
…ument form Agent-Logs-Url: https://github.com/openforcefield/openff-utilities/sessions/0701659a-296a-4946-aa02-05a346cf1b14 Co-authored-by: mattwthompson <7935382+mattwthompson@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix thread safety in temporary_cd for file I/O
Make Apr 17, 2026
temporary_cd() thread-safe by not calling os.chdir() in the no-argument form
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #147 +/- ##
=======================================
Coverage 94.93% 94.93%
=======================================
Files 6 6
Lines 158 158
=======================================
Hits 150 150
Misses 8 8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
os.chdir()is process-wide: concurrent threads callingtemporary_cd()simultaneously all mutate the same CWD, causing races where threads write/read files in each other's temp directories.Changes
temporary_cd()(no-arg): no longer callsos.chdir(). Creates a temp dir, yields its absolute path, and cleans up — the process CWD is never touched. Thread-safe.temporary_cd(path)(explicit path): retainsos.chdir()for backward compatibility. Still not thread-safe (unchanged behaviour, now documented).Generator[None, None, None]→Generator[str, None, None]— the yielded absolute path is the intended interface for file I/O.os.chdir()to avoid resolving a relative path against the new directory.Migration
New tests cover: correct yielded paths in all three call forms, and concurrent correctness with 8 threads.