From 49a28f3509b7bce2048f1eb02d57805d6093aad4 Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Tue, 1 Dec 2026 20:30:29 -0500 Subject: [PATCH 1/2] Run each copy's patch engine behind a pointer The in-process tests run a command on a libtest thread, which has a 2 MiB stack. The deepest chain there is scan -> the nested apply -> apply_gem_package_patch -> apply_package_patch -> the store-copy fan-out -> the engine. Every async fn on that chain awaits apply_package_patch inline, and a debug build keeps room for the awaited future in the caller's poll frame at each call site. The engine's future is the largest of them, so every caller up to the command carried it. apply_package_patch, rollback_package_patch and rollback_store_copies_holding_patch now build the per-copy engine future through a non-inlined boxed(), so their own futures hold a pointer. No behaviour changes. On macOS (arm64, debug) the smallest stack that remove_base_purl_clears_all_platforms_and_rolls_back of in_process_gem_multi_platform passes with goes from 1972 KiB to 1500 KiB. On Windows that test overflowed the stack on a branch that adds nothing to the chain beyond current main. Co-Authored-By: Claude Fable 5.2 --- crates/socket-patch-core/src/patch/apply.rs | 27 ++++++++++++++++++- .../socket-patch-core/src/patch/rollback.rs | 13 ++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/socket-patch-core/src/patch/apply.rs b/crates/socket-patch-core/src/patch/apply.rs index f1f1a7f888..c3b1ac3352 100644 --- a/crates/socket-patch-core/src/patch/apply.rs +++ b/crates/socket-patch-core/src/patch/apply.rs @@ -771,6 +771,27 @@ struct FileOp { saved: Option, } +/// Box the future `make` returns, built in this (non-async) frame so that +/// the caller's poll frame holds only the pointer. +/// +/// A debug build keeps room in an `async fn`'s poll frame for every future +/// it awaits inline, in every branch, and `Box::pin(big(..))` written there +/// builds the future in that frame first. The patch engines sit deep in the +/// call chain of `apply`, `rollback` and `remove`, and the whole chain has +/// to fit the stack of the thread it runs on (2 MiB for a test's). Here the +/// temporary is in a frame that is gone before anything is polled: never +/// inlined, or it would be the caller's again. +#[inline(never)] +pub(crate) fn boxed<'a, T, F, Fut>( + make: F, +) -> std::pin::Pin + Send + 'a>> +where + F: FnOnce() -> Fut, + Fut: std::future::Future + Send + 'a, +{ + Box::pin(make()) +} + /// The sibling name an original is kept under during a [`PackageWrite`]. const SAVED_PREFIX: &str = ".socket-orig-"; @@ -1516,8 +1537,12 @@ pub async fn apply_package_patch( dry_run: bool, policy: MismatchPolicy, ) -> ApplyResult { + // One copy's engine runs behind a pointer: its future is the largest + // of the chain, and every caller up to the command would otherwise + // hold room for it in its own poll frame (see `boxed`). crate::patch::store_copies::fan_out(scope, package_key, pkg_path, |path| async move { - apply_package_patch_at(scope, package_key, &path, files, sources, dry_run, policy).await + boxed(|| apply_package_patch_at(scope, package_key, &path, files, sources, dry_run, policy)) + .await }) .await } diff --git a/crates/socket-patch-core/src/patch/rollback.rs b/crates/socket-patch-core/src/patch/rollback.rs index d1756e3e6e..c3fbdc0975 100644 --- a/crates/socket-patch-core/src/patch/rollback.rs +++ b/crates/socket-patch-core/src/patch/rollback.rs @@ -394,8 +394,13 @@ pub async fn rollback_package_patch( blobs_path: &Path, dry_run: bool, ) -> RollbackResult { + // One copy's engine runs behind a pointer, as apply's does (see + // `apply::boxed`). crate::patch::store_copies::fan_out(scope, package_key, pkg_path, |path| async move { - rollback_package_patch_at(scope, package_key, &path, files, blobs_path, dry_run).await + crate::patch::apply::boxed(|| { + rollback_package_patch_at(scope, package_key, &path, files, blobs_path, dry_run) + }) + .await }) .await } @@ -432,8 +437,10 @@ pub async fn rollback_store_copies_holding_patch( if !holds_this_patch(©, files).await { continue; } - let copy_result = - rollback_package_patch_at(scope, package_key, ©, files, blobs_path, dry_run).await; + let copy_result = crate::patch::apply::boxed(|| { + rollback_package_patch_at(scope, package_key, ©, files, blobs_path, dry_run) + }) + .await; let result = folded.get_or_insert_with(|| RollbackResult { package_key: package_key.to_string(), package_path: pkg_path.display().to_string(), From 41246147fae6ba4c21c4a24562dc8c4389c83553 Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Tue, 1 Dec 2026 20:39:43 -0500 Subject: [PATCH 2/2] Point the two boxed helpers at each other Core's boxed (for the patch engines) and the CLI's (for its command frames) are the same helper but for a Send bound. Each doc comment now names the other, so a change to one is made to both. Co-Authored-By: Claude Fable 5.2 --- crates/socket-patch-cli/src/ecosystem_dispatch.rs | 4 ++++ crates/socket-patch-core/src/patch/apply.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/crates/socket-patch-cli/src/ecosystem_dispatch.rs b/crates/socket-patch-cli/src/ecosystem_dispatch.rs index e5c28f452e..1d8ee51a5b 100644 --- a/crates/socket-patch-cli/src/ecosystem_dispatch.rs +++ b/crates/socket-patch-cli/src/ecosystem_dispatch.rs @@ -1541,6 +1541,10 @@ impl JvmScope { /// temporary in that fn's poll frame first, and a debug build keeps the /// room for it. Here the temporary is in a frame that is gone before /// anything is polled: never inlined, or it would be the caller's again. +/// +/// The core crate has the same helper for the patch engines +/// (`patch::apply::boxed`), with a `Send` bound this one does not need; +/// keep the two in step. #[inline(never)] pub(crate) fn boxed<'a, T, F, Fut>( make: F, diff --git a/crates/socket-patch-core/src/patch/apply.rs b/crates/socket-patch-core/src/patch/apply.rs index c3b1ac3352..77db17f20e 100644 --- a/crates/socket-patch-core/src/patch/apply.rs +++ b/crates/socket-patch-core/src/patch/apply.rs @@ -781,6 +781,11 @@ struct FileOp { /// to fit the stack of the thread it runs on (2 MiB for a test's). Here the /// temporary is in a frame that is gone before anything is polled: never /// inlined, or it would be the caller's again. +/// +/// The CLI has the same helper for its command frames +/// (`ecosystem_dispatch::boxed`), without the `Send` bound: this one keeps +/// the engines' futures `Send`, as they were before they were boxed. Keep +/// the two in step. #[inline(never)] pub(crate) fn boxed<'a, T, F, Fut>( make: F,