Skip to content

fix(core): retry lazy initializer after it throws - #39597

Open
LHMQ878 wants to merge 1 commit into
anomalyco:devfrom
LHMQ878:lazy-retry
Open

fix(core): retry lazy initializer after it throws#39597
LHMQ878 wants to merge 1 commit into
anomalyco:devfrom
LHMQ878:lazy-retry

Conversation

@LHMQ878

@LHMQ878 LHMQ878 commented Jul 30, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #39596

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

lazy() set loaded = true before calling fn(). If the initializer throws, loaded is already true but value was never assigned, so every later call takes the if (loaded) fast path and returns undefined forever. The real error is swallowed and a transient failure becomes permanent.

Moving the assignment after fn() returns fixes it: a throw now leaves loaded false, so the next call retries. Success path is unchanged - still one fn() call, still caches an undefined result.

How did you verify your code works?

Added packages/core/test/util/lazy.test.ts (4 tests: caching, retry after throw, repeated failure, cached undefined).

Checked the tests actually catch the bug - reverted the src change and the two retry tests fail (Received function did not throw, received undefined); with the fix 4/4 pass.

bun typecheck clean in packages/core; bun test test/util/ 31/31 pass.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

lazy() marked itself loaded before invoking fn(), so a throwing
initializer left loaded=true with value unset. Every later call then hit
the memoized fast path and returned undefined, hiding the real error and
making a transient failure permanent.

Only memoize after fn() returns.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

The second result (PR #12520) is unrelated—it's about an MCP search tool, not the lazy() utility function.

No duplicate PRs found

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

Bug: lazy() permanently caches undefined when the initializer throws

1 participant