Skip to content

Keep the hosted flow off the stack of every scan - #2234

Open
Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
mainfrom
fix/scan-hosted-flow-off-the-stack
Open

Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
mainfrom
fix/scan-hosted-flow-off-the-stack

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

A scan needs about 80 KB less stack, which gives the Windows test job room that main does not have today.

Why

scan_project awaited the hosted flow in line. The flow's future embeds the whole hosted engine, so its size 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 a test thread has (macOS arm64, debug build). Windows has less room: on another open pull request a change that made one struct of the hosted flow 64 bytes larger, held three times in the flow, overflowed that test's stack in the test (windows-latest, 2) job (STATUS_STACK_OVERFLOW), while main and the other branches passed it. So main is within a few hundred bytes of failing that job, and the next change to the hosted flow, or two that land together, can tip it.

What changes

Three futures on a scan's chain are now built on a frame that is gone before anything is polled, so their caller holds one pointer:

  • the hosted flow, started from scan_project;
  • the future of a whole scan, which run_scan built with a Box::pin(..) written in line (that makes the future a temporary of the caller's frame first);
  • the inner hosted flow, which run_redirect awaited in line where the other two callers already go through boxed_run_redirect_selected.

The first two use the helper the crate already has for this (boxed in ecosystem_dispatch.rs), now visible to the crate and marked #[inline(never)]. Three files, +36 −28 against main. What a scan does is unchanged.

Measured

The smallest stack on which that test passes, with RUST_MIN_STACK (this machine rounds it to 16 KiB), macOS arm64, debug:

passes at overflows at
main 2,002,944 1,998,848
this change 1,933,312 1,916,928

Main's row is from the investigation on the other pull request; the second row was measured on this branch.

Code review

No correctness finding; six about how far the change goes, all read from the code.

  • Applied: the future of a whole scan is boxed off the frame too; the hosted flow's inner flow goes through the existing constructor; the helper is #[inline(never)]; the thirteen-parameter wrapper of the first commit is gone in favour of the crate's one helper; the comments say what the code does.
  • Not applied: a test that fails when the headroom is used up again. A stack size that holds on Linux, macOS and Windows has to be found by runs on each (the frames differ: Windows needs at least 80 KB more than macOS for this test), and one that is wrong fails unrelated pull requests. It is worth a change of its own once a Windows run of this branch shows how much room there is.
  • Not applied: turning the five older boxed_* constructors into calls of the helper, and marking them #[inline(never)]. They are used by other commands and are not what this change is about.

How it was checked

  • rustfmt --check on the three files; cargo clippy --locked --workspace --all-features -- -D warnings.
  • cli (120), in_process_gem_multi_platform (9), in_process_redirect (138), in_process_redirect_pnpm (55), in_process_rollback_hosted (41), in_process_vendor (157): all pass.
  • Not run locally: the whole test suite, and anything on Windows. A Windows run of this branch is the check that matters. Two jobs of that run fail on main for an unrelated reason (seven pnpm unit tests on fixtures that the Windows checkout converts to CRLF, and one pnpm end-to-end test), so the job to read is test (windows-latest, 2).

🤖 Generated with Claude Code

`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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 25, 2026 07:08
@cursor

cursor Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Stack-layout-only change around future boxing; scan and hosted redirect logic is unchanged.

Overview
Reduces debug-scan stack usage by ensuring three large async futures are built on a short-lived non-async frame so callers only hold a Pin<Box<dyn Future>>, instead of embedding those futures in each caller's poll stack.

run_scan now starts the full scan via the shared boxed helper instead of inline Box::pin(scan_project(...)). Hosted scans run_redirect through boxed the same way. One hosted call site switches from run_redirect_selected to the existing boxed_run_redirect_selected wrapper.

The boxed helper in ecosystem_dispatch.rs is pub(crate), marked #[inline(never), and documented so inlining cannot put the big temporary back on the caller's stack. Scan behavior is unchanged; the goal is roughly ~80 KB less stack per scan so Windows tests stay under the 1 MiB thread limit.

Reviewed by Cursor Bugbot for commit 32d5506. Configure here.

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant