Repository navigation
fix: getPageContext() reads the pageContext Vike sets for a hook only while that hook runs synchronously - #3759
Open
nitedani-agent wants to merge 5 commits into
Open
nitedani-agent wants to merge 5 commits into
nitedani-agent wants to merge 5 commits into
Conversation
… an `await` Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…called next Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
nitedani-agent
marked this pull request as ready for review
November 20, 2026 19:18
Contributor
Author
|
Ready for review at |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On the server,
getPageContext()can return thepageContextof another HTTP request. Vike keeps thepageContextof the hook it calls in one variable for the whole process and clears it in a microtask, so code of another request that runs before that microtask reads it. In my runs it happened when two requests waited for the same promise (a memoizedfetch(), a shared cache) and one of them read after anawait. You asked for this PR in telefunc/telefunc#520 (comment).vike previewmain(e9c6bca92)3c7d65a07)getPageContext()in+data,+guardand+onBeforeRender, 180 of the reads with{ asyncHook: true }: 4160 requests, alone and in overlapping pairs, 2990 readspageContextin 70 reads, 10 of them with that optionuseConfig()of vike-react 0.6.32 after anawaitin+data: 820 overlapping pairs<title>another request setgetPageContext()afterawaits in+onAfterRenderHtml, a hook vike-react calls: 1220 overlapping pairspageContextin 20 pairsVike now sets the variable only while the hook runs synchronously, and puts the previous value back when the hook returns. On the server,
providePageContext(pageContext)writes to the request's AsyncLocalStorage store when there is one, so the published vike-react and vike-vue, which call it before the hooks they call themselves, need no change. In+data,+guardand+onBeforeRender, a read before the firstawaitreturns the request's ownpageContext, as onmain.Without AsyncLocalStorage,
providePageContext(pageContext)still writes the variable, so another request can still read thepageContextan extension provided for its hook there. I'd close that withprovidePageContext(pageContext, fn)in a PR of its own after this one, as in telefunc/telefunc#520. Is that the right direction?getPageContext()right afterprovidePageContext(), in the synchronous part of a hook Vike calls with apageContext, such as+data(not in a+onHookCallwrapper), returns that hook'spageContext(onmain: the given value,nullincluded)pageContextits own callback received+onHookCall,getPageContext()right afterhook.call()returnsnull(the hook'spageContextonmain)+onHookCallgetspageContextas its second argumentWhat you should see
1. A line the first run of the table printed on
main, for two requests sent together that wait for one promise. The same run on this PR printed none. The scripts, and how to run them, are in this gist.Approach
Eight candidates, rated on how sure I am each is the right fix (approach): Vike sets the variable only for the synchronous part of the hook it calls,
providePageContext(pageContext)writes to the request's store when there is one and to the variable when there is none, andprovidePageContext(pageContext, fn)comes in a second pull request (8/10, chosen); the same in one pull request, with nothing written where there is no store (6/10:useConfig()in+onAfterRenderHtmlwould readnullthere, where it works onmain); the scoped set plus the callback form, the extensions switched later (5/10: a Vike update alone fixes nothing for the hooks extensions call); the scoped set alone (3/10: those hooks still leak); the store slot without a callback form (3/10); the call without a callback as a usage error, as in Telefunc (2/10: it breaks every published vike-react and vike-vue app with a callable cumulative hook);getPageContext()reading the store first (1/10); changing nothing (0/10).Loop A
Three passes that count only what they reproduce (first, second, third), none of which reproduced a bug.
685933cfb: request A runs a hook and request B only reads, across six kinds of hook call and five ways of waiting, on Node 26.13.1 and Node 22.23.4.mainreturned A'spageContextto B in 100 reads of each row, this PR in 0 (the one row left at 100 is a hook an extension calls where there is no store, which the description names). 30 edge cases passed (nested hooks, a hook that throws synchronously), and so did 5 cases with a second copy of Vike in the process. The three HTTP scripts, a streamed component, workerd with and withoutnodejs_compat(single requests only), andtest-e2e test/vike-reactandtest/vike-solidagreed.11a16e59d, fails when the queued clear or the fall-back without a store is removed. The third, that the value put back after a nested hook is the outer hook'spageContextand notnull, is covered by the pass's probe and by no test.11a16e59dand on3c7d65a07: the three later commits change one comment and the spec. The builtpackages/vike/distof3c7d65a07and of685933cfbdiffer in that one comment line.Loop B
Two rounds of review and of a pass over the shape of every file and function (review 1, shape 1, review 2, shape 2, what became of each finding).
685933cfb: the two tests only failed when the cross-request read came back, so the third test was added (11a16e59d) and a comment reworded (439f05b8c). Declined as nits: equal alternatives, names, and 10 to 50 ns per call.11a16e59d: the third test's title claimed more than it asserts, so it was renamed (3c7d65a07). The pass rated the PR 8/10.Fresh read
One cold read of
3c7d65a07as the maintainer who merges (report): no bug, nothing worth changing, "merge as is: yes". It reverted the fix in two parts and saw a test fail for each (execHook.tsat the base: the first test; the store slot at the base: the second), ran four more mutations (each turned a test red), and sent 500 overlapping pairs of requests through its own probe:mainreturned the other request'spageContext500 times, this PR 0 times with a store. It did not check overlapping requests on workerd withnodejs_compat, nor the vike-vue and vike-solid e2e lanes (those two are under Gates).Gates
On
3c7d65a07, on my machine (log):CI on
3c7d65a07: 19 checks, all successful.🤖 Generated with Claude Code