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

Abort signalling #732

Merged
merged 2 commits into from
Apr 4, 2022
Merged

Abort signalling #732

merged 2 commits into from
Apr 4, 2022

Conversation

JacobMGEvans
Copy link
Contributor

@JacobMGEvans JacobMGEvans commented Mar 30, 2022

fix: abort async operations in the Remote component to avoid unwanted side-effects
When the Remote component is unmounted, we now signal outstanding fetch() requests, and
waitForPortToBeAvailable() tasks to cancel them. This prevents unexpected requests from appearing
after the component has been unmounted, and also allows the process to exit cleanly without a delay.

fixes #375

@changeset-bot
Copy link

changeset-bot bot commented Mar 30, 2022

🦋 Changeset detected

Latest commit: 89edf75

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

github-actions bot commented Mar 30, 2022

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

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2091243209/npm-package-wrangler-732

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

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/732/npm-package-wrangler-732

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/2091243209/npm-package-wrangler-732 dev path/to/script.js

packages/wrangler/src/proxy.ts Outdated Show resolved Hide resolved
packages/wrangler/src/proxy.ts Outdated Show resolved Hide resolved
packages/wrangler/src/dev/remote.tsx Outdated Show resolved Hide resolved
packages/wrangler/src/create-worker-preview.ts Outdated Show resolved Hide resolved
packages/wrangler/src/create-worker-preview.ts Outdated Show resolved Hide resolved
@@ -421,6 +427,9 @@ export async function waitForPortToBeAvailable(
}

function checkPort() {
// Will clear the interval and timeout if signal is called,
// using resolve prevents Error messages on the console.
options.abortCtrl?.aborted && doResolve();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's wrap this in a conditional

Suggested change
options.abortCtrl?.aborted && doResolve();
if(options.abortCtrl?.aborted){
doResolve();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably more clear, will do.

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @threepointone's suggestions; but otherwise LGTM.

Comment on lines 5 to 7
fix: abort some of the fetches when we unmount the `Remote` component.
Adding abort to the `waitForPortToBeAvailable` as an option and in the `useEffect` the abort signal is called in the return which allows for graceful closing with no hanging
of the `waitForPortToBeAvailable`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fix: abort some of the fetches when we unmount the `Remote` component.
Adding abort to the `waitForPortToBeAvailable` as an option and in the `useEffect` the abort signal is called in the return which allows for graceful closing with no hanging
of the `waitForPortToBeAvailable`
fix: abort async operations in the `Remote` component to avoid unwanted side-effects
When the `Remote` component is unmounted, we now signal outstanding `fetch()` requests, and
`waitForPortToBeAvailable()` tasks to cancel them. This prevents unexpected requests from appearing
after the component has been unmounted, and also allows the process to exit cleanly without a delay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better wording, thank you!!

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I wonder if the waitForPortToBeAvailable() abort should be more along the lines of:

export async function waitForPortToBeAvailable(
  port: number,
  options: { retryPeriod: number; timeout: number; abortCtrl?: AbortSignal }
): Promise<void> {
  return new Promise((resolve, reject) => {
    abortSignal.onabort(() => {
      const abortError = new Error("");
      (error as {code: string}).code = "ABORT_ERR";
      reject(abortError);
    });

): Promise<void> {
return new Promise((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(options.abortSignal as any).addEventListener("abort", () => {
const error = new Error("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will log "Failed to start server:" which will be confusing to anyone who reads it. Could we add a code or something this error, and read it on line 307 and not log anything if it's an aborted attempt? Like @petebacondarwin described in #732 (review)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const error = new Error("");
const abortError = new Error("waitForPortToBeAvailable() aborted");
(error as {code: string}).code = "ABORT_ERR";

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just that one change - otherwise LGTM!

): Promise<void> {
return new Promise((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(options.abortSignal as any).addEventListener("abort", () => {
const error = new Error("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const error = new Error("");
const abortError = new Error("waitForPortToBeAvailable() aborted");
(error as {code: string}).code = "ABORT_ERR";

…e `useEffect` the abort signal is called in the return which allows for graceful closing with no hanging

of the `waitForPortToBeAvailable`

fixes #375
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.

Error when running ctrl-c in dev mode
3 participants