Repository navigation
TML-3345, TML-3210: A closed client's promise-returning members reject instead of throwing at the call - #30840
Conversation
… the transaction ends tx.query, prepared.query(tx) and prepared.execute(tx) on an ended transaction now report RUNTIME.TRANSACTION_CLOSED through the result they return instead of throwing at the call. lazyActivitySource rejects when its source cannot be found. deferRows and deferPromise let a client build a result whose failure is reported by that result. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…eturn query, execute, prepare and transaction on postgres() and sqlite(), the ORM runtime of both, and prepare on a supabase() role-bound client reject on a closed client or one whose driver failed to connect, instead of throwing at the call. The shared builders resolve the runtime inside the result. db.runtime() still throws. The serverless connection and mongo() already followed the rule; tests now pin it. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…tructions Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…ed up at the call runtimeFrom(getRuntime) gives query, execute, connection, prepare and the activity source, each looking up the runtime when it is called and turning a failed lookup into a rejected result. postgres(), sqlite() and supabase() build their ORM runtime, query, execute, prepare, transaction and tx.prepare from it, so a result created before close() fails with the same message as before and the runtime is created at the same moment. AsyncIterableResult.rejected replaces the sql-runtime deferral helpers. prepareQuery takes only what it uses. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
A client member that returns a promise or a result rejects for a failed client or transaction state; a mistake in the call itself still throws at the call. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…ted try blocks, in files that import @prisma/orm Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…e in every facade Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…s and matches only client-shaped receivers Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
@prisma/orm-extension-arktype-json
@prisma/orm-extension-middleware-cache
@prisma/orm-extension-paradedb
@prisma/orm-extension-pgvector
@prisma/orm-extension-postgis
@prisma/orm-extension-supabase
@prisma/orm-family-mongo
@prisma/orm-family-sql
@prisma/orm-framework
@prisma/orm-mongo
@prisma/orm-postgres
@prisma/orm-sqlite
@prisma/orm-target-mongo
@prisma/orm-target-postgres
@prisma/orm-target-sqlite
@prisma/orm-toolchain
commit: |
size-limit report 📦
|
…-closed-client-members-reject Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> # Conflicts: # docs/architecture docs/subsystems/4. Runtime & Middleware Framework.md # packages/3-targets/postgres/9-facade/README.md
When a client is closed, its members now report the failure through the promise or result they return, instead of throwing before they return anything. This holds on every client.
The decision
A member that returns a promise or a lazy result reports a failed state through that value. "Lazy result" means an
AsyncIterableResult, the valuequery(plan)and ORM reads such as.all()return. A failed state is one of three: the client is closed, its database connection failed, or the transaction the call runs in has ended. In each case the call returns normally, and the promise rejects, or the result rejects when it is awaited or read. The error code and message are the same as before.A mistake in the call itself still throws at the call. Two examples are an ORM query that the database's capabilities do not allow, and a prepared statement run on something that is not a runtime, connection or transaction. These are programming errors, found at the point of writing the call, not states the application must handle.
db.runtime()still throws. It returns a runtime, not a promise, so throwing is the only way it can say the client is closed.Where it applies
postgres():transaction,prepare,query,executeand every ORM read and write. These used to throw at the call afterclose().sqlite():prepare,query,executeand every ORM read and write.supabase():prepareon a role-bound client. Its other members already followed the rule.tx.query, and running a prepared statement ontxwithprepared.query(tx, …)orprepared.execute(tx, …).tx.prepareon a transaction still open afterclose()started, on Postgres and SQLite.mongo()already followed the rule. Tests now confirm it.Why
These members are typed to return a promise or a result. Code that handles errors on that value, such as
.catch(handle)orPromise.allSettled([...]), missed the error entirely, and in a server that is shutting down the uncaught exception could crash the process. Code that usedawaitinsidetry/catchcaught it either way, which is why the problem stayed hidden.The cause was the same everywhere: each member looked up its runtime before creating the value it returns, and that lookup throws once the client is closed.
How it is built
Each client now builds its members from one object,
delegatingRuntime(getRuntime)inpackages/2-sql/5-runtime. It looks the runtime up when a member is called and turns a failure of that lookup into a rejected promise or a rejected result.postgres(),sqlite()andsupabase()build their ORM runtime,query,execute,prepare,transactionandtx.preparefrom it, so a member added later follows the rule without extra work. The runtime is still looked up at the call, so when a connection is created, and whenclose()counts a statement as running, is unchanged.A result that must fail when read uses the new
AsyncIterableResult.rejected(error)inframework-components. The prepared-statement bridges on a transaction check that the transaction is still open when the statement runs, inside the returned value.What changes for users
Code that catches these errors with
try/catchbut withoutawaitno longer catches them:Add
await, or handle the returned promise. The upgrade instructions are inupgrade-instructions/pending/closed-client-members-reject/. They find this pattern in files that import an@prisma/orm-*package.Docs
The Shutdown section of
docs/architecture docs/subsystems/4. Runtime & Middleware Framework.md, the Postgres README and theprisma-8skill state the rule once: a failed client or transaction state rejects through the returned value, and a mistake in the call throws.How it was checked
DRIVER.NOT_CONNECTED. Further tests cover a failed connection, an ended transaction,tx.prepareafter close started, error messages unchanged frommain, and a prepared statement given a wrong target still throwing at the call./drive-code-review(system design and code). All acceptance criteria pass.check:upgrade-coverage --mode pr, coverage in every changed package, the upgrade-instruction tests and the integration files that close runtimes, after mergingmain.close(),transaction, ORMallandfirst,query,executeandprepareeach returned and then rejected withDRIVER.NOT_CONNECTED;db.runtime()threw; in a transaction still open while close started, a query ran andtx.preparerejected.Alternatives considered
tx.prepare). One object per client that every member is built from cannot miss one.db.runtime()return a closed runtime instead of throwing. Afterclose()there may be no runtime at all, and creating one would open a pool. A method that returns a value reports failure by throwing.Closes TML-3345, TML-3210.
Agent: raskolnikov-51
Summary by CodeRabbit