Skip to content

Node hangs when console.log(new PrismaClient()) #18292

Closed

Description

@sc0ch

Bug description

Linux - node process hangs (stuck on prisma:client:libraryEngine internalSetup +0ms)
Windows - javascript out of memory error.
Any schema.
Affected versions of @prisma/client > 4.7.0

How to reproduce

  1. npx prisma init
  2. npx prisma generate (npx prisma db push)
  3. Create index.mjs with:
    import { PrismaClient } from '@prisma/client'
    console.log(new PrismaClient())
    
  4. export DEBUG="*" && node index.mjs -> See error

Expected behavior

No response

Prisma information

generator client {
  provider = "prisma-client-js"
}

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

model Session {
  id    Int    @id @default(autoincrement())
  key   String @unique
  value String

  @@map("sessions")
}

Environment & setup

  • OS: Windows 11, Ubuntu 22.04.2
  • Database: MySQL
  • Node.js version: 18.5.0, 19.7.0

Prisma Version

prisma                  : 4.11.0
@prisma/client          : 4.11.0
Current platform        : debian-openssl-3.0.x
Query Engine (Node-API) : libquery-engine 8fde8fef4033376662cad983758335009d522acb (at node_modules/@prisma/engines/libquery_engine-debian-openssl-3.0.x.so.node)
Migration Engine        : migration-engine-cli 8fde8fef4033376662cad983758335009d522acb (at node_modules/@prisma/engines/migration-engine-debian-openssl-3.0.x)
Format Wasm             : @prisma/prisma-fmt-wasm 4.11.0-57.8fde8fef4033376662cad983758335009d522acb
Default Engines Hash    : 8fde8fef4033376662cad983758335009d522acb
Studio                  : 0.483.0

Activity

  1. added
    kind/bug
    on Mar 12, 2023
  2. added
    bug/1-unconfirmed
    on Mar 14, 2023
  3. janpio commented on Mar 14, 2023

    @janpio
    Contributor

    Why do you want to console.log(new PrismaClient())?

  4. AvIsBeastMC commented on Mar 19, 2023

    @AvIsBeastMC

    Why do you want to console.log(new PrismaClient())?

    fr

  5. entrptaher commented on Apr 5, 2023

    @entrptaher

    I did this and then whenever I wanted to console.log(prisma) anywhere for any reason, it hung the whole process.

    import { PrismaClient } from "@prisma/client";
    let prisma = new PrismaClient();
    console.log(prisma)

    The issue is in 4.8.0 and all later versions while it's not present in 4.7.0 or older versions. I believe the issue is related to this optimizations.

    In this release, we have decreased the size of our engine files by an average of 50%. The size of the Query Engine used on Debian, with OpenSSL 3.0.x, for example, went from 39MB to 14MB. We will also remove some smaller engines to decrease the total size in future versions.

  6. professor-l commented on Apr 17, 2023

    @professor-l

    I've encountered this as well. I use a development repl in one of my projects, and though I can const p = new PrismaClient(), a subsequent p to check whether it's defined hangs the process.

    This also means that declaring and defining in one line works:

    const p = new PrismaClient() // runs successfully in a repl

    But attempting to do so in two separate steps does not:

    let p
    p = new PrismaClient() // hangs the process in a repl
  7. changed the title [-]NodeJS hangs when console.log(new PrismaClient())[/-] [+]NodeJS hangs when `console.log(new PrismaClient())`[/+] on Apr 18, 2023
  8. changed the title [-]NodeJS hangs when `console.log(new PrismaClient())`[/-] [+]Node hangs when `console.log(new PrismaClient())`[/+] on Apr 18, 2023
  9. janpio commented on Apr 18, 2023

    @janpio
    Contributor

    I can actually log new PrismaClient() fine:

    const { PrismaClient } = require('@prisma/client')
    
    async function main() {
        console.log(new PrismaClient())
    }
    
    main()
      .catch(async (e) => {
        console.error(e)
        await prisma.$disconnect()
        process.exit(1)
      })
    

    When I put the output of this into a file, it is 525 MB (551.039.534 bytes) big though - so I would not be surprised if that would crash in some environments.

    The output looks recursive to me in 2 places:

    {
      _middlewares: MiddlewareHandler { _middlewares: [] },
      _getDmmf: [Function (anonymous)],
      _getProtocolEncoder: [Function (anonymous)],
      '$extends': [Function: $extends],
      _extensions: MergedExtensionsList { head: undefined },
      _previewFeatures: [],
      _rejectOnNotFound: undefined,
      _clientVersion: '4.13.0',
      _activeProvider: 'postgresql',
      _dataProxy: false,
      _tracingConfig: { enabled: [Getter], middleware: [Getter] },
      _clientEngineType: 'library',
      _errorFormat: 'colorless',
      _baseDmmf: {
        datamodel: { enums: [], models: [Array], types: [] },
        datamodelEnumMap: {},
        modelMap: { User: [Object] },
        typeMap: {},
        typeAndModelMap: { User: [Object] },
        mappings: { modelOperations: [Array], otherOperations: [Object] },
        mappingsMap: { User: [Object] }
      },
      _engineConfig: {
        cwd: 'C:\\Users\\Jan\\Documents\\throwaway\\18292\\prisma',
        dirname: 'C:\\Users\\Jan\\Documents\\throwaway\\18292\\node_modules\\.prisma\\client',
        enableDebugLogs: false,
        allowTriggerPanic: undefined,
        datamodelPath: 'C:\\Users\\Jan\\Documents\\throwaway\\18292\\node_modules\\.prisma\\client\\schema.prisma',
        prismaPath: undefined,
        engineEndpoint: undefined,
        datasources: [],
        generator: {
          name: 'client',
          provider: [Object],
          output: [Object],
          config: [Object],
          binaryTargets: [],
          previewFeatures: []
        },
        showColors: false,
        logLevel: undefined,
        logQueries: undefined,
        env: {
          DATABASE_URL: 'postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public'
        },
        flags: [],
        clientVersion: '4.13.0',
        previewFeatures: [],
        activeProvider: 'postgresql',
        inlineSchema: undefined,
        inlineDatasources: undefined,
        inlineSchemaHash: undefined,
        tracingConfig: { enabled: [Getter], middleware: [Getter] },
        logEmitter: EventEmitter {
          _events: [Object: null prototype],
          _eventsCount: 1,
          _maxListeners: undefined,
          [Symbol(kCapture)]: false
        },
        engineProtocol: 'graphql'
      },
      _engine: LibraryEngine {
        datamodel: '// This is your Prisma schema file,\n' +
          '// learn more about it in the docs: https://pris.ly/d/prisma-schema\n' +
          '\n' +
          'generator client {\n' +
          '  provider = "prisma-client-js"\n' +
          '}\n' +
          '\n' +
          'datasource db {\n' +
          '  provider = "postgresql"\n' +
          '  url      = env("DATABASE_URL")\n' +
          '}\n' +
          '\n' +
          'model User {\n' +
          '  id    Int     @id @default(autoincrement())\n' +
          '  email String  @unique\n' +
          '  name  String?\n' +
          '}\n',
        config: {
          cwd: 'C:\\Users\\Jan\\Documents\\throwaway\\18292\\prisma',
          dirname: 'C:\\Users\\Jan\\Documents\\throwaway\\18292\\node_modules\\.prisma\\client',
          enableDebugLogs: false,
          allowTriggerPanic: undefined,
          datamodelPath: 'C:\\Users\\Jan\\Documents\\throwaway\\18292\\node_modules\\.prisma\\client\\schema.prisma',
          prismaPath: undefined,
          engineEndpoint: undefined,
          datasources: [],
          generator: [Object],
          showColors: false,
          logLevel: undefined,
          logQueries: undefined,
          env: [Object],
          flags: [],
          clientVersion: '4.13.0',
          previewFeatures: [],
          activeProvider: 'postgresql',
          inlineSchema: undefined,
          inlineDatasources: undefined,
          inlineSchemaHash: undefined,
          tracingConfig: [Object],
          logEmitter: [EventEmitter],
          engineProtocol: 'graphql'
        },
        libraryStarted: false,
        logQueries: false,
        logLevel: 'error',
        libraryLoader: DefaultLibraryLoader {
          libQueryEnginePath: null,
          platform: null,
          config: [Object]
        },
        logEmitter: EventEmitter {
          _events: [Object: null prototype],
          _eventsCount: 1,
          _maxListeners: undefined,
          [Symbol(kCapture)]: false
        },
        engineProtocol: 'graphql',
        datasourceOverrides: [Object: null prototype] {},
        libraryInstantiationPromise: Promise { <pending> }
      },
      _fetcher: RequestHandler {
        logEmitter: EventEmitter {
          _events: [Object: null prototype],
          _eventsCount: 1,
          _maxListeners: undefined,
          [Symbol(kCapture)]: false
        },
        client: {
           ... RECURSION 1 HERE ...
        },
        dataloader: DataLoader { options: [Object], tickActive: false, batches: {} }
      },
      _metrics: MetricsClient {
        _engine: LibraryEngine {
          datamodel: '// This is your Prisma schema file,\n' +
            '// learn more about it in the docs: https://pris.ly/d/prisma-schema\n' +
            '\n' +
            'generator client {\n' +
            '  provider = "prisma-client-js"\n' +
            '}\n' +
            '\n' +
            'datasource db {\n' +
            '  provider = "postgresql"\n' +
            '  url      = env("DATABASE_URL")\n' +
            '}\n' +
            '\n' +
            'model User {\n' +
            '  id    Int     @id @default(autoincrement())\n' +
            '  email String  @unique\n' +
            '  name  String?\n' +
            '}\n',
          config: [Object],
          libraryStarted: false,
          logQueries: false,
          logLevel: 'error',
          libraryLoader: [DefaultLibraryLoader],
          logEmitter: [EventEmitter],
          engineProtocol: 'graphql',
          datasourceOverrides: [Object: null prototype] {},
          libraryInstantiationPromise: [Promise]
        }
      },
      user: {
        findUnique: [Function (anonymous)],
        findUniqueOrThrow: [Function (anonymous)],
        findFirst: [Function (anonymous)],
        findFirstOrThrow: [Function (anonymous)],
        findMany: [Function (anonymous)],
        create: [Function (anonymous)],
        createMany: [Function (anonymous)],
        delete: [Function (anonymous)],
        update: [Function (anonymous)],
        deleteMany: [Function (anonymous)],
        updateMany: [Function (anonymous)],
        upsert: [Function (anonymous)],
        aggregate: [Function (anonymous)],
        groupBy: [Function (anonymous)],
        count: [Function (anonymous)],
        name: 'User'
      },
      [Symbol()]: {
          ... RECURSION 2 HERE ...
       }
    }
    

    Both cases are roughly 100.000 lines long in my file.

    Not optimal.

  10. jonschlinkert commented on Apr 21, 2023

    @jonschlinkert

    I experience this too. I've had to create work arounds everywhere in our application that might have client (an instance of prisma) on a class instance. Otherwise the server freezes anytime something logs out.

    To fix this, anywhere recursive information is added to the object I recommend using Reflect.defineProperty so that it can be made non-enumerable, instead of simple accessor assignment.

    Example:

    Reflect.defineProperty(prisma, Symbol(), {
      value: {...},
      enumerable: false
    });
  11. janpio commented on May 17, 2023

    @janpio
    Contributor

    This seems to also happen when logging the tx inside an interactive $transaction - which makes sense as that is just another Client instance: #19137

  12. SD-Gaming commented on Jul 14, 2023

    @SD-Gaming

    I have met this issue too,

    OS:  win10 x64
    
    node: 18.15.4 x64
    
    prisma: 4.16.1 client 4.16.1
    
  13. 23 remaining items

  14. codeinearts commented on Feb 2, 2024

    @codeinearts

    I'm running into an issue that may be a different manifestation of this same issue. In Visual Studio Code, whenever I attempt to debug into a class that extends PrismaClient, the node process hangs. Also if I attempt to inspect the instance of this class before stepping into it, the node process hangs. I'm not able to debug any of my classes that extend PrismaClient because of this. I wonder if the debugging issue has something to do with the debugger inspecting the PrismaClient instance for a watch locals or something and this inspecting leads to the same error as logging the PrismaClient instance.

    I'm also experiencing this issue after upgrading from 4.11.0 to latest 5.9.1.

    When attaching the debugger to some breakpoints while working on some client extensions, it hangs the whole application (drove me crazy today, just in the middle of a deadline 🫠)

  15. codeinearts commented on Feb 2, 2024

    @codeinearts

    @llimllib you can work around the issue with this:

    const repl = require("node:repl");
    
    const r = repl.start("> ");
    r.context.prisma = new (require("@prisma/client").PrismaClient)();
    r.context.prisma[Symbol.for('nodejs.util.inspect.custom')] = 'PrismaClient';

    as suggested here: #19648 (comment)

    I can confirm this workaround also fixes my issue using the debugger when extending PrismaClient as a class in the context of a NestJS application

    @Injectable()
    export class PrismaService
      extends PrismaClient<PrismaClientOptions, "query" | "error">
      implements OnModuleInit, OnModuleDestroy
    {
      private readonly logger = new Logger(PrismaService.name);
    
      constructor(private readonly configService: ApiConfigService) {
        super({
          log: [
            {
              emit: "event",
              level: "query",
            },
            {
              emit: "event",
              level: "error",
            },
            {
              emit: "stdout",
              level: "info",
            },
            {
              emit: "stdout",
              level: "warn",
            },
          ],
        });
    
        /**
         * new PrismaClient versions hangs debugger
         * https://github.com/prisma/prisma/issues/18292#issuecomment-1749123704
         */
        Object.assign(this, {
          [Symbol.for("nodejs.util.inspect.custom")]: "PrismaCLient",
        });
    
        this.$on("error", (event) => {
          this.logger.log(event.target);
        });
    
        this.$on<any>("query", (event: Prisma.QueryEvent) => {
          const { query, timestamp, duration } = event;
          const collection = query.split("(")[0];
          this.logger.log(
            { collection, timestamp, duration, query },
            "PRISMA QUERY DEBUG"
          );
        });
    
        this.$use(async (params, next) => {
          const before = Date.now();
    
          const result = await next(params);
    
          const after = Date.now();
    
          const duration = after - before;
    
          this.logger.log(
            { collection: params.model, operation: params.action, duration },
            `Query ${params.model}.${params.action} took ${duration} ms`
          );
    
          return result;
        });
      }
    
      async onModuleInit() {
        await this.$connect();
      }
    
      async onModuleDestroy() {
        await this.$disconnect();
      }
    }
  16. added 2 commits that reference this issue on Feb 21, 2024
    906d6cf
    6c4ea75
  17. added this to the 5.11.0 milestone on Feb 23, 2024
  18. janpio commented on Mar 4, 2024

    @janpio
    Contributor

    Hey everyone that commented in this issue before, could you please try to temporarily install prisma@dev and @prisma/client@dev in your project and see if this fixes the problem? #23228 should have fixed this and will be included in the next release, but of course a confirmation of it now working as intended would be nice.

  19. wodCZ commented on Mar 5, 2024

    @wodCZ

    @janpio I've just tested and can confirm that the dev version no-longer freezes REPL instance when the command would output anything with prisma instance on it.

    With 5.10.2 it does freeze, so the fix must've addressed it correcly.

    Can't wait for the release, thanks SevInf and team for the fix 🙌

  20. YoannBuzenet commented on Mar 7, 2024

    @YoannBuzenet

    It works with the dev version. Thank you !

  21. thw0rted commented on Apr 19, 2024

    @thw0rted

    Hi @janpio , since it looks like the actual fix in the PR is only a few lines (and the rest are tests...), would it be possible to get a backport for those of us still stuck on 4.x? The [Symbol.for...] fix a few comments up is helpful, but has to be re-applied every time I launch the REPL.

  22. janpio commented on Apr 19, 2024

    @janpio
    Contributor

    Unfortunately not, we never maintain older versions by backporting changes or fixes.

    You would need to do that yourself in a fork and publish it under your own package name.
    Probably you can achieve something similar using https://www.npmjs.com/package/patch-package

  23. divmgl commented on May 13, 2024

    @divmgl

    Thanks for this fix.

    I want to add another case where this was problematic. We have a dependency injection container where we store our Prisma client for use in parts of the app that require it. Dependencies are lazily loaded, but we connect the Prisma client on app launch, so it's always loaded.

    There have been moments where we need to inspect other parts of the container, so we'll console.log the container. This is probably a bad idea, but we would log out the full container to inspect it. This would unfortunately hang the entire process during development and we were never able to figure out why this was happening. So now we know!

    So thank you for fixing this bug! Super helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugtopic: loggingtopic: prisma-client

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions