From bd6a6051203ff577240615ff4e242270035a36b7 Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:56:21 -0700 Subject: [PATCH] fix(install): ensure capacity of `preinstall_state` before cleaning lockfile (#11792) Co-authored-by: Jarred Sumner --- src/bun.js/bindings/webcore/WebSocket.cpp | 3 +-- src/install/install.zig | 6 +++--- src/install/lockfile.zig | 5 ++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/bun.js/bindings/webcore/WebSocket.cpp b/src/bun.js/bindings/webcore/WebSocket.cpp index f558625f6091c..0137f28568552 100644 --- a/src/bun.js/bindings/webcore/WebSocket.cpp +++ b/src/bun.js/bindings/webcore/WebSocket.cpp @@ -1210,13 +1210,12 @@ void WebSocket::didClose(unsigned unhandledBufferedAmount, unsigned short code, // so we just call decPendingActivityCount() after dispatching the event ASSERT(m_pendingActivityCount > 0); - if (this->hasEventListeners("close"_s)) { this->dispatchEvent(CloseEvent::create(wasClean, code, reason)); // we deinit if possible in the next tick if (auto* context = scriptExecutionContext()) { - context->postTask([this, code, wasClean, reason, protectedThis = Ref { *this }](ScriptExecutionContext& context) { + context->postTask([this, protectedThis = Ref { *this }](ScriptExecutionContext& context) { ASSERT(scriptExecutionContext()); protectedThis->decPendingActivityCount(); }); diff --git a/src/install/install.zig b/src/install/install.zig index 852fbd91ecab8..e3d128b8a62e0 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -3232,19 +3232,19 @@ pub const PackageManager = struct { } } - fn ensurePreinstallStateListCapacity(this: *PackageManager, count: usize) !void { + pub fn ensurePreinstallStateListCapacity(this: *PackageManager, count: usize) void { if (this.preinstall_state.items.len >= count) { return; } const offset = this.preinstall_state.items.len; - try this.preinstall_state.ensureTotalCapacity(this.allocator, count); + this.preinstall_state.ensureTotalCapacity(this.allocator, count) catch bun.outOfMemory(); this.preinstall_state.expandToCapacity(); @memset(this.preinstall_state.items[offset..], PreinstallState.unknown); } pub fn setPreinstallState(this: *PackageManager, package_id: PackageID, lockfile: *Lockfile, value: PreinstallState) void { - this.ensurePreinstallStateListCapacity(lockfile.packages.len) catch return; + this.ensurePreinstallStateListCapacity(lockfile.packages.len); this.preinstall_state.items[package_id] = value; } diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index 0a749caee6ea8..38c1fca83e28e 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -846,7 +846,10 @@ pub fn cleanWithLogger( // never grow // preinstall_state is used during installPackages. the indexes(package ids) need - // to be remapped + // to be remapped. Also ensure `preinstall_state` has enough capacity to contain + // all packages. It's possible it doesn't because non-npm packages do not use + // preinstall state before linking stage. + manager.ensurePreinstallStateListCapacity(old.packages.len); var preinstall_state = manager.preinstall_state; var old_preinstall_state = preinstall_state.clone(old.allocator) catch bun.outOfMemory(); defer old_preinstall_state.deinit(old.allocator);