-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Figure out a way to use tempfile.NamedTemporaryFile on Windows #2743
Comments
I always use def onerror(func, path, exc_info):
"""Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
if not os.access(path, os.W_OK):
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise from here. Could you try this and see if it works for your tests? |
I tried that in the past, see 3038c21
This didn't work. The problem doesn't seem to be specific to |
@fmassa According to my experience, the message "permission failed" here doesn't actually refer to a problem related to access/rights. Instead, it is complaining about sth else like you couldn't delete a file/directory if it is still in use, or you couldn't open a file twice with different acess rights, which makes tempfile not so useful under Windows. |
@peterjc123 it looks like the issue is in our code, either the Do you have any idea on what this might be? |
Related: #1662 |
If you Since Edit: In fact it seems more likely that the MapViewOfFile path is being taken, which will be released on destruction with UnmapViewOfFile Windows docs say: Although an application may close the file handle used to create a file mapping object, the system holds the corresponding file open until the last view of the file is unmapped. |
Thanks a lot @jamt9000 , your comment was spot on! Deleting the tensor fixes the issue on Windows.
|
The root cause of the issue that sparkled this issue was fixed via #2743 (comment) , so this is less of a priority for now. Using |
It seems that I have looked into this before and made the PR here for PyTorch but it gets rejected. Actually, it is okay to delete the file while it's open if all file read/write requests to the file are opened with |
@peterjc123 I might be missing something, but given that A quick test in #2763 seems to indicate that numpy handles this case correctly. |
@fmassa The problem is that |
I guess a workaround would be for the torchvision |
@jamt9000 yes, I considered that as a workaround as well. It would be a bit slower and double the amount of memory used, but might be better. Would you be willing to send a PR with this workaround? |
Another workground is mentioned in pytorch/pytorch#45962, that is, to create a context manager to delete the tensors automatically. |
@peterjc123 I would prefer to avoid the extra context manager, as it might have some weird side effects (does it delete all tensors created within the context managers, or only those created via I wonder what would be the drawbacks of not using file mapping, and instead reading the content of the file in memory, if |
File mapping is great for random IO. Also, it is more efficient because all the data are mapped to a virtual address so it is benefiting from the system caching. But if you just read a file from head to tail, I don't see too many differences it would make. |
@peterjc123 Hum, ok, so maybe the system caching could be beneficial. My first thought was that given that we don't expose anything in PyTorch similar to |
Using
tempfile.NamedTemporaryFile
orTemporaryDirectory
on Windows is very brittle, and a Google search returns multiple users facing similar issues.We obtain errors on Windows such as
and from there it was a rabbithole.
The latest example of it was in #2728 (comment), where in the end I gave up on using
tempfile
and instead went for saving the temporary files on an existing directory.This is a recurring issue in torchvision (many tests are disabled on Windows because of this), and on PyTorch as well, so it would be good to have a canonical solution for this problem.
cc @peterjc123 @nbcsm @guyang3532 @maxluk @gunandrose4u @smartcat2010 @mszhanyi
The text was updated successfully, but these errors were encountered: