Conversation
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.
|
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 |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #39596
Type of change
What does this PR do?
lazy()setloaded = truebefore callingfn(). If the initializer throws,loadedis already true butvaluewas never assigned, so every later call takes theif (loaded)fast path and returnsundefinedforever. The real error is swallowed and a transient failure becomes permanent.Moving the assignment after
fn()returns fixes it: a throw now leavesloadedfalse, so the next call retries. Success path is unchanged - still onefn()call, still caches anundefinedresult.How did you verify your code works?
Added
packages/core/test/util/lazy.test.ts(4 tests: caching, retry after throw, repeated failure, cachedundefined).Checked the tests actually catch the bug - reverted the src change and the two retry tests fail (
Received function did not throw, receivedundefined); with the fix 4/4 pass.bun typecheckclean inpackages/core;bun test test/util/31/31 pass.Screenshots / recordings
Not a UI change.
Checklist