Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
service-worker
Is this a regression?
Yes
Description
We recently migrated our large single page application from Angular 9 to Angular 13.
Our production build enables the service worker generation, and the "assetsGroups->resources->files " section include "/assets/**"
During our first production build on a Windows 10 machine (16GB ram), we received this message:
Service worker generation failed.
EMFILE: too many open files,
Our application's assets folder contains all the dictionaries for at least 13 different languages: every folder cointains at least a thousand files: the service worker generation stops right in the middle.
We tried to split the assetsGroups in multiple elements (one for each language folder), but the service worker generation stopped exactly in the same spot.
Next we checked the source code for the assetsGroups generation and the difference between angular 9 and 13. If we are looking in the right source file, from what we saw seems like that the hashing of all assets files is responsible for the error (\node_modules@angular-devkit\build-angular\src\utils\service-worker.js)
Attached to the issue you'll find a zip with our sample app: (in any case, we merely create a new app with "ng new", added the angular/service-worker package and the assetsGroup section, and used "ng build --configuration=production"
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
Service worker generation failed: Too many open files
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 13.2.2
Node: 16.14.0
Package Manager: npm 8.4.1
OS: win32 x64
Angular: 13.2.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, localize, material, platform-browser
... platform-browser-dynamic, router, service-worker
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1302.2
@angular-devkit/build-angular 13.2.2
@angular-devkit/core 13.2.2
@angular-devkit/schematics 13.2.2
@angular/cli 13.2.2
@angular/flex-layout 13.0.0-beta.38
@schematics/angular 13.2.2
ng-packagr 13.2.1
rxjs 6.5.5
typescript 4.5.5
Intel I7 4771 3.50Ghz
16GB Ram
Anything else?
No response
Activity
ajafff commentedon Feb 18, 2022
The issue seems to be this line:
angular/packages/service-worker/config/src/generator.ts
Line 70 in 235bffc
it basically tries to read and hash all files in parallel. instead it should use some kind of queue to limit the parallelism. that ensures only a limited number of files is open at the same time.
gkalpak commentedon Feb 18, 2022
Thx for reporting this, @LucaBruni 👍
@ajafff is spot on!
Would any of you be interested in creating a pull request? 😃
LucaBruni commentedon Feb 23, 2022
In our solution we used a non-configurable "100" elements limit.
If that's ok, and adding a dependency to "p-limit" is not a problem, we'll submit a PR.
Otherwise we can create a suboptimal solution: just let us know.
gkalpak commentedon Feb 24, 2022
Thx for getting back, @LucaBruni.
A non-configurable limit is fine for now, I believe. I was thinking of a higher limit tbh (500 or 250), as I think that should be fine on all common platforms. Is your limit of 100 arbitrary or did you find higher limits to be problematic?
Regarding the implementation, I was thinking that bringing in another dependency (such as
p-limit) would be an overkill for the task at hand. I was thinking about a simple helper function that would allow us to process an array of items in batches. Something like:Then, we could use it in generator.ts like this (with whatever limit we decide on):
WDYT, @LucaBruni?
LucaBruni commentedon Feb 25, 2022
Thanks for the response: 500 is ok: our 100 limit was maybe too much on the safe side :D
Our critical threshold was around 5000, but I have no idea if that limit is ram-dependant or not.
Your batch solution works well for me.
gkalpak commentedon Feb 25, 2022
That's great! So, if you wanted to put that code in a PR and throw in a test or two, I would be happy to review and get it merged 😃
LucaBruni commentedon Mar 1, 2022
We are ready to create PR: we did not create additional tests as the existing ones should cover our needs.
Also, we have no idea how to create a test that involves hashing of 10000 files :)
(edit: we tested the service-worker dist on our sample and works just fine)
Let us know.
gkalpak commentedon Mar 2, 2022
Regarding the test:
We use a
MockFilesystemin tests (example). I was thinking that we could construct aMockFilesysteminstance with more files than the limit we have set and also overwrite thehashmethod to keep track of how many calls there are simultaneously. Then we could check that our limit was not exceeded.I.e. something like:
Does that make sense?
LucaBruni commentedon Mar 4, 2022
Yes, it does make sense: we used your HashTrackingMockFilesystem and added a single test.
Thanks
fix(service-worker): file system hash in batch of 500 elements (#45262)
angular-automatic-lock-bot commentedon Apr 8, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.