Skip to content

Commit 3946d4d

Browse files
Run each copy's patch engine behind a pointer, so the in-process gem tests fit their stack on Windows (#2432)
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 762cec2 commit 3946d4d

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎crates/socket-patch-cli/src/ecosystem_dispatch.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,10 @@ impl JvmScope {
16311631
/// temporary in that fn's poll frame first, and a debug build keeps the
16321632
/// room for it. Here the temporary is in a frame that is gone before
16331633
/// anything is polled: never inlined, or it would be the caller's again.
1634+
///
1635+
/// The core crate has the same helper for the patch engines
1636+
/// (`patch::apply::boxed`), with a `Send` bound this one does not need;
1637+
/// keep the two in step.
16341638
#[inline(never)]
16351639
pub(crate) fn boxed<'a, T, F, Fut>(
16361640
make: F,

‎crates/socket-patch-core/src/patch/apply.rs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,32 @@ struct FileOp {
984984
saved: Option<PathBuf>,
985985
}
986986

987+
/// Box the future `make` returns, built in this (non-async) frame so that
988+
/// the caller's poll frame holds only the pointer.
989+
///
990+
/// A debug build keeps room in an `async fn`'s poll frame for every future
991+
/// it awaits inline, in every branch, and `Box::pin(big(..))` written there
992+
/// builds the future in that frame first. The patch engines sit deep in the
993+
/// call chain of `apply`, `rollback` and `remove`, and the whole chain has
994+
/// to fit the stack of the thread it runs on (2 MiB for a test's). Here the
995+
/// temporary is in a frame that is gone before anything is polled: never
996+
/// inlined, or it would be the caller's again.
997+
///
998+
/// The CLI has the same helper for its command frames
999+
/// (`ecosystem_dispatch::boxed`), without the `Send` bound: this one keeps
1000+
/// the engines' futures `Send`, as they were before they were boxed. Keep
1001+
/// the two in step.
1002+
#[inline(never)]
1003+
pub(crate) fn boxed<'a, T, F, Fut>(
1004+
make: F,
1005+
) -> std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>
1006+
where
1007+
F: FnOnce() -> Fut,
1008+
Fut: std::future::Future<Output = T> + Send + 'a,
1009+
{
1010+
Box::pin(make())
1011+
}
1012+
9871013
/// The sibling name an original is kept under during a [`PackageWrite`].
9881014
const SAVED_PREFIX: &str = ".socket-orig-";
9891015

@@ -1761,8 +1787,12 @@ pub async fn apply_package_patch(
17611787
dry_run: bool,
17621788
policy: MismatchPolicy,
17631789
) -> ApplyResult {
1790+
// One copy's engine runs behind a pointer: its future is the largest
1791+
// of the chain, and every caller up to the command would otherwise
1792+
// hold room for it in its own poll frame (see `boxed`).
17641793
crate::patch::store_copies::fan_out(scope, package_key, pkg_path, |path| async move {
1765-
apply_package_patch_at(scope, package_key, &path, files, sources, dry_run, policy).await
1794+
boxed(|| apply_package_patch_at(scope, package_key, &path, files, sources, dry_run, policy))
1795+
.await
17661796
})
17671797
.await
17681798
}

‎crates/socket-patch-core/src/patch/rollback.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,13 @@ pub async fn rollback_package_patch(
394394
blobs_path: &Path,
395395
dry_run: bool,
396396
) -> RollbackResult {
397+
// One copy's engine runs behind a pointer, as apply's does (see
398+
// `apply::boxed`).
397399
crate::patch::store_copies::fan_out(scope, package_key, pkg_path, |path| async move {
398-
rollback_package_patch_at(scope, package_key, &path, files, blobs_path, dry_run).await
400+
crate::patch::apply::boxed(|| {
401+
rollback_package_patch_at(scope, package_key, &path, files, blobs_path, dry_run)
402+
})
403+
.await
399404
})
400405
.await
401406
}
@@ -432,8 +437,10 @@ pub async fn rollback_store_copies_holding_patch(
432437
if !holds_this_patch(&copy, files).await {
433438
continue;
434439
}
435-
let copy_result =
436-
rollback_package_patch_at(scope, package_key, &copy, files, blobs_path, dry_run).await;
440+
let copy_result = crate::patch::apply::boxed(|| {
441+
rollback_package_patch_at(scope, package_key, &copy, files, blobs_path, dry_run)
442+
})
443+
.await;
437444
let result = folded.get_or_insert_with(|| RollbackResult {
438445
package_key: package_key.to_string(),
439446
package_path: pkg_path.display().to_string(),

0 commit comments

Comments
 (0)