Skip to content

Yield to the event loop during provider lifecycle sweeps - #4651

Merged
SawyerHood merged 2 commits into
mainfrom
perf/yield-provider-lifecycle-sweep
Oct 1, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
perf/yield-provider-lifecycle-sweep

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The periodic provider lifecycle sweep started a sweep for every provider environment in one synchronous pass, including rows that are permanently parked on a provider-owner mismatch. On installs with many provider environments that pass held the event loop.

What changed

Split out of #4532 (the environment-engine.ts part of commit 9537a77, re-applied on current main). Part 2 of 6.

  • apps/server/src/services/environments/environment-engine.ts: sweepProviderLifecycles yields to the event loop after every 25 scheduled sweeps and between providers.
  • packages/db/src/data/environments.ts: listProviderLifecycleEnvironments excludes rows already marked failed with the owner-mismatch message, with no owner thread and still owned by a different plugin. Sweeping them was a no-op that wrote nothing. They come back as soon as the owning plugin provides them again. (Speed up thread switching, resume, realtime, and composer #4532 filtered these after loading; moved into the query after review.)

Not included from #4532: the event-loop stall log threshold change (500 ms → 250 ms). No wire change.

How you verified

  • pnpm exec turbo run typecheck lint --filter=@bb/server --filter=@bb/db: pass.
  • vitest run test/query-plans.test.ts in packages/db: 37/37 pass.
  • vitest run test/services/environments/provider-orchestration.test.ts: 64/64 pass, including the new test that blocked rows are left untouched, a retryable row is still removed, and a blocked row is picked up again once its plugin provides it.
  • Not re-measured on this base. Speed up thread switching, resume, realtime, and composer #4532 reported worst server stall 361 ms → 94 ms for its combined server-stall work, of which this is one part.

🤖 Generated with Claude Code

AGENT GENERATED

The periodic provider lifecycle sweep started every environment's sweep in one synchronous pass, which showed up as the longest server stall on installs with many provider environments. Yield after every 25 scheduled sweeps and between providers, and skip rows already marked as blocked on a provider-owner mismatch, since sweeping them again is a no-op until the owning plugin provides them again.

Co-authored-by: hemaaanth <[email protected]>
Co-Authored-By: Claude Code <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T21:54:26.837058Z 7591ad1 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7591ad1700

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

deps.db,
record.provider.id,
)) {
if (isBlockedOnProviderOwnerMismatch(row, record)) continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Filter blocked lifecycle rows in SQL

When one provider has a large backlog of owner-mismatch rows, listProviderLifecycleEnvironments(...).all() still synchronously materializes every row, and this continue filters them in JavaScript before the interval-yield check, so an all-blocked backlog performs no intermediate yield and can still monopolize the event loop—the exact workload this change targets. Pass the current plugin owner into the query and exclude these permanently blocked rows with a targeted predicate, while allowing them to reappear when the matching plugin returns.

AGENTS.md reference: AGENTS.md:L31-L35

Useful? React with 👍 / 👎.

Filter rows blocked on a provider-owner mismatch in the query instead of after loading them, so a backlog of blocked rows is never materialized and the yield interval counts only rows that are swept.

Co-Authored-By: Claude Code <[email protected]>
@SawyerHood
SawyerHood merged commit 9547850 into main Oct 1, 2026
21 checks passed
@SawyerHood
SawyerHood deleted the perf/yield-provider-lifecycle-sweep branch October 1, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant