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: close vite dev server before creating new one #4374

Merged
merged 3 commits into from
Aug 3, 2021

Conversation

tjk
Copy link
Contributor

@tjk tjk commented Jul 23, 2021

Description

Without this change, when touching vite config (for example), vite logs [vite] server restarted. and then client reconnect loops because it can never reestablish connection to websocket port (since it was not bound correctly).

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92
Copy link
Member

Build is talking about that you need to remove import { prepareError } from './middlewares/error' in L10

@Shinigami92 Shinigami92 marked this pull request as draft July 23, 2021 21:31
@Shinigami92 Shinigami92 added the p3-minor-bug An edge case that only affects very specific usage (priority) label Jul 24, 2021
Shinigami92
Shinigami92 previously approved these changes Jul 24, 2021
Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

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

question: does this affect #3182 somehow?

patak-dev
patak-dev previously approved these changes Jul 24, 2021
@patak-dev
Copy link
Member

@tjk please remove the draft status when you are ready to merge

@tjk
Copy link
Contributor Author

tjk commented Jul 24, 2021

question: does this affect #3182 somehow?

Will test different combinations in a minimal repro and then remove draft status after an update. Thanks!

@tjk
Copy link
Contributor Author

tjk commented Jul 24, 2021

@Shinigami92 is there a good reason that EADDRINUSE is squelched here: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/ws.ts#L79

If you already have a vite dev server running with default config:

$ yarn workspace test-ssr-vue dev
yarn workspace v1.22.5
yarn run v1.22.5
$ node server
http://localhost:3000
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[@vue/compiler-sfc] <script setup> is still an experimental proposal.
Follow its status at https://github.com/vuejs/rfcs/pull/227.

[@vue/compiler-sfc] When using experimental features,
it is recommended to pin your vue dependencies to exact versions to avoid breakage.

[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.

So getting the following is helpful:

$ yarn workspace test-ssr-vue dev
yarn workspace v1.22.5
yarn run v1.22.5
$ node server
WebSocket server error:
Error: listen EADDRINUSE: address already in use :::24678
    at Server.setupListenHandle [as _listen2] (net.js:1313:16)
    at listenInCluster (net.js:1361:12)
    at Server.listen (net.js:1447:7)
    at new WebSocketServer (/home/tjk/src/github.com/tjk/vite/packages/vite/dist/node/chunks/dep-e21de3b6.js:61872:20)
    at createWebSocketServer (/home/tjk/src/github.com/tjk/vite/packages/vite/dist/node/chunks/dep-e21de3b6.js:62281:15)
    at Object.createServer (/home/tjk/src/github.com/tjk/vite/packages/vite/dist/node/chunks/dep-e21de3b6.js:73607:16)
    at async createServer (/home/tjk/src/github.com/tjk/vite/packages/playground/ssr-vue/server.js:30:12)
http://localhost:3000
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
[Vue Router warn]: No match found for location with path "/favicon.ico"
[Vue warn]: <Suspense> slots expect a single root node.
...

@Shinigami92
Copy link
Member

@Shinigami92 is there a good reason that EADDRINUSE is squelched here: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/ws.ts#L79

I personally don't know 🤷 You need to ask someone else

@tjk
Copy link
Contributor Author

tjk commented Jul 24, 2021

@Shinigami92 is there a good reason that EADDRINUSE is squelched here: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/ws.ts#L79

I personally don't know 🤷 You need to ask someone else

Ack. That line is 8 months old. @yyx990803 do you remember a good reason for this?

EDIT: see my comment above... it would be nice for the behavior from the client to also be better but I don't have any obvious ideas yet (in terms of not just spin refreshing if the HMR port is down)

EDIT: (to be clear, this PR currently only addresses re-binding to the same HMR ws port correctly on dev server restart at the moment)

@tjk
Copy link
Contributor Author

tjk commented Jul 24, 2021

question: does this affect #3182 somehow?

I left comments on that other PR (#3182 (review)).

As it stands, this issue happens in middlewareMode, whereas #3182 only affects root/serve CLI command. This change would affect all restartServer usage obviously... there may be a "better" solution that tries to get at the crux of creating web socket server but easiest fix seems to just ensure it is closed before restarting.

@patak-dev
Copy link
Member

I agree, should we mark this one as ready to merge and out of draft then @tjk?

@tjk tjk marked this pull request as ready for review July 24, 2021 17:06
@HomyeeKing
Copy link
Contributor

question: if a Socket disconnect, does the upstream websocket disconnect with it ?

@tjk
Copy link
Contributor Author

tjk commented Jul 24, 2021

question: if a Socket disconnect, does the upstream websocket disconnect with it ?

I imagine so. Is there a particular scenario you are worried would not work? I'm happy to test cases that might be borked, just let me know please.

@tjk
Copy link
Contributor Author

tjk commented Jul 25, 2021 via email

@tjk
Copy link
Contributor Author

tjk commented Jul 26, 2021

@patak-js so during the time between server close and new server is restarted, the client will continuously reconnect at 1 second intervals. This is better than previous behavior and likely shouldn't really trigger a timeout ping... if we want to improve this we could try to signal to client we are restarting so it handles that behavior better or do the socket "handoff" better if possible? Let me know what you think.

(moved to review thread)

@patak-dev
Copy link
Member

For reference there is a new issue and PR #4460 related to the way the server is restarting. @tjk did you thought about splitting server.close() into a server.stop() that would return { httpServer, webSocket } and allow these to be used in the next createServer? Not having to recreate this looks like a solution to these issues to me.

I would like to merge this PR and we can continue to explore how to improve here. But I see that we don't have an issue related to it. @tjk would it be possible for you to create a bug report with a minimal reproduction so we can better track this change? Thanks.

@patak-dev patak-dev dismissed stale reviews from Shinigami92 and themself via fdc5d80 August 3, 2021 19:27
@patak-dev patak-dev linked an issue Aug 3, 2021 that may be closed by this pull request
6 tasks
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Tested #4492 and this PR properly fix the issue
Thanks for creating the issue @tjk

@patak-dev patak-dev merged commit 9e4572e into vitejs:main Aug 3, 2021
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Doesn't restart server correctly when using https server
4 participants