Python3 TemporaryFile().truncate() fails in WSL2 (breaking pytest) #6268
Replies: 7 comments 1 reply
-
That error is from userspace; you are on a VM in WSL2. A gist to a threaded |
Beta Was this translation helpful? Give feedback.
-
@therealkenc What version of python are you using? I was using 3.6. Maybe I need to upgrade. Either way, it looks like my issue not WSL's. Feel free to close. Sorry to trouble. |
Beta Was this translation helpful? Give feedback.
-
No trouble. I'll move over to discussion. Python version is probably not it. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the gist.
I don't really 'do' Python, but you've got a local installation looking for |
Beta Was this translation helpful? Give feedback.
-
Sorry to revive this old thread, but I'm facing the same issue running It seems like a WSL2 issue or an issue with Python interacting with WSL2 to me. I tried running the same command in a regular Windows 10 environment (although an unclean one) and there were no issues. |
Beta Was this translation helpful? Give feedback.
-
Tested with
Looking at your gist this might be of interest:
Try running the command inside a python session. You'll get more feedback if you enable the python debugger (import pdb) also. |
Beta Was this translation helpful? Give feedback.
-
Hi! I have digged deep into this problem and found a workaround solution. Maybe someone with higher expertise in core python modules will change it one day. The short story is that in WSL (The kernel that the linux images run on) files work differently, and files cannot be unlinked (deleted) while they are open by the process, but right now the tempfile.py thinks we are in pure LINUX os and platform, while our files in docker should work WINDOWS like because we are in WSL. This is the bit that created the use case problem inside tempfile.py if _os.name != 'posix' or _sys.platform == 'cygwin'
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
# while it is open.
TemporaryFile = NamedTemporaryFile
else:
..... The workaround I have found is to include this: def is_wsl():
try:
with open("/proc/version", "r") as f:
version_info = f.read().lower()
return "microsoft" in version_info
except FileNotFoundError:
return False
if _os.name != 'posix' or _sys.platform == 'cygwin' or is_wsl():
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
# while it is open.
TemporaryFile = NamedTemporaryFile
else:
.... And running a little script it for sure works:
Now the tough part is to get this running smoothly.... I would either create a tempfile.py with this logic and replace it, but if there is any new version on tempfile.py you will miss it... If you just want a quick solution to your problem add this code to your Dockerfile (pick the part you need and make sure you are creating the tempfile in your python.X.X)
Hope this helps anyone, or it helps someone mantaining tempfile.py to create a solution for WSL |
Beta Was this translation helpful? Give feedback.
-
Environment
Steps to reproduce
Creating a temp file in python and calling truncate on it doesn't work in WSL2. This came up when running a set of
pytests
that work in a linux vm but don't work on WSL2.$ python3 -c "import tempfile; tempfile.TemporaryFile().truncate()"
I can provide an strace if needed but I'm hoping it will be easy enough to reproduce.
Expected behavior
In a ubuntu 18.04 vm, the above command succeeds without error.
Actual behavior
On WSL2 I get the following error.
Beta Was this translation helpful? Give feedback.
All reactions