nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Server doesn't properly restart

Open darklight9811 opened this issue 1 year ago • 5 comments

Environment

nitropack: 0.4.12 node: 14.20.0

Reproduction

Use something that persists through reloads (such as databases), in my case I used prisma, if create a server that has prisma, and reload (make a change in the code), the server will reload but won't answer any more requests.

Describe the bug

The server reloads (the changes made) but requests stop working. From time to time the server crashes.

Additional context

No response

Logs

No response

darklight9811 avatar Jul 29 '22 03:07 darklight9811

Would you provide a reproduction? 🙏

danielroe avatar Jul 29 '22 06:07 danielroe

@danielroe hi, sorry for the lack of response. I'm really struggling with time right now so I can't provide a proper reproduction. It seems to trigger at random.

Those are my dependencies:

{
	"dependencies": {
		"@prisma/client": "^4.1.1",
		"nitropack": "^0.4.12",
		"ts-node": "^10.9.1"
	},
	"devDependencies": {
		"@types/node": "^18.6.2",
		"@typescript-eslint/eslint-plugin": "^5.12.1",
		"@typescript-eslint/parser": "^5.12.1",
		"eslint": "^8.20.0",
		"prisma": "4.0.0",
		"typescript": "^4.7.4"
	}
}

This is how I'm adding prisma:

import { PrismaClient } from "@prisma/client"

declare global {
	// eslint-disable-next-line no-var
	var prisma: PrismaClient | undefined
}

export const db = global.prisma || new PrismaClient()

if (process.env.NODE_ENV !== "production") global.prisma = db

I'm going to try to find some time to properly do a reproduction. Because its not an immediate bug too

darklight9811 avatar Aug 05 '22 03:08 darklight9811

From time to time it throws this error:

error Command failed with exit code 3221225477.

darklight9811 avatar Aug 05 '22 04:08 darklight9811

EDIT : nevermind, no idea why it worked for a day, but now it crashes again... Ended up installing WSL : finally, I can edit files without having to manually restart the server everytime.

Can confirm the bug. If prisma is connected to DB while server reloads, in hangs without error and stop responding to requests. It has already been reported but not fixed : https://github.com/unjs/nitro/issues/257#issue-1248275871 (repro in linked post).

Not sure why, but if I create a global instance of prismaClient, it works (global vars are not reloaded by HMR if I correctly understand). I used a piece of code found in Prisma doc :

// usePrisma.ts
// NOT an actual composable, just a piece of code to import
import { PrismaClient } from '@prisma/client'

declare global {
  // eslint-disable-next-line no-var
  var db: PrismaClient | undefined
}

// export 'prisma' (PrismaClient) from global.db if already exists or create new prismaClient if not
export const prisma =
  global.db ||
  new PrismaClient({
    log: ['query'],
  })

// if new prismaClient, add it to global IN DEV ENV ONLY
if (process.env.NODE_ENV !== 'production') global.db = prisma

Then use prismaClient as usual :

import { prisma } from "~~/composables/usePrisma";

export default defineEventHandler(async (event) => {
  try {
    const users = await prisma.user.findMany();
    return { users }
  } catch (e) {
    // Error management
  }
});

Since then, no ever-pending request on silent error. Still get a few error Command failed with exit code 3221225477. (like when adding package while yarn dev is running). Note that this code should probably NOT be in /composables/ but I had no idea where to put it ^^.

Nuxt 3.0.0.RC8 NoteJS x64 16.17.0 LTS

Lucky-Strike1695 avatar Aug 22 '22 06:08 Lucky-Strike1695

having the exact same issue, can confirm that WSL works as a workaround in the meantime

chuahziyang avatar Aug 29 '22 04:08 chuahziyang

For me, this occurs only on windows platform, never encountered this issue on linux. Maybe that is why WSL also works fine in this case.

 ELIFECYCLE  Command failed with exit code 3221225477.

mubaidr avatar Dec 06 '22 05:12 mubaidr