Skip to content

Transaction with bad input should rollback (when using middleware) #6705

Closed

Description

@Sytten

Bug description

If one query in the transaction fails the input validation, the transaction doesn't rollback and the other queries proceed normally.

Error:

Error: Argument city for data.address.create.city is missing.

    at Document.validate (/app/node_modules/.prisma/client/runtime/index.js:32462:19)
    at NewPrismaClient._executeRequest (/app/node_modules/.prisma/client/runtime/index.js:34399:17)
    at NewPrismaClient._requestWithMiddlewares (/app/node_modules/.prisma/client/runtime/index.js:34350:19)
    at /app/node_modules/.prisma/client/runtime/index.js:34343:53
    at /app/src/common/prisma/retry.js:35:38
    at NewPrismaClient._requestWithMiddlewares (/app/node_modules/.prisma/client/runtime/index.js:34343:16)
    at /app/node_modules/.prisma/client/runtime/index.js:34332:54
    at AsyncResource.runInAsyncScope (async_hooks.js:197:9)
    at NewPrismaClient._request (/app/node_modules/.prisma/client/runtime/index.js:34332:27)
    at Object.requestTransaction (/app/node_modules/.prisma/client/runtime/index.js:34469:39)

How to reproduce

Consider the following schema:

model User {
  id   Int    @id @default(autoincrement())
  name String
}

Run the following code:

const prisma = new PrismaClient();

prisma.$transaction([
  prisma.user.create({ data: { name: 'emile' } })
  prisma.user.create({ data: {} })
])

There should be a user in the database as the transaction is not rollback.

Expected behavior

The transaction should be rollback.

Environment & setup

  • OS: Linux
  • Database: PostgreSQL
  • Node.js version: 14
  • Prisma version: 2.21.2

Activity

  1. added
    bug/1-unconfirmed
    kind/bug
    on Apr 22, 2021
  2. pantharshit00 commented on Apr 22, 2021

    @pantharshit00
    Contributor

    Hmm, I wasn't able to reproduce this. I don't get the user in the database.

    I also enabled postgres logging and checked the database logs and those queries didn't even hit the database:
    image

    Maybe the faulty details went missing when you abstracted your query for a reproduction?

  3. added
    bug/0-unknown
    and removed
    bug/1-unconfirmed
    on Apr 22, 2021
  4. Sytten commented on Apr 22, 2021

    @Sytten
    Author

    Yeah most likely, just put an example on top of my head but I will try to reproduce. I think it my be due to a children create.

  5. Sytten commented on Apr 22, 2021

    @Sytten
    Author

    Ok I dig out the issue, it happens when there is a middleware present.
    Here is a simple repro: https://github.com/efugulin/prisma-repro-transaction
    Logs:

    Example app listening at http://localhost:3000
    prisma:query SELECT `main`.`User`.`id`, `main`.`User`.`name`, `main`.`User`.`noteId` FROM `main`.`User` WHERE 1=1 LIMIT ? OFFSET ?
    {"id":1,"name":"test","noteId":1}
    {"id":2,"name":"test","noteId":2}
    PrismaClientValidationError:
    Invalid `prisma.user.create()` invocation:
    
    {
      data: {
        name: 'test',
        note: {
          create: {
    +       content: String
          }
        }
      }
    }
    
    Argument content for data.note.create.content is missing.
    
    Note: Lines with + are required
    
        at Document.validate (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:32462:19)
        at NewPrismaClient._executeRequest (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34399:17)
        at NewPrismaClient._requestWithMiddlewares (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34350:19)
        at /Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34343:53
        at /Users/Sytten/Projects/prisma/prisma-repro-transaction/src/prisma.ts:9:26
        at NewPrismaClient._requestWithMiddlewares (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34343:16)
        at /Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34332:54
        at AsyncResource.runInAsyncScope (async_hooks.js:173:16)
        at NewPrismaClient._request (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34332:27)
        at Object.requestTransaction (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34469:39)
    PrismaClientValidationError:
    Invalid `prisma.user.create()` invocation:
    
    {
      data: {
        name: 'test',
        note: {
          create: {
    +       content: String
          }
        }
      }
    }
    
    Argument content for data.note.create.content is missing.
    
    Note: Lines with + are required
    
        at Document.validate (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:32462:19)
        at NewPrismaClient._executeRequest (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34399:17)
        at NewPrismaClient._requestWithMiddlewares (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34350:19)
        at /Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34343:53
        at /Users/Sytten/Projects/prisma/prisma-repro-transaction/src/prisma.ts:9:26
        at NewPrismaClient._requestWithMiddlewares (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34343:16)
        at /Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34332:54
        at AsyncResource.runInAsyncScope (async_hooks.js:173:16)
        at NewPrismaClient._request (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34332:27)
        at Object.requestTransaction (/Users/Sytten/Projects/prisma/prisma-repro-transaction/node_modules/@prisma/client/runtime/index.js:34469:39)
    prisma:query BEGIN
    prisma:query INSERT INTO `main`.`Note` (`content`) VALUES (?)
    prisma:query SELECT `main`.`Note`.`id`, `main`.`Note`.`content` FROM `main`.`Note` WHERE `main`.`Note`.`id` = ? LIMIT ? OFFSET ?
    prisma:query COMMIT
    

    This is my middleware:

    prisma.$use(async (params, next) => {
      try {
        const result = await next(params);
        return result;
      } catch (err) {
        console.log(err);
        throw err;
      }
    });
  6. added
    bug/2-confirmed
    and removed
    bug/0-unknown
    on Apr 22, 2021
  7. changed the title [-]Transaction with bad input should rollback[/-] [+]Transaction with bad input should rollback (when using middleware)[/+] on Apr 22, 2021
  8. 1 remaining item

  9. modified the milestones: 2.23.0, 2.24.0 on May 19, 2021
  10. modified the milestones: 2.24.0, 2.25.0 on Jun 2, 2021
  11. millsp commented on Jun 14, 2021

    @millsp
    Contributor

    Note: The rollback is ensured by controlling a race condition. In _executeRequest , there is a first block that validates. Then there's that return of this._fetcher.request that is another promise. When running transactions without middlewares, _executeRequest is called for each member of the transaction, thus running the validation. No await is yet called, just execution. Then an exception might have thrown in the validation, program stops. If it has not stopped, it continues to loop over the results of the previous execution, but this time with await. This causes to have a 2-in-1 function that validates then yields. However, with middlewares, since we do await next(params), it hijacks this 2-in-1 system, and renders it useless. Worth mentioning that this works because transactions are not executed immediately but stored in an anonymous fn that comes out of this._fetcher.request. Regular requests don't work like that and are executed immediately. When using middlewares, transactions are marked as regular requests => the reason why they don't rollback. However, adding the transactionId that identifies it as a real transaction fixed the rollback issues but caused this issue you linked.

  12. modified the milestones: 2.25.0, 2.26.0 on Jun 17, 2021
  13. millsp commented on Jun 30, 2021

    @millsp
    Contributor

    So in link with what I mentioned above, the current transaction implementation relies on failure immediateness to rollback. For this reason, since middlewares are asynchronous by design and under the hood, we cannot rollback. Doing await next(params) triggers the query to be executed. This is a problem because we expect a result, but at the same time we want previous queries to rollback if needed. Because of this, we cannot rely on immediate failure to rollback, we are awaiting results - one at a time.

    The obvious solution now is that we implement #1844

  14. Sytten commented on Jun 30, 2021

    @Sytten
    Author

    I mean in my case one of the query fails the validation and is not even sent through to the query engine. Could we at least get that working?

  15. millsp commented on Jun 30, 2021

    @millsp
    Contributor

    If we "fix" things so that it can fail/stop on Document.validate, we'll end up with a mediocre solution that just stops on the first error. Rollback won't happen and you'll have a partial "transaction" applied. Transactions are sequential, before you move to the next one you wait that the middleware has finished (and that's the query exec). So no rollback like asked in the OP. (this is again because async makes the failure non-immediate).

  16. matthewmueller commented on Jul 1, 2021

    @matthewmueller
    Contributor

    Depends on #1844

  17. removed this from the 2.26.0 milestone on Jul 1, 2021
  18. added this to the 2.29.0 milestone on Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/2-confirmedkind/bugtopic: middlewaretopic: transaction

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions