Skip to content

WSL pins opened directories #1529

Closed
Closed

Description

@therealkenc

Pulling this out of #1492. It is possibly a dup of #1420; but I'm thinking maybe not because that issue has no lingering fds. This hits in spades with nfs-ganesha, because the protocol is stateless and file descriptors are cached for a while before being released. Windows Explorer traipses all over thousands of files, causing lots of directory fds to remain open. This in turn causes unexpected mv and rm operations on seemingly random directories on the WSL side to fail.

This will probably also show itself when people start doing web development scenarios, because http servers have a tendency to do the same sort of fd caching.

// silly-rename.c
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, const char* argv[]) {
  const char msg[] = "amy's parent morton renamed to maura\n";
  int fd, ret;

  mkdir("/tmp/morton", 0777);
  fd = open("/tmp/morton/amy", O_CREAT|O_RDWR, 0666);
  ret = rename("/tmp/morton/", "/tmp/maura/");
  if (ret < 0) {
    perror("rename failed: ");
  } else {
    write(fd, msg, sizeof(msg) - 1);
  }
  close(fd);
}

strace on WSL:

mkdir("/tmp/morton", 0777)              = 0
open("/tmp/morton/amy", O_RDWR|O_CREAT, 0666) = 3
rename("/tmp/morton/", "/tmp/maura/")   = -1 EACCES (Permission denied)

strace on Ubuntu:

mkdir("/tmp/morton", 0777)              = 0
open("/tmp/morton/amy", O_RDWR|O_CREAT, 0666) = 3
rename("/tmp/morton/", "/tmp/maura/")   = 0

Activity

  1. sunilmut commented on Jul 7, 2017

    @sunilmut
    Member

    therealkenc - Apologize for the delay here. I am sure there are other neglected issues, and I personally would like to get through them too.

    Anyways, I think this stems from the NTFS limitation that prevents renaming directories which have a handle open to anything below. Sven Groot (@SvenGroot) to confirm.

  2. seffyroff commented on Nov 17, 2018

    @seffyroff

    Other than 'try to not open lots of files' is there any advice on avoiding this issue, given that a fix seems to be escaping us? Is there a way to monitor and close file handles? Is there a more suitable filesystem for hosting WSL filesystems that would avoid hitting this? Is there a node setting we can flag to ignore these errors, given that they're apparently false positives?

  3. bitcrazed commented on Dec 19, 2018

    @bitcrazed
    Contributor

    Assigned to Sven Groot (@SvenGroot) and Tara Raj (@tara-raj) to take a look, though with Xmas fast approaching, may be a couple of weeks until this gets looked into.

    Interested party here: https://twitter.com/rainabba/status/1075192299791908864

  4. therealkenc commented on Dec 19, 2018

    @therealkenc
    Author

    There has been chirping crickets since 2016, is is fairly safe to assume, mostly because the problem is understood and there isn't much for Sven or Ben or Brian to add. The problem is caused by underlying limitations in NTFS and the NT APIs you have to work with. I posted an outline of a possible (albeit nontrivial) solution elsewhere, of which they've almost certainly been aware since 2016 as well. There isn't anything new to "look into" here.

  5. therealkenc commented on Dec 20, 2018

    @therealkenc
    Author

    Tell you what you can do, if you are looking for something constructive. Right now it is impossible to differentiate between EACCES caused by WSL failing to respect Unix filesystem semantics, versus folks who simply have their permissions wrong (EUSERCONFUSED). You know up in that rename(2) the difference, because you know whether the mode bits (what we tend to call metadata) are okay or not. In the case you can't rename because NTFS won't play ball, return a nonstandard EBUSY instead of EACCES, which will at least stick out like a sore thumb. That way reported issues triggering the known problem can be summarily duped here instead of guessing. This was asked for about a year ago but could have been easily buried versus rejected. Just a thought.

  6. liudonghua123 commented on Jan 21, 2019

    @liudonghua123

    I have the similar problem. I wanted to install rocket.chat according to the instructions on https://rocket.chat/docs/installation/manual-installation/ubuntu/ .

    $ sudo mv /tmp/bundle /opt/Rocket.Chat
    mv: cannot move '/tmp/bundle' to '/opt/Rocket.Chat': Permission denied
    $ ll -d /tmp/bundle/
    drwxr-xr-x 1 liudonghua liudonghua 4096 Jan 17 19:03 /tmp/bundle//
    $ sudo su
    # mv bundle /opt/Rocket.Chat
    mv: cannot move 'bundle' to '/opt/Rocket.Chat': Permission denied
    #
    
  7. eriksneff commented on Jan 22, 2019

    @eriksneff

    seffyroff I have a recommendation. In my case, what I was trying to do was npm install chalk and other packages, and it was failing with the EACCES error. I was running that command from within a folder inside my WSL ubuntu 18 install's home dir from WSL ubuntu's bash command prompt.

    I realized that when I closed all programs that were looking at that folder, the error went away. Specifically, I realized that the windows application MobaXterm was the cause for my specific issues, because when i closed it, npm install chalk would run without issue. HTH!!

  8. DemiMarie commented on Feb 8, 2019

    @DemiMarie

    This is another motivating reason for WSL to ditch NTFS and the NT filesystem APIs, in favor of Unix-native approaches. Probably ext4, since it has FreeBSD drivers.

  9. therealkenc commented on Apr 9, 2019

    @therealkenc
    Author

    With ref to #14 (message)

    Interesting approach. This approach will work around some EACESS errors folks are experiencing, specifically where the open handle triggering #1529 is transient and goes away in one second or less. For other scenarios it won't work, like an editor holding a handle open, or handles being held in some cache or another for more than one second or indefinitely. It won't address the OP test case, for example. Like the effort though. Working around some scenarios (especially your own) is better than it not working at all.

    Hotpatching [g]libc to intercept all syscalls is a pretty big hammer though, and might seem a little excessive to some. [Not me, mind. Anything worth doing is worth overdoing.]

    Another approach would be to hook higher in the food chain. Perhaps in libuv here.

    $  objdump -T /usr/lib/x86_64-linux-gnu/libuv.so.1 | grep uv_fs_rename
    

    Anyway, nice to see people seeking plausible mitigations.

  10. fuzzyTew commented on Apr 9, 2019

    @fuzzyTew

    In response to therealkenc's comments, I spent some time expanding on this hotpatch, now at https://github.com/fuzzyTew/wslunpindir . When the error happens, it enumerates open filehandles from /proc, spits some messaging out on /dev/tty, and waits until they are closed. This does not find open files from Windows, only from within Linux. Assumes the app is being run interactively.

    I considered switching the hook to libuv to make things faster and lightweight, but figured time was better spent making it work in more scenarios rather than fewer.

  11. therealkenc commented on Apr 10, 2019

    @therealkenc
    Author

    I spent some time expanding on this hotpatch

    Noice 💯. If nothing else, it serves as a very useful troubleshooting tool.

    figured time was better spent making it work in more scenarios rather than fewer.

    Expand your intercept-all-the-things approach enough and you can fix the problem even for the case where the handle remains open 😉.

  12. jacob-greenfield commented on Jun 15, 2019

    @jacob-greenfield

    Is the WSL team planning to fix this? I ran into this bug today simply trying to build a npm project. wslunpindir worked like a charm (now I use export LD_PRELOAD=~/git/wslunpindir/interceptrename.so) but this is clearly only a workaround and does not solve the problem for all situations.

  13. onomatopellan commented on Jun 15, 2019

    @onomatopellan

    Jacob Greenfield (@Coder-256) it's fixed in WSL2.

  14. therealkenc commented on Jun 25, 2019

    @therealkenc
    Author

    Jacob Greenfield (@Coder-256) it's fixed in WSL2.

    For a good swath of folk's major pain points, yes. The underlying issue remains however. With minor string edits to OP:

    // silly-rename.c
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main(int argc, const char* argv[]) {
      const char msg[] = "amy's parent morton renamed to maura\n";
      int fd, ret;
    
      mkdir("/mnt/c/Users/there/morton", 0777);
      fd = open("/mnt/c/Users/there/morton/amy", O_CREAT|O_RDWR, 0666);
      ret = rename("/mnt/c/Users/there/morton/", "/mnt/c/Users/there/maura/");
      if (ret < 0) {
        perror("rename failed: ");
      } else {
        write(fd, msg, sizeof(msg) - 1);
      }
      close(fd);
    }
    

    and the strace for pedantry:

    mkdir("/mnt/c/Users/there/morton", 0777) = -1 EEXIST (File exists)
    openat(AT_FDCWD, "/mnt/c/Users/there/morton/amy", O_RDWR|O_CREAT, 0666) = 3
    rename("/mnt/c/Users/there/morton/", "/mnt/c/Users/there/maura/") = -1 EACCES (Permission denied)
    [...]
    write(4, "rename failed: : Permission deni"..., 35) = 35
    

    Which would seem academic, except that it affects everyone using WSL for WSL's raison d'être, which is interop with Windows.

  15. snevs commented on Aug 28, 2019

    @snevs

    why is [Install Remote WSL fail with mv - permission denied #109](https://github.com/microsoft/vscode-remote-release/issues/109) even closed? I have the same issue with mv

  16. slikts commented on Dec 27, 2019

    @slikts

    The problem is caused by underlying limitations in NTFS and the NT APIs you have to work with.

    So it's an another facet of the same root problem as #873 and why WSL2 is being added.

    … it's fixed in WSL2.

    It's not as much fixed in WSL2 as WSL2 is a fundamentally different tool with its own set of significant limitations (deriving from virtualization). Specifically, Hyper-V is not compatible with at least a couple of tools I use, and it also adds significant overhead by running Windows as a guest.

  17. DemiMarie commented on Dec 27, 2019

    @DemiMarie
  18. amanhigh commented on Mar 15, 2020

    @amanhigh

    Facing Similar Issue

    go get -u github.com/PuerkitoBio/goquery
    go: extracting github.com/PuerkitoBio/goquery v1.5.1
    go get: rename /home/aman/go/pkg/mod/github.com/!puerkito!bio/[email protected] /home/aman/go/pkg/mod/github.com/!puerkito!bio/[email protected]: permission denied
    

    Permissions

    ~/go:  ls
    total 0
    drwxr-xr-x 1 aman aman 512 Mar 15 02:43 ..
    drwxrwxrwx 1 aman aman 512 Mar 15 02:46 .
    drwxrwxrwx 1 aman aman 512 Mar 15 02:46 src
    drwxrwxrwx 1 aman aman 512 Mar 15 09:53 pkg
    
  19. clshortfuse commented on Mar 23, 2020

    @clshortfuse

    I have a hypothesis that may or may not help illuminate the issue.

    I have a machine that uses WSL v1 (no issues on v2). I can replicate EACCES pretty easily by trying to install a package with VSCode. I believe there's some sort of access limit or open handles.

    Basically, I know VSCode is monitoring the folder I'm working in. If I install a package with VSCode is open, I'll get EACCES errors. If I close VSCode and install the packages over the Ubuntu bash, no errors.

    So, to create this issue, I believe you just have to:

    • Initiate some sort of file system watcher on a folder (VSCode)
    • Create and delete files within the same folder very quickly (npm install)

    I think what's causing the EACCES issue is that VSCode isn't handling the file system changes faster than npm can create or delete them. That's why this only happens sometimes and on some machines. The machine I'm working on currently is REALLY slow in general and I get this errors consistently if I try it installing a package with VSCode open. WSLv2 may have fixed this issue, or it's just so much faster than WSLv1 that the it's rare for the collision to occur. The fact that the issue goes away when I close VSCode means that the handle is being kept open by the file system watcher.

    I'm sure somebody smarter than I can create some sort of test script that creates and deletes a bunch of files with a watcher in the background. You might find the collision. I just don't know where in WSL this issue might occur. Perhaps inotify is bugged somewhere?

    Edit: Worth noting, I don't use /mnt/* so it probably has little to do with the Windows drives.

  20. added a commit that references this issue on Mar 26, 2020
  21. added a commit that references this issue on Jan 6, 2022
  22. microsoft-github-policy-service commented on Feb 1, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has been automatically closed since it has not had any activity for the past year. If you're still experiencing this issue please re-file this as a new issue or feature request.

    Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions