You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that creating multiple virtual environments in parallel via virtualenv.cli_run() is not thread-safe. When running the following code snippet (see details), you can experience a FileNotFoundError for a json file at pypa/virtualenv/py_info/1 if the underlying interpreter has been updated so that the json file is removed and written again.
Environment
Provide at least:
OS: Windows 10
pip list of the host python where virtualenv is installed:
Since each filelock instance defines a reentrant lock, two different instances (whether they are in the same process/thread or not) will block during locking when the other instance already holds the lock. But locking the same instance twice while not block.
As you may have already guessed, the same instance is used for each thread in virtualenv due to the _lock_store.
Issue
I noticed that creating multiple virtual environments in parallel via
virtualenv.cli_run()
is not thread-safe. When running the following code snippet (see details), you can experience aFileNotFoundError
for ajson
file atpypa/virtualenv/py_info/1
if the underlying interpreter has been updated so that the json file is removed and written again.Environment
Provide at least:
OS: Windows 10
pip list
of the host python wherevirtualenv
is installed:packages
Details
test script
The failure is at
virtualenv/src/virtualenv/app_data/via_disk_folder.py
Line 143 in 0398e5b
virtualenv/src/virtualenv/discovery/cached_py_info.py
Line 73 in 0398e5b
It made me wonder that two threads can be in this section because it is protected by a lock at
virtualenv/src/virtualenv/discovery/cached_py_info.py
Line 62 in 0398e5b
After some digging, I found an interesting statement considering
filelock
in tox-dev/filelock#22 (comment):As you may have already guessed, the same instance is used for each thread in virtualenv due to the
_lock_store
.I also verified the cited statement:
One `FileLock` per thread (thread-safe):
Output (one thread per time in critical section):
One `FileLock` for all threads (not thread-safe):
Output (both threads at the same time in critical section):
The text was updated successfully, but these errors were encountered: