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

In pages functions avoid cloning the request when not necessary #4916

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dario-piotrowicz
Copy link
Member

@dario-piotrowicz dario-piotrowicz commented Feb 3, 2024

Partially fixes #3259 (the full fix can only be applied as a breaking change so it'll have to wait for a wrangler major release unfortunately)

Supersedes #4861


Example for the changes: https://github.com/dario-piotrowicz/cf-pages-functions-request-clone-repro


TODO:

  • understand why this only occurred in POST requests
  • investigate alternative suggested in comment
  • add tests for plugins

Copy link

changeset-bot bot commented Feb 3, 2024

🦋 Changeset detected

Latest commit: b489e23

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dario-piotrowicz dario-piotrowicz changed the title in pages-templates clone request only when there is more than one handler In pages-templates clone request only when there is more than one handler Feb 3, 2024
@dario-piotrowicz dario-piotrowicz changed the title In pages-templates clone request only when there is more than one handler In pages functions avoid cloning the request when not necessary Feb 3, 2024
Copy link
Contributor

github-actions bot commented Feb 3, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7777149625/npm-package-wrangler-4916

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/4916/npm-package-wrangler-4916

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7777149625/npm-package-wrangler-4916 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7777149625/npm-package-create-cloudflare-4916 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7777149625/npm-package-miniflare-4916
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7777149625/npm-package-cloudflare-pages-shared-4916

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.26.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240129.0
workerd 1.20240129.0 1.20240129.0
workerd --version 1.20240129.0 2024-01-29

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@dario-piotrowicz dario-piotrowicz force-pushed the pages-functions-request-clone-fix branch 3 times, most recently from 32e5d22 to e6ee10e Compare February 4, 2024 12:11
Copy link

codecov bot commented Feb 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (fcbde34) 70.62% compared to head (b489e23) 70.69%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4916      +/-   ##
==========================================
+ Coverage   70.62%   70.69%   +0.06%     
==========================================
  Files         292      292              
  Lines       15187    15187              
  Branches     3865     3865              
==========================================
+ Hits        10726    10736      +10     
+ Misses       4461     4451      -10     

see 7 files with indirect coverage changes

@dario-piotrowicz dario-piotrowicz force-pushed the pages-functions-request-clone-fix branch 6 times, most recently from 99ffb58 to d9c58ad Compare February 4, 2024 22:17
@Skye-31
Copy link
Contributor

Skye-31 commented Feb 4, 2024

I wonder if we could use Request.bodyUsed alongside Request.body.cancel() to remove the warning in all cases, by cancelling the body streams of any requests where its not been read?

@russelldavis
Copy link

russelldavis commented Feb 14, 2024

@Skye-31 that's an interesting idea. I think it would make the warnings go away (because the clones would be cleaned up right before the warning logic sees them), but unfortunately I think the underlying issue that is being warned about would remain. You'd still be calling request.clone() before each call to next(), and you'd only be able to safely call request.body.cancel() after the call to next() returns, which means the entire request would still get processed while clones of the stream exist, causing the problems mentioned in the warning message.

@admah admah added this to the Wrangler v4 milestone Mar 8, 2024
@GregBrimble
Copy link
Member

There's two things in one here:

  1. We can minimize the number of clones in a Pages Functions Worker. That can happen today. There shouldn't be any impact to users. We simply check the number of handlers. If < 2, then we don't need to clone. At a quick skim, that seems to be what the code of this PR does, and like I say, that could ship today because it's not a breaking change unless I'm misunderstanding something.

  2. Foregoing automatic cloneing entirely. We can't do this, even with a major version bump, because Pages Functions is versioned independently (it currently is not versioned at all). This will need to wait until we figure out what we're doing with Pages Functions ownership long-term.

@GregBrimble GregBrimble removed this from the Wrangler v4 milestone Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Your worker called response.clone(), but did not read the body of both clones
5 participants