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: register middleware once for module workers #3585

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Jul 11, 2023

Fixes #2386

What this PR solves / how to test:

Previously, the middleware loader for module workers would register all middleware on each request. These registrations would be in addition to previous requests' registrations, meaning the middleware chain grew linearly with each request. This eventually lead to a stack overflow, as the middleware dispatcher is recursive. It also significantly slowed down requests over time. Editing code and reloading the dev server would reset the middleware chain.

This change ensures middleware is only registered on the first request.

To test this, follow the reproduction instructions here: https://github.com/BillBrower-Shopify/cf-workerd-crash-minimal
With this change stack overflows and slowdowns shouldn't be observed.

Associated docs issue(s)/PR(s):

  • N/A

Author has included the following, where applicable:

Reviewer is to perform the following, as applicable:

  • Checked for inclusion of relevant tests
  • Checked for inclusion of a relevant changeset
  • Checked for creation of associated docs updates
  • Manually pulled down the changes and spot-tested

Previously, the middleware loader for module workers would register
all middleware on each request. These registrations would be in
addition to previous requests' registrations, meaning the middleware
chain grew linearly with each request. This eventually lead to a
stack overflow, as the middleware dispatcher is recursive. It also
significantly slowed down requests over time. Editing code and
reloading the dev server would reset the middleware chain.

This change ensures middleware is only registered on the first
request.

Closes #2386
@mrbbot mrbbot requested review from a team as code owners July 11, 2023 13:20
@changeset-bot
Copy link

changeset-bot bot commented Jul 11, 2023

🦋 Changeset detected

Latest commit: 21bf848

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

@github-actions
Copy link
Contributor

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/5520488221/npm-package-wrangler-3585

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

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

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/5520488221/npm-package-wrangler-3585 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/5520488221/npm-package-cloudflare-pages-shared-3585

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

@mrbbot mrbbot merged commit e33bb44 into main Jul 11, 2023
@mrbbot mrbbot deleted the bcoll/modules-middleware-register-once branch July 11, 2023 14:23
@github-actions github-actions bot mentioned this pull request Jul 11, 2023
lrapoport-cf pushed a commit that referenced this pull request Aug 11, 2023
Previously, the middleware loader for module workers would register
all middleware on each request. These registrations would be in
addition to previous requests' registrations, meaning the middleware
chain grew linearly with each request. This eventually lead to a
stack overflow, as the middleware dispatcher is recursive. It also
significantly slowed down requests over time. Editing code and
reloading the dev server would reset the middleware chain.

This change ensures middleware is only registered on the first
request.

Closes #2386
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: wrangler dev --experimental-local slows down over time and leaks memory
2 participants