Skip to content

[WIP] Fix EACCESS on install - #593

Closed
bryphe wants to merge 7 commits into
masterfrom
bryphe/bugfix/eaccess-on-install
Closed

bryphe wants to merge 7 commits into
masterfrom
bryphe/bugfix/eaccess-on-install

Conversation

@bryphe

@bryphe bryphe commented Nov 6, 2018

Copy link
Copy Markdown
Collaborator

Issue: We were seeing sporadic, intermittent EACCESS failures when renaming a source directory from the staging path to the install path.

Example error:

Info fetching: done
.... installing @opam/conf-m4@opam:1esy: internal error, uncaught exception:
     Unix.Unix_error(Unix.EACCES, "rename", "C:\\Users\\bryph\\.esy\\source\\s\\esy_ocaml__s__esy_installer-a6f22bd59ae8e31a9aa79eb3b966f1d8")

After some debugging, @andreypopp found that the code that reads the package.json file seemed to be holding a file handle, which was causing the rename operation to fail.

It seems that there is some issues with Lwt_io.with_file and the O_NONBLOCK flag on Windows - this is tracked and described here: ocsigen/lwt#574 .

Proposed workaround: We'll fall back to using the [synchronous] file I/O operations on Windows, for now. This is not ideal for performance, but allows us to unblock Windows in this step.

I believe there is still a way to get Lwt to work, but we'd need to drop to some lower primitives.

@bryphe

bryphe commented Nov 6, 2018

Copy link
Copy Markdown
Collaborator Author

@andreypopp , you have more lwt experience than me, so you might have a better solution in mind!

@bryphe
bryphe force-pushed the bryphe/bugfix/eaccess-on-install branch from cdb11b7 to 9c22591 Compare November 6, 2018 02:35
@andreypopp

Copy link
Copy Markdown
Member

Still seeing EACCESS errors on rename with that diff.

@aantron

aantron commented Nov 6, 2018

Copy link
Copy Markdown
Contributor

If the process doing the renaming and the process doing the reading is the same one, and you are trying to do both operations from the main thread, then the problem can't be that Lwt is blocking that thread, as suggested in ocsigen/lwt#574. There may well be some other problem with Lwt, though.

I'd be happy to help out fixing this, just let me know what to set up/run/etc. I am on a Windows system.

@bryphe

bryphe commented Nov 7, 2018 •

Copy link
Copy Markdown
Collaborator Author

Hey @aantron ! Thanks a lot for your reply and the detailed response at ocsigen/lwt#574.

It looks like @andreypopp put together a better fix for us here:

(* We are getting EACCESS error on Windows if we try to rename directory

We still haven't figured out exactly why we're leaking file descriptors (which blocks the rename operation), but his workaround seems to significantly improve Windows!

When we investigated initially - we found that removing our Fs.readFile operation:
let readFile (path : Path.t) =
let path = Path.show path in
let desc = Printf.sprintf "Unable to read file %s" path in
toRunAsync ~desc (fun () ->
let f ic = Lwt_io.read ic in
Lwt_io.with_file ~mode:Lwt_io.Input path f
)

seemed to reduce the occurrence of the issue. In addition, if we switch to an alternative strategy, like: ```
  let ic = open_in (Path.show filename) in
      let _line = try Some (input_line ic) with End_of_file -> None in
      close_in ic;
      return None

or

  let fd = Unix.openfile (Path.show filename) Unix.[O_RDONLY] 0o777 in
      let ic = Unix.in_channel_of_descr fd in
      let _line = try Some (input_line ic) with End_of_file -> None in
      Unix.close fd;
      return None

The problem seemed to occur less frequently (although still happened - as @andreypopp we saw the failure still in our extended test suite). So it could be that the removal of Lwt_io.with_file was a red herring, or that we are using it in other places that are still causing us to leak file descriptors - inconclusive 😄

I'd be happy to help out fixing this, just let me know what to set up/run/etc. I am on a Windows system.

We're not blocked on this at the moment, so I don't want to take too much of your time - the repro is consistent but involved, and we haven't been able to get a minimal repro as far as I know - this is the repro that is pretty consistent:

Setup: (if you've never built esy before)

  • git clone https://github.com/esy/esy esy-dev && cd esy-dev
  • git checkout 571f1c28d15752ec960550abfd25eaa7b4ec8b80 (before andrey's fix)
  • esy install
  • esy build
  • cd ..

Repro:

  • git clone https://github.com/esy/esy esy-test & cd esy-test
  • ../esy-dev/_build/default/esy/bin/esyCommand.exe install

NOTE: Run git clean -dxf in esy-test between each repro, and delete your C:/users/<name>/.esy/3_/source folder.

~80% of the time that install command will fail with that EACCESS error when copying. We primarily investigated by removing bits from the install path until it worked:

run (fun () ->

But as I mentioned - with @andreypopp 's fix we're pretty good for now. But hopefully you don't mind if we loop you in if we hit future Lwt issues, @aantron - thanks again for helping us here and for all your work on lwt! 💯 If you do want to try the repro and have any questions or issues, feel free to hit me up on Discord too (same username)

@bryphe

bryphe commented Nov 7, 2018

Copy link
Copy Markdown
Collaborator Author

Closing this since @andreypopp has a better fix than my proposed one here.

@bryphe bryphe closed this Nov 7, 2018
@bryphe

bryphe commented Nov 7, 2018

Copy link
Copy Markdown
Collaborator Author

Possible investigation avenues if this comes up again:

@aantron

aantron commented Nov 7, 2018

Copy link
Copy Markdown
Contributor

Thanks for the info. Absolutely do loop me in, please :) If this issue, or any other, is being caused by Lwt, I would very much like to solve it in Lwt, upstream. This is especially true if the issue is somehow rare, as that means opportunities for finding and solving it are limited (due to lack of reports/cases) :)

@andreypopp
andreypopp deleted the bryphe/bugfix/eaccess-on-install branch January 13, 2020 09:22
@rizo

rizo commented Apr 20, 2020

Copy link
Copy Markdown
Contributor

Hi!

Has this issue been resolved? I'm getting a similar issue with 0.6.2:

info esy 0.6.2 (using package.json)
.... fetching @opam/lwt_log@opam:1.1.1esy: internal error, uncaught exception:
     Unix.Unix_error(Unix.EACCES, "rename", "C:\\Users\\...\\_esy\\default\\tmp\\esy-6a3d8b\\archive")
     Raised at file "src/core/lwt.ml", line 2999, characters 20-29
     Called from file "src/unix/lwt_main.ml", line 26, characters 8-18
     Called from file "esy-lib/Cli.re", line 263, characters 9-28
     Called from file "cmdliner_term.ml", line 25, characters 19-24
     Called from file "cmdliner.ml", line 25, characters 27-34
     Called from file "cmdliner.ml", line 116, characters 32-39
.... fetching @opam/react@opam:1.2.1

@andreypopp

Copy link
Copy Markdown
Member

@rizo try 0.6.4 but not sure if it resolves this exact failure

@a-c-sreedhar-reddy

Copy link
Copy Markdown

Hi!

Has this issue been resolved? I'm getting a similar issue with 0.6.2:

info esy 0.6.2 (using package.json)
.... fetching @opam/lwt_log@opam:1.1.1esy: internal error, uncaught exception:
     Unix.Unix_error(Unix.EACCES, "rename", "C:\\Users\\...\\_esy\\default\\tmp\\esy-6a3d8b\\archive")
     Raised at file "src/core/lwt.ml", line 2999, characters 20-29
     Called from file "src/unix/lwt_main.ml", line 26, characters 8-18
     Called from file "esy-lib/Cli.re", line 263, characters 9-28
     Called from file "cmdliner_term.ml", line 25, characters 19-24
     Called from file "cmdliner.ml", line 25, characters 27-34
     Called from file "cmdliner.ml", line 116, characters 32-39
.... fetching @opam/react@opam:1.2.1

Similar issue in 0.6.8 #1097 (comment)

@aantron aantron mentioned this pull request Jan 1, 2022
ManasJayanth added a commit that referenced this pull request Jan 6, 2022
Retry rename on EACCES

This PR retries `rename` upon getting `EACCES`. I've included data about how many retries are likely necessary.

On my system (WSL1 Ubuntu 20.04, omitting hardware details), the `EACCES` issue makes it impossible to use esy to install any of the Dream examples or use Dream's quick start. As the data below shows, every installation is expected to fail with `EACCES`, if it is not worked around.

The underlying `EACCES` issue seems to be a long-standing problem on WSL1 (microsoft/WSL#1529, microsoft/WSL#3395), and I think we do have to work around it in esy. I'm not sure what is causing the `EACCES` exactly. I think there are two main classes of possibilities:

- Self-interaction between esy's opened file descriptors and `rename`. I think the self-interaction is due to WSL rather than Lwt or another library. Since I compiled esy on WSL, it is using Lwt's Unix (rather than Windows) C code. Since the Unix code seems to work fine on Linux and Mac, this suggests a WSL issue.
- Interaction between esy and file indexers or other proceses running on the system. I'm not sure if that's a WSL issue or not, but I've never had to be aware of such processes when doing renames in Cygwin or elsewhere.

I built esy with this patch under WSL and ran clean `esy install`s in Dream's [full-stack ReScript](https://github.com/aantron/dream/tree/03e4d37cb5f5f638707479cd46105e2ee2b1df0e/example/w-fullstack-rescript#readme) example, using this script:

```sh
#!/bin/bash

export PATH="/home/antron/code/attic/esy/_build/install/default/bin:$PATH"
export ESY__PREFIX="/home/antron/code/dream/dream/example/w-fullstack-rescript/esy-prefix"
export OCAMLRUNPARAM=b
RUN=1

while true
do
  rm -rf esy-prefix _esy esy.lock lib node_modules/ package-lock.json
  echo
  echo "RUN $RUN"
  which esy
  esy install # --verbosity debug
  if [ $? != 0 ]
  then
    exit
  fi
  RUN=$((RUN+1))
done
```

The example was checked out into NTFS. The system was freshly restarted, and VSCode (or anything similar) was not running.

I used a version of this patch with a print showing the number of attempts before `rename` succeeds, and got the following results from 5 runs:

```
1 attempt:   802
2 attempts:   52
3 attempts:   12
4 attempts:    3
5 attempts:    1
total:       870
```

Based on this, I naively estimated that if a `rename` needs more than 1 attempt, the number of attempts needed decays by a factor of 4 at each step. I set the limit on the number of attempts naively to 8, thus expecting one failure in about 500 `esy install` attempts of the Dream ReScript example, under all these simplified assumptions.

The delay between attempts is (over) one second, so this means that upon legitimate `EACCES`, users will have to wait eight seconds to get an error message. I think there are two ways to address this:

- Fall back to recursive copy rather than retrying `rename` when `rename` fails. Do we have a recursive copy available in esy or its dependencies? Is it fine to leave the source directory intact?
- Detect WSL and retry only on WSL. Waiting for 8 seconds is still a much better user experience than failure to install at all, so the PR will still be an improvement, without, in this case, harming Linux or Mac users. We could also add a message, shown in case we finally fail with `EACCES` on WSL, giving users a hint about potential VSCode or other watchers, and what else they can try to solve the problem.

Closes #1363.
Probably fixes #1097, some of the reports after the first one.
Probably fixes #1083.
Probably fixes #593, but I haven't looked into non-WSL Windows yet.
Probably fixes camlworks/dream#63.

cc @bryphe, @rizo, @jordwalke, @iMplode-nZ, @a-c-sreedhar-reddy, @srirajshukla, @andreypopp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants