Repository navigation
Closed
Labels
bug/2-confirmedBug has been reproduced and confirmed.kind/bugA reported bug.tech/typescriptIssue for tech TypeScript.topic: interactiveTransactionstopic: previewFeaturesIssue touches on an preview feature flag
Milestone
Description
Bug description
I'm trying to use interactive transactions to build a bank transfer. I'd like to try to achieve the following:
- Alice and Bob have $100
- Concurrently, Alice sends Bob $100 twice
- One of those requests goes through
- The other one is rejected saying there's not enough money
- Alice has $0, Bob has $100
How to reproduce
Reproduction Repo: https://github.com/matthewmueller/interactive-transactions
Given the following Prisma Schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions"]
}
model Account {
id Int @id @default(autoincrement())
email String @unique
balance Int
}And the following script:
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
async function unseed() {
await prisma.account.deleteMany({
where: {
OR: [{ email: "[email protected]" }, { email: "[email protected]" }],
},
})
}
async function seed() {
await prisma.account.create({
data: {
email: "[email protected]",
balance: 100,
},
})
await prisma.account.create({
data: {
email: "[email protected]",
balance: 100,
},
})
}
async function transfer(nth: number, from: string, to: string, amount: number) {
return await prisma.$transaction(
async (prisma) => {
console.time("send " + nth)
const sender = await prisma.account.update({
data: {
balance: {
decrement: amount,
},
},
where: {
email: from,
},
})
console.timeEnd("send " + nth)
console.time("throw " + nth)
if (sender.balance < 0) {
throw new Error(`${from} doesn't have enough to send ${amount}`)
}
console.timeEnd("throw " + nth)
console.time("recieve " + nth)
const recipient = prisma.account.update({
data: {
balance: {
increment: amount,
},
},
where: {
email: to,
},
})
console.timeEnd("recieve " + nth)
return recipient
},
{
timeout: 20000,
}
)
}
async function main() {
await prisma.$connect()
await unseed()
await seed()
console.time("transfer")
await Promise.all([
transfer(1, "[email protected]", "[email protected]", 100),
transfer(2, "[email protected]", "[email protected]", 100),
])
console.timeEnd("transfer")
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect())If you setup a database and then run ts-node index.ts, you'll get:
send 1: 7.005ms
throw 1: 0.005ms
recieve 1: 0.279ms
send 2: 20.005s
throw 2: 0.367ms
recieve 2: 0.644ms
PrismaClientKnownRequestError3 [PrismaClientKnownRequestError]:
Invalid `prisma.account.deleteMany()` invocation in
/Users/m/dev/src/github.com/prisma/interactive-transactions/index.ts:5:58
2 const prisma = new PrismaClient()
3
4 async function unseed() {
→ 5 await prisma.account.deleteMany(
Transaction API error: Transaction already closed: Transaction is no longer valid. Last state: 'Expired'.
at RequestHandler.request (/Users/m/dev/src/github.com/prisma/interactive-transactions/node_modules/@prisma/client/runtime/index.js:36361:15)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at PrismaClient._transactionWithCallback (/Users/m/dev/src/github.com/prisma/interactive-transactions/node_modules/@prisma/client/runtime/index.js:36932:18) {
code: 'P2028',
clientVersion: '2.30.0-dev.8',
meta: {
error: "Transaction already closed: Transaction is no longer valid. Last state: 'Expired'."
}
}
Two notes:
- The 2nd send takes the full 20s
- An FYI that the stack trace is off. It was pointing to that line even when it was commented out.
I think this could be a bug because if I change the code to transfer serially, it works as expected:
async function main() {
await prisma.$connect()
await unseed()
await seed()
await transfer("[email protected]", "[email protected]", 100)
await transfer("[email protected]", "[email protected]", 100) // Error: [email protected] doesn't have enough to send 100
}Expected behavior
I'd expect this to work as expected, you should be able to initialize multiple transactions concurrently and let the database sort it out.
Prisma information
Environment & setup
- OS: OSX
- Database: Postgres
- Node.js version: v14.16.0
Prisma Version
Environment variables loaded from .env
prisma : 2.30.0-dev.8
@prisma/client : 2.30.0-dev.8
Current platform : darwin
Query Engine (Binary) : query-engine 71d96e8bbd21982078694f00add0f51da2056a8b (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 71d96e8bbd21982078694f00add0f51da2056a8b (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 71d96e8bbd21982078694f00add0f51da2056a8b (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 71d96e8bbd21982078694f00add0f51da2056a8b (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 71d96e8bbd21982078694f00add0f51da2056a8b
Studio : 0.419.0
Preview Features : interactiveTransactions
Reacted by Lukas Heddendorp, Mustafa Ekrem KENTER, Michał Lytek, smiley-uriux, Tom MacWright, Rishabh Rao and Michał
Metadata
Metadata
Assignees
Labels
bug/2-confirmedBug has been reproduced and confirmed.kind/bugA reported bug.tech/typescriptIssue for tech TypeScript.topic: interactiveTransactionstopic: previewFeaturesIssue touches on an preview feature flag
Activity
[-]Transaction API error: Transaction already closed: Transaction is no longer valid. Last state: 'Expired'[/-][+]interactiveTransactions: 2 concurrent writes to the row will cause it to hang until expiring[/+][-]interactiveTransactions: 2 concurrent writes to the row will cause it to hang until expiring[/-][+]interactiveTransactions: 2 concurrent writes to the same row will cause it to hang until expiring[/+]matthewmueller commentedon Aug 12, 2021
I just tested it with Postgres via
pg. First I used a single connection:Running
ts-node postgres.ts,The result:
This sort of makes sense because with 1 connection, 2 concurrent requests, it's not clear in what order the queries happen.
Next I setup the pool and used a separate connection per transfer.
This works as expected:
From this, it seems like Prisma should use a separate connection per transaction. I'm still not sure why it hangs since even on the same connection, pg still processes the queries.
matthewmueller commentedon Aug 24, 2021
More investigation here: https://github.com/matthewmueller/interactive-transactions
27 remaining items
millsp commentedon Jan 11, 2022
I think I might have a repro - which differs from the OP since that issue has been fixed. Instead, I just performed a load test and triggering many transactions at once. That sometimes works and other times it leads to the screenshot below.

binaryengine seems more susceptible to this, even though it happens onlibrarytoo (without proof of where is stalls on that one though).s-montigny-desautels commentedon Jan 11, 2022
I did experience this too. What I also found is that when using the
interactiveTransactionspreview feature, regular transaction will also suffer from the same probleme, i.e a transaction using an array of Prisma Promise. When disabling the feature, regular transaction work as expected.linde12 commentedon Jan 12, 2022
Encountering the same issue when having multiple shortlived concurrent transactions.
{"code":"P2028","clientVersion":"3.4.2","meta":{"error":"Transaction already closed: Transaction is no longer valid. Last state: 'Expired'."}}Can't provide code unfortunately :(
ebaer-payix commentedon Jan 21, 2022
I have seen the same thing in my application. I created a "Poor Man's" SEQUENCE for MySQL, and it uses the transaction array listed in s-montigny-desautels comment above. The application is running in AWS with a Health Check that hits it about every 10 seconds. I put many changes in, including upgrading all the packages, and Prisma to 3.7. Suddenly my application was crashing randomly (varying from <30 minutes to 4+ hours), and forcing an AWS restart. This was too difficult to narrow down for debugging purposes, as when the CPU went to 100%, it was only 1 - 2 minutes before AWS restarted the application, and it wasn't happening locally.
I was at a complete loss to determine what was going on, due to the number of changes, but it finally occurred to me it was the Prisma change, as two applications were crashing, neither of which were doing anything at the time, except the Health Checks. After investigating the Health Check, I realized it was using a prisma.$transaction([]), and had a lead, since I had also turned on the previewFeatures = ["interactiveTransactions"], and even though it was not being used anywhere, it was a likely cause.
I commented out the interactiveTransaction in the schema.prisma, and redeployed. The attached graph shows the Random crashes before the ~ 9:30am deployment, and none since.
I could provide the code that caused it, but I don't think it is needed, as it is pretty obvious that the Health Check Transaction was somehow getting called again, before completing the previous call. The only reason this would likely happen, is that the prisma.$transaction wasn't finishing and/or cleaning up anything it was doing before returning from the Promise, so the next call to it is not starting with a "clean slate".
NOTE: This is some how tied to the previewFeature interactiveTransactions.
Update:
After looking at the graphs highly expanded (image attached), another thought occurred to me. These Memory Usages were from 2 separate Tasks running in separate Containers. I am now thinking that whatever Prisma is using to create (identify) the Transaction is stored in the Database (possibly a timestamp ?), because the Database requests are coming from 2 different systems at the same time, and both systems CPU usage went to 100%, and then both got restarted. I am guessing that the 2nd transaction in is somehow corrupting the first one because it ends up with the same identifier and then whatever they use to check completion goes into an infinite loop, causing both to go to 100% CPU utilization.
garrensmith commentedon Jan 21, 2022
Thanks for reporting this. We are actively working to resolve these issues.
ebaer-payix commentedon Jan 21, 2022
Sorry, I had a dyslectic moment with the Prisma Version numbers in the first Graph above.
I have corrected it now...
millsp commentedon Jan 31, 2022
Engine deadlocks have been fixed via
prisma/prisma-engines#2619
and #10797
Update 058-transactions.mdx (#2551)
fix: address automated review findings (Redis timeout, tx concurrency…