vitejs/vite

Vite does not shutdown properly when calling close() on the ViteDevServer

Closed this issue · 5 comments

Describe the bug

I would expect after running this script that the process would end:

import { createServer } from 'vite'
 
const viteServer = await createServer()
await viteServer.close()

This is problematic when using the ViteDevServer with Jest as the process doesn't end cleanly after the server is closed.

This was the commit that introduced the issue: 5999444

Reproduction

This was introduced in Vite 2.4.0-beta.0

https://github.com/schalk-b/vite-not-shutting-down

System Info

System:
    OS: Windows 10 10.0.19041
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 16.06 GB / 31.92 GB
  Binaries:
    Node: 16.4.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD
    npm: 7.19.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (91.0.864.59)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    vite: ^2.4.0-beta.0 => 2.4.0-beta.3

Used Package Manager

npm

Logs

No response

Validations

IanVS commented

I've taken a look, and I guess I don't see where a call to process.exit() should happen when calling close(). If anything, the new behavior seems more correct, and it should be up to the overall parent process to determine when to end the process. In your reproduction, calling process.exit() after closing the server does end the process.

Edit: I'm still taking a look into this, because I'm not super-familiar with node.js conventions and expectations.

IanVS commented

It seems to be the addition of process.stdin.resume() that causes this issue. I think it can be removed without too much trouble. I put it in because a previous attempt had been made to add it.

Yup that resolves it, thank you

I've taken a look, and I guess I don't see where a call to process.exit() should happen when calling close(). If anything, the new behavior seems more correct, and it should be up to the overall parent process to determine when to end the process. In your reproduction, calling process.exit() after closing the server does end the process.

Edit: I'm still taking a look into this, because I'm not super-familiar with node.js conventions and expectations.

It shouldn't be necessary for a node process to call process.exit() for the process to end. If everything has been "cleaned up" and stopped. The process should end by itself.

I found my way here debugging vite-jest not working with vite@2.4. It looks like #4082 is close to merging so hopefully a fix can be published soon. In the meantime, using vite@2.3 or setting CI=true (e.g. CI=true vite-jest) seems to work around this.