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

called Option::unwrap() on a None value #19968

Open
sosafe-tim-schumacher opened this issue Jun 26, 2023 · 6 comments
Open

called Option::unwrap() on a None value #19968

sosafe-tim-schumacher opened this issue Jun 26, 2023 · 6 comments
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. tech/engines/query engine Issue in the Query Engine topic: multiSchema multiple schemas topic: previewFeatures Issue touches on an preview feature flag topic: rust panic topic: unwrap error topic: views

Comments

@sosafe-tim-schumacher
Copy link

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

Versions

Name Version
Node v18.16.1
OS debian-openssl-3.0.x
Prisma Client 4.15.0
Query Engine 8fbc245156db7124f997f4cecdd8d1219e360944
Database postgresql

Logs

prisma:tryLoadEnv Environment variables not found at /home/ubuntu/fake-data/node_modules/.env
prisma:tryLoadEnv Environment variables not found at /home/ubuntu/fake-data/node_modules/.env
prisma:tryLoadEnv No Environment variables loaded
prisma:client checkPlatformCaching:postinstall false
prisma:client checkPlatformCaching:ciName GitHub Actions
prisma:tryLoadEnv Environment variables not found at /home/ubuntu/fake-data/node_modules/.env
prisma:tryLoadEnv Environment variables not found at /home/ubuntu/fake-data/node_modules/.env
prisma:tryLoadEnv No Environment variables loaded
prisma:client dirname /home/ubuntu/fake-data/node_modules/@sosafe/prisma/dist
prisma:client relativePath ../../../src/prisma
prisma:client cwd /home/ubuntu/fake-data/node_modules/@sosafe/prisma/dist
prisma:client protocol graphql
prisma:client clientVersion 4.15.0
prisma:client clientEngineType library
prisma:client:libraryEngine internalSetup
prisma:get-platform Found distro info:
{
  "targetDistro": "debian",
  "familyDistro": "debian",
  "originalDistro": "ubuntu"
}
prisma:get-platform Trying platform-specific paths for "debian" (and "ubuntu")
prisma:get-platform Found libssl.so file using platform-specific paths: libssl.so.3
prisma:get-platform The parsed libssl version is: 3.0.x
prisma:client:engines:resolveEnginePath enginePath /home/ubuntu/fake-data/node_modules/@sosafe/prisma/dist/libquery_engine-debian-openssl-3.0.x.so.node
prisma:client:libraryEngine library starting
prisma:client:libraryEngine library started
prisma:client Prisma Client call:
prisma:client prisma.login.count({
  select: {
    _count: {
      select: {
        _all: true
      }
    }
  }
})
prisma:client Generated request:
prisma:client query {
  aggregatelogin {
    _count {
      _all
    }
  }
}

prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client Prisma Client call:
prisma:client prisma.login.findMany({
  skip: 795000,
  take: 1000,
  include: {
    address: true
  },
  orderBy: {
    id: 'asc'
  }
})
prisma:client Generated request:
prisma:client query {
  findManylogin(
    skip: 795000
    take: 1000
    orderBy: [
      {
        id: asc
      }
    ]
  ) {
    id
    email
    password
    salt
    lastSeen
    created
    enabled
    role
    language
    mailActivated
    gender
    area
    mandantId
    firstname
    lastname
    addressId
    passwordReset
    softDeleted
    playaudio
    feedback
    address {
      id
      customerId
      address
      enabled
      firstname
      lastname
      language
      gender
      degree
      created
      uuid
      changed
      source
      customerUuid
    }
  }
}

prisma:client:libraryEngine sending request, this.libraryStarted: true

Client Snippet

  protected override batchQuery(index: number) {
    return this.client.login.findMany({
      skip: 795695,
      take: 1,
      include: { address: true },
      orderBy: { id: 'asc' },
    });
  }

Schema

model login {
  id                                                                          Int                                @id @default(autoincrement())
  email                                                                       String?                            @db.VarChar(254)
  password                                                                    String?                            @db.VarChar(128)
  salt                                                                        String?                            @db.VarChar(16)
  lastSeen                                                                    DateTime?                          @map("last_seen") @db.Timestamptz(6)
  created                                                                     DateTime?                          @default(now()) @db.Timestamptz(6)
  enabled                                                                     Boolean?                           @default(true)
  role                                                                        Int?                               @default(0)
  language                                                                    String?                            @default("de") @db.VarChar(2)
  mailActivated                                                               Boolean?                           @default(false) @map("mail_activated")
  mandantId                                                                   Int?                               @default(1) @map("mandant_id")
  firstname                                                                   String?                            @db.VarChar(512)
  lastname                                                                    String?                            @db.VarChar(512)
  addressId                                                                   Int?                               @map("address_id")
  unlocked                                                                    Boolean?                           @default(true)
  passwordReset                                                               Boolean?                           @default(false) @map("password_reset")
  softDeleted                                                                 DateTime?                          @map("soft_deleted") @db.Timestamptz(6)
  loginIdentifier                                                             String?                            @map("login_identifier")
  address                                                                     address_view?                      @relation(fields: [addressId], references: [id])

  @@index([addressId])
  @@schema("public")
}

view address_view {
  id                     Int       @unique
  customerId             Int?      @map("customer_id")
  address                String?   @db.VarChar(256)
  enabled                Boolean?
  firstname              String?   @db.VarChar(512)
  lastname               String?   @db.VarChar(512)
  language               String?   @db.VarChar(2)
  gender                 Int?
  degree                 String?   @db.VarChar(256)
  created                DateTime? @db.Timestamptz(6)
  uuid                   String?   @db.Uuid
  type                   Int?
  organizeId             Int?      @map("organize_id")
  organize               String?   @db.VarChar(512)
  changed                DateTime? @db.Timestamptz(6)
  optIn                  Boolean?  @map("opt_in")
  source                 String?
  customerUuid           String?   @map("customer_uuid") @db.Uuid
  login                  login[]

  @@schema("public")
}

Prisma Engine Query

{"X":{}}

Engine Stack trace

thread 'tokio-runtime-worker' panicked at 'called `Option::unwrap()` on a `None` value', query-engine/core/src/interpreter/query_interpreters/nested_read.rs:223:50
stack backtrace:
   0:     0x7f64cb0d1adc - <unknown>
   1:     0x7f64ca9c962e - <unknown>
   2:     0x7f64cb0a6c24 - <unknown>
   3:     0x7f64cb0d31df - <unknown>
   4:     0x7f64cb0d2ddf - <unknown>
   5:     0x7f64cb0d3e7b - <unknown>
   6:     0x7f64cb0d3904 - <unknown>
   7:     0x7f64cb0d389c - <unknown>
   8:     0x7f64cb0d3871 - <unknown>
   9:     0x7f64ca8a0d52 - <unknown>
  10:     0x7f64ca8a0e4c - <unknown>
  11:     0x7f64caecc461 - <unknown>
  12:     0x7f64caed1595 - <unknown>
  13:     0x7f64caed0e14 - <unknown>
  14:     0x7f64caed2ee0 - <unknown>
  15:     0x7f64caed1623 - <unknown>
  16:     0x7f64caefbc1f - <unknown>
  17:     0x7f64caf00f66 - <unknown>
  18:     0x7f64ca8da4ae - <unknown>
  19:     0x7f64ca8dc4c9 - <unknown>
  20:     0x7f64ca8f7788 - <unknown>
  21:     0x7f64ca8f35d6 - <unknown>
  22:     0x7f64ca8e378e - <unknown>
  23:     0x7f64ca93db1b - <unknown>
  24:     0x7f64ca9152f1 - <unknown>
  25:     0x7f64ca91b377 - <unknown>
  26:     0x7f64cb0e95a4 - <unknown>
  27:     0x7f64cb0f2b2f - <unknown>
  28:     0x7f64cb0ec243 - <unknown>
  29:     0x7f64cb0ebcb5 - <unknown>
  30:     0x7f64cb0ec7e0 - <unknown>
  31:     0x7f64cb0d4f95 - <unknown>
  32:     0x7f64d9894b43 - <unknown>
  33:     0x7f64d9926a00 - <unknown>
  34:                0x0 - <unknown>

Remarks

I think something is not well with our data, but that should not crash the query engine. Are there any ways I could provide more debugging information?

These are the records that will break the query engine, starting at 795695:

staging=> select id,address_id from login where id > 795692 and id < 795700 order by id asc;
┌────────┬────────────┐
│   id   │ address_id │
├────────┼────────────┤
│ 795693 │    4779790 │
│ 795694 │    4238274 │
│ 795695 │    4953101 │
│ 795696 │    4949819 │
│ 795697 │    4751447 │
│ 795698 │    2859302 │
│ 795699 │    4751551 │
└────────┴────────────┘
(7 rows)

Time: 29.377 ms

If you want I can provide you the raw query results in private, but would need to check with legal first.

@jkomyno jkomyno added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. tech/engines/query engine Issue in the Query Engine team/client Issue for team Client. topic: rust panic labels Jun 29, 2023
@Jolg42 Jolg42 added topic: previewFeatures Issue touches on an preview feature flag topic: views topic: multiSchema multiple schemas labels Jun 29, 2023
@Jolg42
Copy link
Member

Jolg42 commented Jun 29, 2023

Note: Here is the same schema but with the datasource & generator blocks including the necessary preview features.
(It is valid, checked with prisma validate version 4.15 and 4.16.1)

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["multiSchema", "views"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  schemas  = ["public"]
}

model login {
  id              Int           @id @default(autoincrement())
  email           String?       @db.VarChar(254)
  password        String?       @db.VarChar(128)
  salt            String?       @db.VarChar(16)
  lastSeen        DateTime?     @map("last_seen") @db.Timestamptz(6)
  created         DateTime?     @default(now()) @db.Timestamptz(6)
  enabled         Boolean?      @default(true)
  role            Int?          @default(0)
  language        String?       @default("de") @db.VarChar(2)
  mailActivated   Boolean?      @default(false) @map("mail_activated")
  mandantId       Int?          @default(1) @map("mandant_id")
  firstname       String?       @db.VarChar(512)
  lastname        String?       @db.VarChar(512)
  addressId       Int?          @map("address_id")
  unlocked        Boolean?      @default(true)
  passwordReset   Boolean?      @default(false) @map("password_reset")
  softDeleted     DateTime?     @map("soft_deleted") @db.Timestamptz(6)
  loginIdentifier String?       @map("login_identifier")
  address         address_view? @relation(fields: [addressId], references: [id])

  @@index([addressId])
  @@schema("public")
}

view address_view {
  id           Int       @unique
  customerId   Int?      @map("customer_id")
  address      String?   @db.VarChar(256)
  enabled      Boolean?
  firstname    String?   @db.VarChar(512)
  lastname     String?   @db.VarChar(512)
  language     String?   @db.VarChar(2)
  gender       Int?
  degree       String?   @db.VarChar(256)
  created      DateTime? @db.Timestamptz(6)
  uuid         String?   @db.Uuid
  type         Int?
  organizeId   Int?      @map("organize_id")
  organize     String?   @db.VarChar(512)
  changed      DateTime? @db.Timestamptz(6)
  optIn        Boolean?  @map("opt_in")
  source       String?
  customerUuid String?   @map("customer_uuid") @db.Uuid
  login        login[]

  @@schema("public")
}

@Jolg42
Copy link
Member

Jolg42 commented Jun 29, 2023

@Jolg42
Copy link
Member

Jolg42 commented Jun 29, 2023

Note: It looks like in the past we had some reports for the same line of code (see https://github.com/search?q=repo%3Aprisma%2Fprisma+query-engine%2Fcore%2Fsrc%2Finterpreter%2Fquery_interpreters%2Fnested_read.rs&type=issues)

@jkomyno
Copy link
Contributor

jkomyno commented Jan 19, 2024

This problem is unfortunately not reproducible as it is.
@sosafe-tim-schumacher, can you please provide a minimal example with seed data of login and address in the range in which this query fails? (select id,address_id from login where id > 795692 and id < 795700 order by id asc;)

Thanks.

@apolanc apolanc added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jan 19, 2024
@sosafe-tim-schumacher
Copy link
Author

@jkomyno with a little bit of binary search I pinned it down to these records:

staging=> select id,address_id from login where id in (685369, 1301225, 1301226);
┌─────────┬────────────┐
│   id    │ address_id │
├─────────┼────────────┤
│  685369 │     299606 │
│ 1301225 │    9666792 │
│ 1301226 │    9666793 │
└─────────┴────────────┘
(3 rows)

Time: 33.120 ms

Looking further, I found something realy interesting:

staging=> SELECT
  l.id AS l__id,
  l.address_id AS l__address_id,
  av.id AS va__id
FROM login AS l
LEFT JOIN address_view as av ON l.address_id = av.id
WHERE l.id in (685369, 1301225, 1301226);
┌─────────┬───────────────┬─────────┐
│  l__id  │ l__address_id │ va__id  │
├─────────┼───────────────┼─────────┤
│ 1301225 │       9666792 │ 9666792 │
│ 1301225 │       9666792 │ 9666792 │
│  685369 │        299606 │  299606 │
│  685369 │        299606 │  299606 │
│ 1301226 │       9666793 │ 9666793 │
│ 1301226 │       9666793 │ 9666793 │
└─────────┴───────────────┴─────────┘
(6 rows)

Time: 4764.957 ms (00:04.765)

Somehow the view that get's joined into the login table returns two records for that id. And these are the only records querying further:

staging=> select count(*),id from address_view group by id having count(*) > 1;
┌───────┬─────────┐
│ count │   id    │
├───────┼─────────┤
│     2 │  299606 │
│     2 │ 9666792 │
│     2 │ 9666793 │
└───────┴─────────┘
(3 rows)

Time: 6086.317 ms (00:06.086)

So maybe prisma should crash with a more understandable error, nevertheless I'll fix the database, so I can avoid that issue.

@jkomyno jkomyno added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Feb 5, 2024
@jkomyno
Copy link
Contributor

jkomyno commented Mar 4, 2024

Thank you @sosafe-tim-schumacher. I confirm that the error arises because a fundamental precondition that Prisma requires on Views is violated by the datatabase: that a view (address_view, in this case) has a unique id.
views are still a preview feature, but we'll take this feedback into account (also see: #17335).
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. tech/engines/query engine Issue in the Query Engine topic: multiSchema multiple schemas topic: previewFeatures Issue touches on an preview feature flag topic: rust panic topic: unwrap error topic: views
Projects
None yet
Development

No branches or pull requests

4 participants