Skip to content
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

[BUG] npm ci fails with EPERM/unlink error on windows #568

Closed
momesana opened this issue Dec 6, 2019 · 11 comments
Closed

[BUG] npm ci fails with EPERM/unlink error on windows #568

momesana opened this issue Dec 6, 2019 · 11 comments
Labels
Bug thing that needs fixing platform:windows is Windows-specific Release 6.x work is associated with a specific npm 6 release

Comments

@momesana
Copy link

momesana commented Dec 6, 2019

We have a moderately large web-application and installing the dependencies sometimes leads to EPERM error messages that npm can't recover from. This happens on multiple systems but can't reliably be reproduced. If the problem occurs it seems to persist until the cache is cleared via npm cache clean --force and the next run may or may not yield the desired result (that is: a successful run of npm install). The problem only happens on Windows, neither Linux nor Mac-OS are affected and it only appears to happen with large dependencies (in our case antd, monaco-editor and react-icons).

I couldn't reproduce the problem when fetching the files from npmjs directly. It is noteworthy that we use a private artifactory repository (to shield us from issues like left-pad suddenly being pulled from npmjs). There is a proxy that hides artifactory and it seems like the connection to the proxy may be a bit flaky at times (that's merely an educated guess though) so it might be the case that the connection/download is reset while the fetch is ongoing. Let me show the error message that npm spits out when things go south:

npm ERR! code EPERM
npm ERR! syscall unlink
npm ERR! path C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules.staging\antd-c7c0bb0f\dist\antd.min.js
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, unlink 'C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules.staging\antd-c7c0bb0f\dist\antd.min.js'
npm ERR!  { [Error: EPERM: operation not permitted, unlink 'C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules.staging\antd-c7c0bb0f\dist\antd.min.js']
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, unlink 'C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules.staging\antd-c7c0bb0f\dist\antd.min.js'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'unlink',
npm ERR!      path:
npm ERR!       'C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules\.staging\antd-c7c0bb0f\dist\antd.min.js' },
npm ERR!   stack:
npm ERR!    'Error: EPERM: operation not permitted, unlink \'C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules\.staging\antd-c7c0bb0f\dist\antd.min.js\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'unlink',
npm ERR!   path:
npm ERR!    'C:\Users\xy\master\projects\tools\dashboard\react-frontend\node_modules\.staging\antd-c7c0bb0f\dist\antd.min.js',
npm ERR!   parent: 'react-frontend' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\anon\AppData\Roaming\npm-cache_logs\2019-12-03T14_03_56_555Z-debug.log

or on another colleagues system

npm WARN tarball tarball data for antd@3.25.3 (sha1-KEy/g89atsJt/7YGZXbyAupgr1A=) seems to be corrupted. Trying one more time.
npm WARN tarball tarball data for monaco-editor@0.18.1 (sha1-ztfDBaIxCYdf7q85WlBLkfY1jPw=) seems to be corrupted. Trying one more time.
npm ERR! code EPERM
npm ERR! syscall unlink
npm ERR! path D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\antd\dist\antd-with-locales.min.js.map
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, unlink 'D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\antd\dist\antd-with-locales.min.js.map'
npm ERR!  { [Error: EPERM: operation not permitted, unlink 'D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\antd\dist\antd-with-locales.min.js.map']

or during another run

"C:/.conan/d98eae/1/npm" --scripts-prepend-node-path=true install
npm WARN deprecated [...]
npm WARN tarball tarball data for antd@^3.25.3 (sha1-CmpD/dhXfmFwiTK3vDS6AjTQ8Tg=) seems to be corrupted. Trying one more time.
npm WARN tar ENOENT: no such file or directory, open 'D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\.staging\core-js-94a456ad\web\timers.js'
[...]
npm ERR! code EPERM
npm ERR! syscall unlink
npm ERR! path D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\.staging\antd-3a918c71\dist\antd.js
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, unlink 'D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\.staging\antd-3a918c71\dist\antd.js'

and again in another run about 4 hours and a gazillion attempts later:

npm WARN tarball tarball data for antd@3.25.3 (sha1-KEy/g89atsJt/7YGZXbyAupgr1A=) seems to be corrupted. Trying one more time.
npm WARN tarball tarball data for monaco-editor@0.18.1 (sha1-ztfDBaIxCYdf7q85WlBLkfY1jPw=) seems to be corrupted. Trying one more time.
npm ERR! code EPERM
npm ERR! syscall unlink
npm ERR! path D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\antd\dist\antd-with-locales.min.js.map
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, unlink 'D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\antd\dist\antd-with-locales.min.js.map'
npm ERR!  { [Error: EPERM: operation not permitted, unlink 'D:\Share\Develop\xy\projects\tools\dashboard\react-frontend\node_modules\antd\dist\antd-with-locales.min.js.map']

Surprisingly the sha1 hash for antd mentioned in the latest instance and flagged as corrupt is in fact correct as can be calculated via sha1sum on the downloaded package from artifactory and from npmjs:

$ npm v antd@3.25.3

antd@3.25.3 | MIT | deps: 53 | versions: 466
An enterprise-class UI design language and React components implementation
http://ant.design/

keywords: ant, component, components, design, framework, frontend, react, react-component, ui

dist
.tarball: https://registry.npmjs.org/antd/-/antd-3.25.3.tgz
.shasum: 284cbf83cf5ab6c26dffb6066576f202ea60af50
.integrity: sha512-qTegXllFsswzfSUyZHS8hebfUSqJ7uriDRcbE4Iwsa+/+IioHwDqha8cSzdbqrUqdjnozVV2vFDiGvRrI5fwEA==
.unpackedSize: 50.3 MB

dependencies:
@ant-design/create-react-context: ^0.2.4 babel-runtime: 6.x                       enquire.js: ^2.1.6                       raf: ^3.4.1                              rc-collapse: ~1.11.3                     
@ant-design/icons-react: ~2.0.1          c

Now in a node interactive REPL, I will get the exact same value (i. e. KEy/g89atsJt/7YGZXbyAupgr1A=) if I convert the above checksum to base64 representation:

Buffer.from('284cbf83cf5ab6c26dffb6066576f202ea60af50', 'hex').toString('base64')
'KEy/g89atsJt/7YGZXbyAupgr1A='

The problem has never occurred on our CI system (Jenkins) which is run on the same server rack as artifactory. Nor does it occur if we bypass artifactory altogether and access npmjs directly (as I've already mentioned above). We have a bunch of developers working on linux/macos and about 6-7 developers running on windows. Almost every windows user in the team has been affected at least once in the course of the last 2 months (after we introduced npm into the build pipeline). It's a showstopper par excellence to say the least.
Searching the internet yields many bug-reports that describe the same problem but the proposed solutions at best amount to voodoo programming.

We took a closer look at the EPERM issue and it turns out that the Node process leaks the file-handle (pay attention to the entry with the label DELETE_PENDING):

npm_problem

Image Pasted at 2019-12-4 14-41

The EPERM error presumably results from npm failing to close the file-handle in time. The file-handle is only closed once Node.exe itself terminates. In other words: the process blocking the file and the process trying to delete it (more preceisely unlink it) are identical (as evident by the fact that they share the same PID).

Our theory so far is the following:
For some reason fetching large files occasionally fails. If that happens on linux, there is a file-handle leak but that doesn't prevent npm from trying again and finally successfully downloading the file. The file-handle leak on windows however prevents npm from deleting/overwriting the file in question in its further attempts. This ultimately causes npm ci (or install) to fail.
Maybe there is a missing fs.closeSync(fd) in the respective npm code for fetching packages in case of an exception.

We tried this with different versions of node/npm btw. We had successfull runs and failures with all these versions. We cleared the cache using npm cache clean --force before each run. In particular we tested the following versions of node (and the respective npm version packaged along with it):

  • LTS 8.12.0
  • LTS 8.16.2
  • LTS 10.16.3
  • LTS 12.13.0
  • 13.3.0

Anyways, we had to get things working so we switched over to yarn and it works like a charm. Unfortunately that also means one further dependency to manage so I personally would prefer a less brittle npm. Maybe that helps you guys investigate and work out the issue.

@Download
Copy link

Seeing the same thing:

C:\ws\sol>npm ci
npm WARN prepare removing existing node_modules/ before installation
npm ERR! path C:\ws\sol\node_modules\tape
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall unlink
npm ERR! Error: EPERM: operation not permitted, unlink 'C:\ws\sol\node_modules\tape'
npm ERR!  { [Error: EPERM: operation not permitted, unlink 'C:\ws\sol\node_modules\tape']
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, unlink 'C:\ws\sol\node_modules\tape'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'unlink',
npm ERR!      path: 'C:\\ws\\sol\\node_modules\\tape' },
npm ERR!   stack:
npm ERR!    'Error: EPERM: operation not permitted, unlink \'C:\\ws\\sol\\node_modules\\tape\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'unlink',
npm ERR!   path: 'C:\\ws\\sol\\node_modules\\tape' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\stijn\AppData\Roaming\npm-cache\_logs\2019-12-16T12_03_09_625Z-debug.log

@Download
Copy link

If I run npm ci multiple times, it eventually succeeds, but the error keeps coming back as well

@momesana
Copy link
Author

momesana commented Jan 12, 2020

If I run npm ci multiple times, it eventually succeeds, but the error keeps coming back as well

Hi @Download,
if that issue ever shows up again, it might be helpful if you also ran the process monitor to investigate what exactly causes the EPERM "hiccup".

@darcyclarke darcyclarke added platform:windows is Windows-specific Bug thing that needs fixing labels Oct 30, 2020
@darcyclarke darcyclarke added the Release 6.x work is associated with a specific npm 6 release label Nov 19, 2020
@biltongza
Copy link

see microsoft/TypeScript#29407

@Alphy11
Copy link

Alphy11 commented Feb 17, 2021

@momesana I am guessing since you started working around it with yarn you never got to the bottom of this? I am seeing this happen once in a while in our CI builds, always with the same package. I am at a loss as to why it is happening since we are killing hanging node processes.

@Download
Copy link

@momesana Yes when I encounter it again I will do this.
That said, I don't know if it's luck or what (knocks on wood) but NPM has been working very well for me lately.
Where at some point I almost got used to seeing lots of file management related Windows-only issues with NPM, for about a year or so I hardly have seen any. I at one point also switched to Yarn, but I switched back because NPM got so much more reliable.

@biltongza
Copy link

My team actually put in a retry loop on npm ci because of this issue, it only gives up after 5 retries. Made our problems go away, but it does mean it takes extra time when it fails.

@igorjacauna
Copy link

For me the problem was solved running terminal/vscode as admin when I try to run npm ci

@darcyclarke
Copy link
Contributor

npm v6 is no longer in active development; We will continue to push security releases to v6 at our team's discretion as-per our Support Policy.

If your bug is preproducible on v7, please re-file this issue using our new issue template.

If your issue was a feature request, please consider opening a new RRFC or RFC. If your issue was a question or other idea that was not CLI-specific, consider opening a discussion on our feedback repo

Closing: This is an automated message.

1 similar comment
@darcyclarke
Copy link
Contributor

npm v6 is no longer in active development; We will continue to push security releases to v6 at our team's discretion as-per our Support Policy.

If your bug is preproducible on v7, please re-file this issue using our new issue template.

If your issue was a feature request, please consider opening a new RRFC or RFC. If your issue was a question or other idea that was not CLI-specific, consider opening a discussion on our feedback repo

Closing: This is an automated message.

@momesana
Copy link
Author

momesana commented Sep 16, 2022

@momesana I am guessing since you started working around it with yarn you never got to the bottom of this?

yep, we never had issues like that with yarn though yarn has its own problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing platform:windows is Windows-specific Release 6.x work is associated with a specific npm 6 release
Projects
None yet
Development

No branches or pull requests

6 participants