prisma / prisma-client-js

Type-safe database client for TypeScript & Node.js (ORM replacement)
Apache License 2.0
1.47k stars 67 forks source link

PANIC: index out of bounds: the len is 1 but the index is 1 #869

Closed vel1024 closed 4 years ago

vel1024 commented 4 years ago

Hi Prisma Team! My Prisma Client just crashed. This is the report:

Error

error

Enviroment & Set up

Name Version
Node v12.16.2
OS windows 10
Prisma 2.6.0
PostgreSQL 12

Logs

  prisma-client {
  prisma-client   engineConfig: {
  prisma-client     cwd: 'C:\\Users\\khucs\\Downloads\\Linker\\back\\prisma',
  prisma-client     enableDebugLogs: false,
  prisma-client     enableEngineDebugMode: undefined,
  prisma-client     datamodelPath: 'C:\\Users\\khucs\\Downloads\\Linker\\back\\node_modules\\.prisma\\client\\schema.prisma',
  prisma-client     prismaPath: undefined,
  prisma-client     engineEndpoint: undefined,
  prisma-client     generator: {
  prisma-client       name: 'client',
  prisma-client       provider: 'prisma-client-js',
  prisma-client       output: 'C:\\Users\\khucs\\Downloads\\Linker\\back\\node_modules\\@prisma\\client',
  prisma-client       binaryTargets: [Array],
  prisma-client       previewFeatures: [],
  prisma-client       config: {}
  prisma-client     },
  prisma-client     showColors: false,
  prisma-client     logLevel: undefined,
  prisma-client     logQueries: undefined,
  prisma-client     flags: [],
  prisma-client     clientVersion: '2.6.0',
  prisma-client     enableExperimental: [],
  prisma-client     useUds: undefined
  prisma-client   }
  prisma-client }  

Schema.prisma

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "debian-openssl-1.1.x", "darwin"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id                Int             @default(autoincrement()) @id
  avatarUrl         String?         @default("")
  username          String
  email             String          @unique
  password          String
  phoneNum          String?         @default("") @unique
  emailSecret       String?         @default("")
  phoneSecret       String?         @default("")
  bio               String?         @default("")
  participatedGroup Group[]         @relation("Group_Participants")
  createdGroup      Group[]
  messages          Message[]
  directMessages    DirectMessage[]
  createdAt         DateTime?       @default(now())
}

model Group {
  id           Int       @default(autoincrement()) @id
  name         String    @unique
  participants User[]    @relation("Group_Participants")
  messages     Message[]
  creator      User      @relation(fields: [creatorId], references: [id])
  creatorId    Int
  files        String?   @default("")
  createdAt    DateTime? @default(now())
}

model DirectMessage {
  id                    Int       @default(autoincrement()) @id
  text                  String    @default("")
  directMessageSender   User      @relation(fields: [directMessageSenderId], references: [id])
  createdAt             DateTime? @default(now())
  directMessageSenderId Int
}

model Message {
  id                Int       @default(autoincrement()) @id
  text              String    @default("")
  messageSender     User      @relation(fields: [messageSenderId], references: [id])
  messageSenderId   Int
  affiliatedGroup   Group     @relation(fields: [affiliatedGroupId], references: [id])
  affiliatedGroupId Int
  createdAt         DateTime? @default(now())
}

createAccount.js

import { prisma, generateToken } from "../../../utils";
import bcrypt from "bcryptjs";

export default {
  Mutation: {
    createAccount: async (_, args) => {
      const { username, password, email } = args;

      const encryptPw = await bcrypt.hash(password, 10);
      let token;
      let newUser;
      try {
        newUser = await prisma.user.create({
          data: {
            username,
            email,
            password: encryptPw,
          },
        });
        token = generateToken(user.id);
        console.log(newUser, token);
      } catch (error) {
        console.log(error);
      }
      return { token, newUser };
    },
  },
};

utils.js

import dotenv from "dotenv";
dotenv.config();
import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);

Prisma client was successfully created.
Also, prisma migrate was successfully performed. But, all query and mutation operations related to DB occur this error. ex) await prisma.user.findMany({}), await prisma.user.findOne({....}) etc..

To fix this, i saw same title issue https://github.com/prisma/prisma-client-js/issues/832 This issue occured because of wrong schema.

Why this error occured? Is schema wrong?

pantharshit00 commented 4 years ago

Hey @khusw

Your issue looks different from #832

I am also unable to reproduce this with your example: image

Maybe share generateToken function with us as well as that might be causing the crash.

Also, if you can include the full crash logs here, that might be also helpful.

vel1024 commented 4 years ago

generateToken

import dotenv from "dotenv";
dotenv.config();
import jwt from "jsonwebtoken";
export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);

.env

JWT_SECRET="X9ZQ{rC77S_kEEaiAh_7IGMw`B,BfL"

logs

--
  prisma-client {
  prisma-client   engineConfig: {
  prisma-client     cwd: 'C:\\Users\\khucs\\Downloads\\Linker\\back\\prisma',
  prisma-client     enableDebugLogs: false,
  prisma-client     enableEngineDebugMode: undefined,
  prisma-client     datamodelPath: 'C:\\Users\\khucs\\Downloads\\Linker\\back\\node_modules\\.prisma\\client\\schema.prisma',
  prisma-client     prismaPath: undefined,
  prisma-client     engineEndpoint: undefined,
  prisma-client     generator: {
  prisma-client       name: 'client',
  prisma-client       provider: 'prisma-client-js',
  prisma-client       output: 'C:\\Users\\khucs\\Downloads\\Linker\\back\\node_modules\\@prisma\\client',
  prisma-client       binaryTargets: [Array],
  prisma-client       previewFeatures: [],
  prisma-client       config: {}
  prisma-client     },
  prisma-client     showColors: false,
  prisma-client     logLevel: undefined,
  prisma-client     logQueries: undefined,
  prisma-client     flags: [],
  prisma-client     clientVersion: '2.6.0',
  prisma-client     enableExperimental: [],
  prisma-client     useUds: undefined
  prisma-client   }
  prisma-client }  
--

C:\Users\khucs\Downloads\Linker\back>npm run dev

> capstone-back@1.0.0 dev C:\Users\khucs\Downloads\Linker\back
> nodemon --exec babel-node src/server.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,graphql
[nodemon] starting `babel-node src/server.js`
Server ready at http://localhost:4000
GET / 200 11.707 ms - -
POST /graphql 200 70.137 ms - 30506
POST /graphql 200 17.598 ms - 30506
PrismaClientRustPanicError:
Invalid `prisma.user.create()` invocation in
C:\Users\khucs\Downloads\Linker\back\src\api\User\createAccount\/createAccount.js:13:37

   9 const encryptPw = await bcrypt.hash(password, 10);
  10 let token;
  11 let newUser;
  12 try {
→ 13   newUser = await prisma.user.create(
  PANIC: index out of bounds: the len is 1 but the index is 1

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

https://github.com/prisma/prisma-client-js/issues/new?body=Hi+Prisma+Team%21+My+Prisma+Client+just+crashed.+This+is+the+report%3A%0A%23%23+Versions%0A%0A%7C+Name+++++%7C+Version++++++++++++%7C%0A%7C----------%7C--------------------%7C%0A%7C+Node+++++%7C+v12.16.2+++++++++++%7C+%0A%7C+OS+++++++%7C+windows++++++++++++%7C%0A%7C+Prisma+++%7C+2.6.0++++++++++++++%7C%0A%0A%0A%0A%23%23+Logs%0A%60%60%60%0A++prisma-client+%7B%0A++prisma-client+++engineConfig%3A+%7B%0A++prisma-client+++++cwd%3A+%27C%3A%5C%5CUsers%5C%5Ckhucs%5C%5CDownloads%5C%5CLinker%5C%5Cback%5C%5Cprisma%27%2C%0A++prisma-client+++++enableDebugLogs%3A+false%2C%0A++prisma-client+++++enableEngineDebugMode%3A+undefined%2C%0A++prisma-client+++++datamodelPath%3A+%27C%3A%5C%5CUsers%5C%5Ckhucs%5C%5CDownloads%5C%5CLinker%5C%5Cback%5C%5Cnode_modules%5C%5C.prisma%5C%5Cclient%5C%5Cschema.prisma%27%2C%0A++prisma-client+++++prismaPath%3A+undefined%2C%0A++prisma-client+++++engineEndpoint%3A+undefined%2C%0A++prisma-client+++++generator%3A+%7B%0A++prisma-client+++++++name%3A+%27client%27%2C%0A++prisma-client+++++++provider%3A+%27prisma-client-js%27%2C%0A++prisma-client+++++++output%3A+%27C%3A%5C%5CUsers%5C%5Ckhucs%5C%5CDownloads%5C%5CLinker%5C%5Cback%5C%5Cnode_modules%5C%5C%40prisma%5C%5Cclient%27%2C%0A++prisma-client+++++++binaryTargets%3A+%5BArray%5D%2C%0A++prisma-client+++++++previewFeatures%3A+%5B%5D%2C%0A++prisma-client+++++++config%3A+%7B%7D%0A++prisma-client+++++%7D%2C%0A++prisma-client+++++showColors%3A+false%2C%0A++prisma-client+++++logLevel%3A+undefined%2C%0A++prisma-client+++++logQueries%3A+undefined%2C%0A++prisma-client+++++flags%3A+%5B%5D%2C%0A++prisma-client+++++clientVersion%3A+%272.6.0%27%2C%0A++prisma-client+++++enableExperimental%3A+%5B%5D%2C%0A++prisma-client+++++useUds%3A+undefined%0A++prisma-client+++%7D%0A++prisma-client+%7D++%0A%60%60%60&title=PANIC%3A+index+out+of+bounds%3A+the+len+is+1+but+the+index+is+1&template=bug_report.md

If you want the Prisma team to look into it, please open the link above �

    at PrismaClientFetcher.request (C:\Users\khucs\Downloads\Linker\back\node_modules\@prisma\client\src\runtime\getPrismaClient.ts:1188:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
POST /graphql 200 850.863 ms - 54

To help, i upload repo url below. https://github.com/khusw/Linker/blob/master/back/src/utils.js#L40

In repo, i wrote dockerfile, but i didn't use docker. i only executed this repo in windows 10.

pantharshit00 commented 4 years ago

Hey @khusw

Can you please try again with the latest version once? I remember we fixed a similar issue recently.

The function you shared looks valid so I am unable to get to a reproduction. But yes, please try again with 2.7.1 once

vel1024 commented 4 years ago

@pantharshit00 I changed to the latest version, and the problem disappeared. Can you share me what caused the problem?

pantharshit00 commented 4 years ago

Hey @khusw

I don't remember the exact PR which fixed this but there was a duplicate issue which was opened in which we fixed this.

Basically the panic says internally in Rust binary code is trying to get first index of an empty array which will cause a panic.