Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass ctx.props to default handlers #3465

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tewaro
Copy link
Contributor

@tewaro tewaro commented Feb 5, 2025

No description provided.

@tewaro tewaro requested review from a team as code owners February 5, 2025 06:55
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch from 7bede3c to 06dc9cc Compare February 5, 2025 18:33
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@tewaro tewaro requested a review from kentonv February 5, 2025 18:37
@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch 2 times, most recently from f874f4b to c26f53f Compare February 6, 2025 08:02
Copy link

github-actions bot commented Feb 6, 2025

The generated output of @cloudflare/workers-types matches the snapshot in types/generated-snapshot 🎉

@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch from c26f53f to f607315 Compare February 6, 2025 14:51
@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch from f607315 to 7c05d79 Compare February 6, 2025 14:58
@jasnell
Copy link
Member

jasnell commented Feb 6, 2025

What exactly is the intent here? The PR description needs some explanation and detail. If the idea is to give every invocation it's own unique context, then rather than cloning the entire thing the way this PR does, could it make sense to simply wrap the context in a JS Proxy object for every request and either CoW the underlying context or just intercept mutations so that the original remains unmodified? The per-request props can then be injected via the proxy. Wrapping in a proxy could likely reduce much of the complexity here.

src/workerd/api/global-scope.h Outdated Show resolved Hide resolved
src/workerd/api/global-scope.h Outdated Show resolved Hide resolved
src/workerd/api/global-scope.h Outdated Show resolved Hide resolved
src/workerd/api/global-scope.h Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
src/workerd/io/worker.c++ Outdated Show resolved Hide resolved
@kentonv
Copy link
Member

kentonv commented Feb 6, 2025

What exactly is the intent here? The PR description needs some explanation and detail.

The goal of the change is to fix a known bug in which ctx.props is not available for non-class-based entrypoints because such entrypoints have always reused the same ctx object for all invocations. There's a comment on line 1674 describing the issue:

                        // TODO(cleanup): Unfortunately, for non-class-based handlers, we have
                        //   always created only a single `ctx` object and reused it for all
                        //   requests. This is weird and obviously wrong but changing it probably
                        //   requires a compat flag. Until then, connection properties will not be
                        //   available for non-class handlers.

BTW @tewaro we should update that comment to something like:

"Historically, non-class-based handlers reused the same ctx object for all requests. This was an accident, but some Workers depend on it. Newer worker with the unique_ctx_per_invocation will allocate a new ctx for every request."

Wrapping in a proxy could likely reduce much of the complexity here.

That sounds way more complicated to me. This change as-is is really not very complex at all. I don't see what benefit a Proxy would provide here.

@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch 2 times, most recently from f87e594 to d3c5a1a Compare February 6, 2025 22:25
@tewaro tewaro requested a review from kentonv February 6, 2025 22:25
@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch from d3c5a1a to f2d764c Compare February 6, 2025 22:26
@tewaro tewaro requested a review from danlapid February 6, 2025 22:27
@tewaro tewaro force-pushed the tewaro/ctx-props-on-default branch from f2d764c to b53030f Compare February 7, 2025 02:46
@tewaro
Copy link
Contributor Author

tewaro commented Feb 7, 2025

@kentonv how do I force the feature flag on all tests? do I need to modify each one?

@tewaro tewaro requested a review from kentonv February 7, 2025 04:18
@kentonv
Copy link
Member

kentonv commented Feb 7, 2025

@tewaro I'm just proposing that you temporarily change your code locally to enable the flag by default and make sure the tests actually pass.

@tewaro
Copy link
Contributor Author

tewaro commented Feb 7, 2025

@tewaro I'm just proposing that you temporarily change your code locally to enable the flag by default and make sure the tests actually pass.

I've already done a test where I bump supported-compatibility-date.txt locally, is there anything more I need to do?

@kentonv
Copy link
Member

kentonv commented Feb 7, 2025

Yes, please add a unit test of some sort, where the test directly enables the flag and verifies that ctx.props is then present when expected. You can perhaps modify or duplicate one of the existing tests for ctx.props in server-test.c++.

@kentonv
Copy link
Member

kentonv commented Feb 7, 2025

I've already done a test where I bump supported-compatibility-date.txt locally, is there anything more I need to do?

Oh, this doesn't actually accomplish what I asked for earlier.

To be clear, you need to do two things:

  1. Include a unit test in the PR itself, which enables the flag and verifies it does what you wanted it to do. In general, you should always do this, in every PR, before you post the PR for review.
  2. Unique to this PR: I recommend forcing the flag to be on and running all the existing tests. We would not expect this flag to break any of our tests, so this would help shake out any weird edge cases that we might have missed. Bumping supported-compatibility-date.txt does not cause the flag to be turned on. What I'm suggesting is you temporarily set the flag default value to true, or even just temporarily remove the if statement so that the true branch always runs.

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.

4 participants