Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/install/PackageManager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ pub struct PackageManager {
///
/// TODO: Does this need to be atomic? It seems to be accessed only from the main thread.
pub(crate) pending_pre_calc_hashes: AtomicU32,
/// Manifest cache writes still running on the thread pool. Not part of
/// `pending_tasks` (nothing consumes their result), but `sleep_until` and
/// the runtime's module poll do not report done until they land.
pub pending_manifest_saves: AtomicU32,
pub pending_tasks: AtomicU32,
pub total_tasks: u32,
pub(crate) preallocated_network_tasks: PreallocatedNetworkTasks,
Expand Down Expand Up @@ -947,6 +951,24 @@ impl PackageManager {
trampoline::<C>,
)
};

// The tasks the caller waited on may have handed manifest cache
// writes to the thread pool (`Serializer::save_async`). Nothing reads
// their result, but returning — and letting the command exit — before
// they land makes what the next run finds cached a matter of thread
// scheduling, so every wait also covers them.
fn saves_landed(this: *mut c_void) -> bool {
// SAFETY: `this` is the `*mut PackageManager` passed below; atomic
// field projection only, no `&mut PackageManager` is formed.
unsafe {
(*this.cast::<PackageManager>())
.pending_manifest_saves
.load(Ordering::Acquire)
== 0
}
}
// SAFETY: as above; `saves_landed` never borrows `*event_loop`.
unsafe { AnyEventLoop::tick_raw(event_loop, this.cast::<c_void>(), saves_landed) };
}

pub(crate) fn ensure_temp_node_gyp_script(&mut self) -> Result<(), Error> {
Expand Down Expand Up @@ -1922,6 +1944,7 @@ pub fn init(
wr!(patch_calc_hash_batch, thread_pool::Batch::default());
wr!(patch_task_queue, PatchTaskQueue::default());
wr!(pending_pre_calc_hashes, AtomicU32::new(0));
wr!(pending_manifest_saves, AtomicU32::new(0));
wr!(pending_tasks, AtomicU32::new(0));
wr!(total_tasks, 0);
wr!(lifecycle_script_time_log, LifecycleScriptTimeLog::default());
Expand Down Expand Up @@ -2355,6 +2378,7 @@ fn init_with_runtime_once(
wr!(patch_task_fifo, PatchTaskFifo::init());
wr!(patch_task_queue, PatchTaskQueue::default());
wr!(pending_pre_calc_hashes, AtomicU32::new(0));
wr!(pending_manifest_saves, AtomicU32::new(0));
wr!(pending_tasks, AtomicU32::new(0));
wr!(total_tasks, 0);
wr!(lifecycle_script_time_log, LifecycleScriptTimeLog::default());
Expand Down
26 changes: 20 additions & 6 deletions src/install/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,11 +1237,10 @@ pub mod package_manifest {
}

/// We save into a temporary directory and then move the file to the cache directory.
/// Saving the files to the manifest cache doesn't need to prevent application exit.
/// It's an optional cache.
/// Therefore, we choose to not increment the pending task count or wake up the main thread.
///
/// This might leave temporary files in the temporary directory that will never be moved to the cache directory. We'll see if anyone asks about that.
/// Nothing consumes the result, so this is not a `pending_tasks` entry;
/// it is counted in `pending_manifest_saves` instead so the command
/// waits for the write to land before exiting (otherwise whether the
/// next run finds the manifest cached depends on thread scheduling).
pub(crate) fn save_async(
this: &PackageManifest,
scope: &registry::Scope,
Expand Down Expand Up @@ -1301,6 +1300,17 @@ pub mod package_manifest {
Output::flush();
}
}
drop(save_task);

let pm = crate::package_manager::get();
// SAFETY: atomic field projection from a worker thread (no
// `&mut PackageManager`); `wake_raw` is the thread-safe wake.
unsafe {
(*pm)
.pending_manifest_saves
.fetch_sub(1, core::sync::atomic::Ordering::Release);
PackageManager::wake_raw(pm);
}
}
}

Expand All @@ -1317,7 +1327,11 @@ pub mod package_manifest {

// SAFETY: task is a valid Box-allocated SaveTask
let batch = PoolBatch::from(unsafe { core::ptr::addr_of_mut!((*task).task) });
PackageManager::get().thread_pool.schedule(batch);
let manager = PackageManager::get();
manager
.pending_manifest_saves
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
manager.thread_pool.schedule(batch);
}

fn manifest_file_name<'b>(
Expand Down
4 changes: 3 additions & 1 deletion src/jsc/AsyncModule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ impl Queue {
// `container_of`-derived `*mut` reborrow. The package manager is a
// separate heap allocation, disjoint from `self` (= `vm.modules`).
let pm = VirtualMachine::get().as_mut().package_manager();
if pm.pending_tasks.load(Ordering::Relaxed) > 0 {
if pm.pending_tasks.load(Ordering::Relaxed) > 0
|| pm.pending_manifest_saves.load(Ordering::Relaxed) > 0
{
return;
}

Expand Down