-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(#6990): reduce likelihood of libzip OOM in larger dependency graphs #7007
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
Open
redwheelbarrow
wants to merge
3
commits into
yarnpkg:master
Choose a base branch
from
redwheelbarrow:fix-wasm-mem-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| releases: | ||
| "@yarnpkg/builder": patch | ||
| "@yarnpkg/cli": patch | ||
| "@yarnpkg/core": patch | ||
| "@yarnpkg/doctor": patch | ||
| "@yarnpkg/extensions": patch | ||
| "@yarnpkg/fslib": patch | ||
| "@yarnpkg/libzip": patch | ||
| "@yarnpkg/nm": patch | ||
| "@yarnpkg/plugin-catalog": patch | ||
| "@yarnpkg/plugin-compat": patch | ||
| "@yarnpkg/plugin-constraints": patch | ||
| "@yarnpkg/plugin-dlx": patch | ||
| "@yarnpkg/plugin-essentials": patch | ||
| "@yarnpkg/plugin-exec": patch | ||
| "@yarnpkg/plugin-file": patch | ||
| "@yarnpkg/plugin-git": patch | ||
| "@yarnpkg/plugin-github": patch | ||
| "@yarnpkg/plugin-http": patch | ||
| "@yarnpkg/plugin-init": patch | ||
| "@yarnpkg/plugin-interactive-tools": patch | ||
| "@yarnpkg/plugin-jsr": patch | ||
| "@yarnpkg/plugin-link": patch | ||
| "@yarnpkg/plugin-nm": patch | ||
| "@yarnpkg/plugin-npm": patch | ||
| "@yarnpkg/plugin-npm-cli": patch | ||
| "@yarnpkg/plugin-pack": patch | ||
| "@yarnpkg/plugin-patch": patch | ||
| "@yarnpkg/plugin-pnp": patch | ||
| "@yarnpkg/plugin-pnpm": patch | ||
| "@yarnpkg/plugin-stage": patch | ||
| "@yarnpkg/plugin-typescript": patch | ||
| "@yarnpkg/plugin-version": patch | ||
| "@yarnpkg/plugin-workspace-tools": patch | ||
| "@yarnpkg/pnp": patch | ||
| "@yarnpkg/pnpify": patch | ||
| "@yarnpkg/sdks": patch | ||
| "@yarnpkg/shell": patch | ||
| vscode-zipfs: patch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import {PortablePath} from '@yarn | |
| import {Libzip} from '@yarnpkg/libzip'; | ||
|
|
||
| import {ZipImplInput, type CompressionData, type Stat, type ZipImpl} from './ZipFS'; | ||
| import {getInstance} from './instance'; | ||
| import {newInstance} from './instance'; | ||
|
|
||
|
|
||
| export class LibzipError extends Error { | ||
|
|
@@ -16,12 +16,93 @@ export class LibzipError extends Error { | |
| } | ||
| } | ||
|
|
||
|
|
||
| type LibzipInstance = {instance: Libzip | null, active: boolean, reserved: number, highWaterMark: number}; | ||
| type LibzipReservation = {byteLength: number, instanceIndex: number}; | ||
| /** | ||
| * Tracks the estimate of WASM memory usage by libzip to reduce the risk | ||
| * of OOM errors. | ||
| * | ||
| * Internally, favors the oldest WASM instances to minimize fragmentation. | ||
| * Cleans up instances when older instances have space to accomodate new zips. | ||
| */ | ||
| class ElasticLibzipFactory { | ||
| private static readonly LIBZIP_METADATA = 512 * 1024; // 500KB | ||
| private static readonly WASM_MEM_MAX = 2 * 1024 * 1024 * 1024 - (100 * 1024 * 1024); // 1.9GB | ||
| private static KEY = 1; | ||
|
|
||
| /** | ||
| * The WASM instances, their currently reserved memory, and the high water mark, since | ||
| * WASM memory isn't usually shrinkable. | ||
| */ | ||
| private readonly instances: Array<LibzipInstance> = []; | ||
| /** | ||
| * The reservations by unique ID, and the index into the {@link instances} array. | ||
| */ | ||
| private readonly reservations = new Map<number, LibzipReservation>(); | ||
|
|
||
| /** | ||
| * Provide (and possibly build new) a libzip WASM for the given ZIP byte length | ||
| * | ||
| * @param byteLength The size of the ZIP file | ||
| * @returns [unique ID, Libzip instance] | ||
| */ | ||
| getInstance(byteLength: number): [number, Libzip] { | ||
| const size = byteLength + ElasticLibzipFactory.LIBZIP_METADATA; | ||
| let index = this.instances.findIndex(i => i.active && (i.reserved + size) < ElasticLibzipFactory.WASM_MEM_MAX); | ||
| let instance; | ||
|
|
||
| if (index >= 0) { | ||
| instance = this.instances[index]; | ||
| instance.reserved += size; | ||
| instance.highWaterMark = Math.max(instance.highWaterMark, instance.reserved); | ||
| } else { | ||
| index = this.instances.length; | ||
| instance = {instance: newInstance(), reserved: size, highWaterMark: size, active: true}; | ||
| this.instances.push(instance); | ||
| } | ||
| ElasticLibzipFactory.KEY += 1; | ||
| this.reservations.set(ElasticLibzipFactory.KEY, {byteLength: size, instanceIndex: index}); | ||
| return [ElasticLibzipFactory.KEY, instance.instance!]; | ||
| } | ||
|
|
||
| remove(key: number) { | ||
| const reservation = this.reservations.get(key); | ||
| if (!reservation) | ||
| return; | ||
|
|
||
| this.reservations.delete(key); | ||
|
|
||
| const instance = this.instances[reservation.instanceIndex]; | ||
| instance.reserved -= reservation.byteLength; | ||
| this.cleanup(reservation); | ||
| } | ||
|
|
||
| /** | ||
| * Remove the reservation's instance if the previous one has enough space, | ||
| * or if the reservations instance is nearly out of memory. | ||
| * | ||
| * @param reservation | ||
| */ | ||
| private cleanup(reservation: LibzipReservation) { | ||
| const instance = this.instances[reservation.instanceIndex]; | ||
|
|
||
| if (instance.reserved <= 0) { | ||
| instance.active = false; | ||
| this.instances[reservation.instanceIndex].instance = null; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to believe the native WASM will get unloaded by being dereferenced, but I don't know. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| const libzipFactory = new ElasticLibzipFactory(); | ||
|
|
||
| export class LibZipImpl implements ZipImpl { | ||
| private readonly libzip: Libzip; | ||
| private readonly lzSource: number; | ||
| private readonly zip: number; | ||
| private readonly listings: Array<string>; | ||
| private readonly symlinkCount: number; | ||
| private readonly key: number; | ||
|
|
||
| public filesShouldBeCached = true; | ||
|
|
||
|
|
@@ -30,7 +111,7 @@ export class LibZipImpl implements ZipImpl { | |
| ? opts.buffer | ||
| : opts.baseFs.readFileSync(opts.path); | ||
|
|
||
| this.libzip = getInstance(); | ||
| [this.key, this.libzip] = libzipFactory.getInstance(buffer.byteLength); | ||
|
|
||
| const errPtr = this.libzip.malloc(4); | ||
| try { | ||
|
|
@@ -50,20 +131,20 @@ export class LibZipImpl implements ZipImpl { | |
| if (this.zip === 0) { | ||
| const error = this.libzip.struct.errorS(); | ||
| this.libzip.error.initWithCode(error, this.libzip.getValue(errPtr, `i32`)); | ||
|
|
||
| throw this.makeLibzipError(error); | ||
| } | ||
| } catch(error) { | ||
| libzipFactory.remove(this.key); | ||
| throw error; | ||
| } finally { | ||
| this.libzip.free(errPtr); | ||
| } | ||
|
|
||
| const entryCount = this.libzip.getNumEntries(this.zip, 0); | ||
|
|
||
| const listings = new Array<string>(entryCount); | ||
| this.listings = new Array<string>(entryCount); | ||
| for (let t = 0; t < entryCount; ++t) | ||
| listings[t] = this.libzip.getName(this.zip, t, 0); | ||
|
|
||
| this.listings = listings; | ||
| this.listings[t] = this.libzip.getName(this.zip, t, 0); | ||
|
|
||
| this.symlinkCount = this.libzip.ext.countSymlinks(this.zip); | ||
| if (this.symlinkCount === -1) { | ||
|
|
@@ -121,6 +202,7 @@ export class LibZipImpl implements ZipImpl { | |
| throw this.makeLibzipError(this.libzip.getError(this.zip)); | ||
| } | ||
| } | ||
|
|
||
| return newIndex; | ||
| } catch (error) { | ||
| this.libzip.source.free(lzSource); | ||
|
|
@@ -261,6 +343,7 @@ export class LibZipImpl implements ZipImpl { | |
| } finally { | ||
| this.libzip.source.close(this.lzSource); | ||
| this.libzip.source.free(this.lzSource); | ||
| libzipFactory.remove(this.key); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -307,5 +390,6 @@ export class LibZipImpl implements ZipImpl { | |
|
|
||
| public discard(): void { | ||
| this.libzip.discard(this.zip); | ||
| libzipFactory.remove(this.key); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to fix doc