From 31ad4e533af3c5c2ecfb3df44600f6d627b030ae Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Wed, 25 Nov 2026 02:01:20 -0500 Subject: [PATCH 1/2] Keep the hosted flow off the stack of every scan `scan_project` awaited the hosted flow in line, so the flow's future, which embeds the whole hosted engine, lay in the poll frame of a scan in every mode, an agent scan too. The deepest in-process test, `in_process_gem_multi_platform::rollback_all_over_broad_manifest_succeeds`, needs about 2,013,000 of the 2,097,152 bytes of a test thread on macOS, and Windows has less room: a change that made one struct of the hosted flow 64 bytes larger overflowed that test's stack there. The flow is now built by a constructor that is not async, the way the vendored flows of a scan already are, so a scan holds one pointer to it. That test needs about 49 KB less stack: it passes with a stack of 1,966,080 bytes and overflows at 1,949,696, where before it passed at 2,002,944 and overflowed at 1,998,848 (`RUST_MIN_STACK`, which this machine rounds to 16 KiB). What a scan does is unchanged. Co-Authored-By: Claude Fable 5.2 --- .../src/commands/scan/hosted.rs | 42 ++++++++++++++++++- .../socket-patch-cli/src/commands/scan/mod.rs | 4 +- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/crates/socket-patch-cli/src/commands/scan/hosted.rs b/crates/socket-patch-cli/src/commands/scan/hosted.rs index 19759632a..03e271e2b 100644 --- a/crates/socket-patch-cli/src/commands/scan/hosted.rs +++ b/crates/socket-patch-cli/src/commands/scan/hosted.rs @@ -1146,11 +1146,51 @@ fn gem_sha_key(purl: &str) -> (String, String) { .unwrap_or_default() } +/// Transient-frame boxed constructor for [`run_redirect`]. `scan_project` +/// awaited the hosted flow in line, so the flow's future, which embeds the +/// whole hosted engine, lay in the poll frame of a scan in every mode, an +/// agent scan too. Built here, on a frame that is gone before anything is +/// polled, it costs a scan one pointer (the 2 MiB stack of a test thread +/// and the main thread's on Windows; same rationale as scan's `boxed_*` +/// family). +#[allow(clippy::too_many_arguments)] +pub(super) fn boxed_run_redirect<'a>( + args: &'a ScanArgs, + api_client: &'a socket_patch_core::api::client::ApiClient, + all_packages_with_patches: &'a [BatchPackagePatches], + can_access_paid_patches: bool, + policy: &'a super::policy::ScanPolicy, + scan_result: Envelope, + npm_prior: Option<&'a crate::ecosystem_dispatch::NpmCrawlSnapshot>, + recorded: &'a super::rollout::RecordedState<'a>, + batch_failed: bool, + stage: &'a mut super::rollout::Stage, + prior: Option>, + references: Option, + prune: bool, +) -> std::pin::Pin + 'a>> { + Box::pin(run_redirect( + args, + api_client, + all_packages_with_patches, + can_access_paid_patches, + policy, + scan_result, + npm_prior, + recorded, + batch_failed, + stage, + prior, + references, + prune, + )) +} + /// `scan --mode hosted --json`: select through `discover_selected`, then run /// [`run_redirect_selected`]. The human arm calls /// [`boxed_run_redirect_selected`] from `scan/mod.rs` directly. #[allow(clippy::too_many_arguments)] -pub(super) async fn run_redirect( +async fn run_redirect( args: &ScanArgs, api_client: &socket_patch_core::api::client::ApiClient, all_packages_with_patches: &[BatchPackagePatches], diff --git a/crates/socket-patch-cli/src/commands/scan/mod.rs b/crates/socket-patch-cli/src/commands/scan/mod.rs index c1d29b6d9..200ed48ae 100644 --- a/crates/socket-patch-cli/src/commands/scan/mod.rs +++ b/crates/socket-patch-cli/src/commands/scan/mod.rs @@ -74,8 +74,8 @@ pub(crate) use self::discovery::{ vendored_ledger_supplement as project_vendored_supplement, }; use self::gc::gc_into; +use self::hosted::boxed_run_redirect; pub(crate) use self::hosted::boxed_run_redirect_selected; -use self::hosted::run_redirect; use self::vendor_flow::{ boxed_vendor_interactive_path, boxed_vendor_json_path, partition_skipped_selected, }; @@ -3676,7 +3676,7 @@ async fn scan_project( if let Some(sweep) = withdrawn_sweep { sweep.record_into(&mut env); } - return run_redirect( + return boxed_run_redirect( &args, &api_client, &all_packages_with_patches, From 32d550654c1c38f733c34119d237a68ad87ca9ec Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Wed, 25 Nov 2026 02:08:41 -0500 Subject: [PATCH 2/2] Box a scan's futures through the one helper, at three places The wrapper of the last commit repeated the thirteen parameters of the hosted flow. The crate already has a helper that builds a future on a frame of its own and returns the pointer (`boxed`, used by the crawl): it is now visible to the crate and never inlined, since inlined it would put the future back into its caller's frame, and a scan uses it instead of a wrapper of its own. Two more futures of a scan were temporaries of a frame on the same chain: `run_scan` wrote `Box::pin(scan_project(..))` in line, and the hosted flow awaited its inner flow in line where the other two callers of that flow already go through `boxed_run_redirect_selected`. Both are built off the frame now. The deepest in-process test passes with a stack of 1,933,312 bytes and overflows at 1,916,928 (`RUST_MIN_STACK`, 16 KiB steps); with the last commit alone it needed 1,966,080, and on main 2,002,944. Co-Authored-By: Claude Fable 5.2 --- .../src/commands/scan/hosted.rs | 44 +-------------- .../socket-patch-cli/src/commands/scan/mod.rs | 54 +++++++++---------- .../src/ecosystem_dispatch.rs | 10 +++- 3 files changed, 38 insertions(+), 70 deletions(-) diff --git a/crates/socket-patch-cli/src/commands/scan/hosted.rs b/crates/socket-patch-cli/src/commands/scan/hosted.rs index 03e271e2b..40a94ae94 100644 --- a/crates/socket-patch-cli/src/commands/scan/hosted.rs +++ b/crates/socket-patch-cli/src/commands/scan/hosted.rs @@ -1146,51 +1146,11 @@ fn gem_sha_key(purl: &str) -> (String, String) { .unwrap_or_default() } -/// Transient-frame boxed constructor for [`run_redirect`]. `scan_project` -/// awaited the hosted flow in line, so the flow's future, which embeds the -/// whole hosted engine, lay in the poll frame of a scan in every mode, an -/// agent scan too. Built here, on a frame that is gone before anything is -/// polled, it costs a scan one pointer (the 2 MiB stack of a test thread -/// and the main thread's on Windows; same rationale as scan's `boxed_*` -/// family). -#[allow(clippy::too_many_arguments)] -pub(super) fn boxed_run_redirect<'a>( - args: &'a ScanArgs, - api_client: &'a socket_patch_core::api::client::ApiClient, - all_packages_with_patches: &'a [BatchPackagePatches], - can_access_paid_patches: bool, - policy: &'a super::policy::ScanPolicy, - scan_result: Envelope, - npm_prior: Option<&'a crate::ecosystem_dispatch::NpmCrawlSnapshot>, - recorded: &'a super::rollout::RecordedState<'a>, - batch_failed: bool, - stage: &'a mut super::rollout::Stage, - prior: Option>, - references: Option, - prune: bool, -) -> std::pin::Pin + 'a>> { - Box::pin(run_redirect( - args, - api_client, - all_packages_with_patches, - can_access_paid_patches, - policy, - scan_result, - npm_prior, - recorded, - batch_failed, - stage, - prior, - references, - prune, - )) -} - /// `scan --mode hosted --json`: select through `discover_selected`, then run /// [`run_redirect_selected`]. The human arm calls /// [`boxed_run_redirect_selected`] from `scan/mod.rs` directly. #[allow(clippy::too_many_arguments)] -async fn run_redirect( +pub(super) async fn run_redirect( args: &ScanArgs, api_client: &socket_patch_core::api::client::ApiClient, all_packages_with_patches: &[BatchPackagePatches], @@ -1258,7 +1218,7 @@ async fn run_redirect( .iter() .map(|r| (r.writer.purl.clone(), r.writer.uuid.clone())) .collect(); - run_redirect_selected( + boxed_run_redirect_selected( &args.common, &args.vex, prune, diff --git a/crates/socket-patch-cli/src/commands/scan/mod.rs b/crates/socket-patch-cli/src/commands/scan/mod.rs index 200ed48ae..41fcf0150 100644 --- a/crates/socket-patch-cli/src/commands/scan/mod.rs +++ b/crates/socket-patch-cli/src/commands/scan/mod.rs @@ -34,8 +34,8 @@ use std::path::{Path, PathBuf}; use crate::args::{apply_env_toggles, GlobalArgs}; use crate::commands::vex::{generate_vex_from_manifest_path, VexEmbedArgs}; use crate::ecosystem_dispatch::{ - crawl_ecosystems_with_npm, crawl_ecosystems_with_npm_roots, find_package_paths_for_path_scope, - partition_purls, + boxed, crawl_ecosystems_with_npm, crawl_ecosystems_with_npm_roots, + find_package_paths_for_path_scope, partition_purls, }; use crate::json_envelope::{ usage_error, Command as JsonCommand, Envelope, EnvelopeError, PatchAction, PatchEvent, @@ -74,8 +74,8 @@ pub(crate) use self::discovery::{ vendored_ledger_supplement as project_vendored_supplement, }; use self::gc::gc_into; -use self::hosted::boxed_run_redirect; pub(crate) use self::hosted::boxed_run_redirect_selected; +use self::hosted::run_redirect; use self::vendor_flow::{ boxed_vendor_interactive_path, boxed_vendor_json_path, partition_skipped_selected, }; @@ -2173,15 +2173,10 @@ async fn run_scan( report: &mut report::Run, ) -> i32 { let mut unchecked = 0usize; - let code = Box::pin(scan_project( - args, - invocation, - explicit, - ahead, - &mut unchecked, - report, - )) - .await; + // Built by `boxed`, not by a `Box::pin` here, which would make the + // future of a whole scan a temporary of this frame first. + let code = + boxed(|| scan_project(args, invocation, explicit, ahead, &mut unchecked, report)).await; if unchecked > 0 && code == 0 { 1 } else { @@ -3676,21 +3671,26 @@ async fn scan_project( if let Some(sweep) = withdrawn_sweep { sweep.record_into(&mut env); } - return boxed_run_redirect( - &args, - &api_client, - &all_packages_with_patches, - can_access_paid_patches, - &policy, - env, - npm_crawl.as_ref(), - &recorded, - lookup_incomplete, - &mut stage, - prior_discovery, - sweep_references, - prune, - ) + // The hosted flow's future embeds the whole hosted engine: + // awaited in line it lay in the poll frame of a scan in every + // mode. `boxed` leaves this frame one pointer. + return boxed(|| { + run_redirect( + &args, + &api_client, + &all_packages_with_patches, + can_access_paid_patches, + &policy, + env, + npm_crawl.as_ref(), + &recorded, + lookup_incomplete, + &mut stage, + prior_discovery, + sweep_references, + prune, + ) + }) .await; } diff --git a/crates/socket-patch-cli/src/ecosystem_dispatch.rs b/crates/socket-patch-cli/src/ecosystem_dispatch.rs index 7d7f510d4..e5c28f452 100644 --- a/crates/socket-patch-cli/src/ecosystem_dispatch.rs +++ b/crates/socket-patch-cli/src/ecosystem_dispatch.rs @@ -1536,7 +1536,15 @@ impl JvmScope { /// Box the future `make` returns, constructing it inside this (non-async) /// frame so the caller's poll frame only ever holds the pointer. -fn boxed<'a, T, F, Fut>(make: F) -> std::pin::Pin + 'a>> +/// +/// `Box::pin(big(..))` written in an async fn builds the future as a +/// 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. +#[inline(never)] +pub(crate) fn boxed<'a, T, F, Fut>( + make: F, +) -> std::pin::Pin + 'a>> where F: FnOnce() -> Fut, Fut: std::future::Future + 'a,