Skip to content

Commit

Permalink
fix: close HotChannel on environment close (#18206)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Sep 27, 2024
1 parent 220d6ec commit 2d148e3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
createEnvironmentPluginContainer,
} from './pluginContainer'
import type { RemoteEnvironmentTransport } from './environmentTransport'
import { isWebSocketServer } from './ws'

export interface DevEnvironmentContext {
hot: false | HotChannel
Expand Down Expand Up @@ -206,6 +207,8 @@ export class DevEnvironment extends BaseEnvironment {
await Promise.allSettled([
this.pluginContainer.close(),
this.depsOptimizer?.close(),
// WebSocketServer is independent of HotChannel and should not be closed on environment close
isWebSocketServer in this.hot ? this.hot.close() : Promise.resolve(),
(async () => {
while (this._pendingRequests.size > 0) {
await Promise.allSettled(
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface HotChannel {
/**
* Disconnect all clients, called when server is closed or restarted.
*/
close(): void
close(): Promise<unknown> | void
}
/** @deprecated use `HotChannel` instead */
export type HMRChannel = HotChannel
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export type WebSocketCustomListener<T> = (
client: WebSocketClient,
) => void

export const isWebSocketServer = Symbol('isWebSocketServer')

export interface WebSocketServer extends HotChannel {
[isWebSocketServer]: true
/**
* Listen on port and host
*/
Expand Down Expand Up @@ -88,6 +91,7 @@ export function createWebSocketServer(
): WebSocketServer {
if (config.server.ws === false) {
return {
[isWebSocketServer]: true,
get clients() {
return new Set<WebSocketClient>()
},
Expand Down Expand Up @@ -234,6 +238,7 @@ export function createWebSocketServer(
let bufferedError: ErrorPayload | null = null

return {
[isWebSocketServer]: true,
listen: () => {
wsHttpServer?.listen(port, host)
},
Expand Down

0 comments on commit 2d148e3

Please sign in to comment.