mikro-orm / nestjs-realworld-example-app

Example real world backend API built with NestJS + MikroORM
https://realworld.io/
MIT License
280 stars 72 forks source link

The app uses global EntityManager instance on debugging #63

Closed RedMickey closed 2 years ago

RedMickey commented 2 years ago

Hi, I faced interesting issue while trying out this sample app. The issue is that the application throws the following error only when a debugger is attached to the app:

ERROR [ExceptionsHandler] Using global EntityManager instance methods for context specific actions is disallowed. If you need to work with the global instance's identity map, use `allowGlobalContext` configuration option or `fork()` instead.
ValidationError: Using global EntityManager instance methods for context specific actions is disallowed. If you need to work with the global instance's identity map, use `allowGlobalContext` configuration option or `fork()` instead.
    at Function.cannotUseGlobalContext (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/node_modules/@mikro-orm/core/errors.js:83:16)
    at SqlEntityManager.getContext (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/node_modules/@mikro-orm/core/EntityManager.js:751:44)
    at SqlEntityManager.createQueryBuilder (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/node_modules/@mikro-orm/knex/SqlEntityManager.js:15:30)
    at SqlEntityRepository.createQueryBuilder (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/node_modules/@mikro-orm/knex/SqlEntityRepository.js:15:24)
    at ArticleService.<anonymous> (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/src/article/article.service.ts:27:8)
    at Generator.next (<anonymous>)
    at /Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/dist/article/article.service.js:20:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/dist/article/article.service.js:16:12)
    at ArticleService.findAll (/Users/user/Documents/nestjs-realworld-example-app2/nestjs-realworld-example-app/dist/article/article.service.js:39:16)

When I start the app without a debugger, it works fine.

I've investigated this problem and found out that the issue is related to Node.js 18.x. I tested the app on Node.js v18.6 and v16.15 and the issue was reproduced only on 18th version. As I understood from my investigation, on Node 18, the currentRequestContext function sometimes returns undefined instead of the current store. Then I also faced the same problem on my own app while migrating to the 5th version of the ORM, so this issue may not be specific only to the sample application.

Steps to reproduce

Prerequisites: Node.js 18.x

  1. Configure RequestContext support in your app (nestjs-realworld-example-app is already configured)
  2. Run the app in debug mode using yarn start:debug command or via VS Code scenario:
        {
            "name": "Launch via yarn",
            "request": "launch",
            "runtimeArgs": [
                "start"
            ],
            "runtimeExecutable": "yarn",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "type": "node"
        }
  3. If the app was started in the terminal, attach a debugger to the app (Google Chrome or VS Code, etc)
  4. Send the request http://localhost:3000/api/articles
B4nan commented 2 years ago

I doubt this is something "fixable" on our end, its probably a nodejs bug.

RedMickey commented 2 years ago

Yep, It seems that this issue is related to these ones in Node repo: https://github.com/nodejs/node/issues/44415 https://github.com/nodejs/node/issues/43148