Skip to content

Fix SqlResolver batching outside requests into a concurrent transaction - #9928

Closed
Marve10s wants to merge 10 commits into
Effect-TS:mainfrom
Marve10s:fix/sql-resolver-transaction-key
Closed

Marve10s wants to merge 10 commits into
Effect-TS:mainfrom
Marve10s:fix/sql-resolver-transaction-key

Conversation

@Marve10s

@Marve10s Marve10s commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

SqlResolver can run a write that was issued outside a transaction on a concurrent transaction's connection, and the write is lost when that transaction rolls back even though the caller saw it succeed:

// Writes is a layer-provided service wrapping a SqlResolver.void insert and sql.withTransaction;
// its callers do not have SqlClient in their own context
yield* Effect.all([
  Effect.exit(writes.withTransaction(Effect.andThen(writes.insert(1), Effect.fail("rollback")))),
  Effect.exit(writes.insert(2))
], { concurrency: "unbounded" })
// insert(2) succeeds, batches: [[1, 2]], persisted rows: []

The module documentation promises that batches are kept apart by the active transaction connection. All four resolver variants (ordered, grouped, findById, void) share the same key function, so all are affected.

transactionKey found the active transaction only through the generic SqlClient service in the request's context. Callers of a layer-provided repository, or of a client built with make, often do not carry that service, or carry a different client. The key was then undefined even though makeWithTransaction had placed the client's own transaction service in the context, so both requests shared one batch that ran with the first entry's context. #9568 (for #9553) deliberately left SQL transaction grouping unchanged, so this case remained.

makeWithTransaction now also records its transaction service in an internal CurrentTransactionService reference, next to the transaction level. transactionKey still reads the generic SqlClient's transaction first, and reads the innermost makeWithTransaction transaction through that reference. When only one is active, it is the key, so the usual same-client case keeps the key it had before. When two different transactions are active, the key is the pair, which only splits batches further. Because the reference holds the service rather than the level, removing the transaction service from a context (afterCommit callbacks, or Context.omit) also removes the fallback. The reference lives in internal/, so there is no new public API.

Known limitation: clients that implement withTransaction themselves instead of using makeWithTransaction, such as @effect/sql-sqlite-do, never set the reference. Their transactions are only recognized when that client is the SqlClient in the caller's context. This is documented on SqlResolver, SqlClient.withTransaction and makeWithTransaction, and the sqlite-do fix is tracked separately.

The regression tests in packages/sql/sqlite-node/test/Resolver.test.ts cover a caller whose SqlClient is another client without a transaction, a caller inside another client's transaction, and a request that removed the transaction service from its context.

Closes EFF-2929

@changeset-bot

changeset-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c306b30

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.28 KB 39.28 KB 0.00 KB (0.00%)
basic.ts 7.15 KB 7.15 KB 0.00 KB (0.00%)
batching.ts 11.03 KB 11.03 KB 0.00 KB (0.00%)
brand.ts 6.83 KB 6.83 KB 0.00 KB (0.00%)
cache.ts 11.46 KB 11.46 KB 0.00 KB (0.00%)
config.ts 22.70 KB 22.70 KB 0.00 KB (0.00%)
differ.ts 21.83 KB 21.83 KB 0.00 KB (0.00%)
http-client.ts 26.26 KB 26.26 KB 0.00 KB (0.00%)
http-router.ts 36.57 KB 36.57 KB 0.00 KB (0.00%)
logger.ts 11.84 KB 11.84 KB 0.00 KB (0.00%)
metric.ts 9.38 KB 9.38 KB 0.00 KB (0.00%)
optic.ts 7.04 KB 7.04 KB 0.00 KB (0.00%)
pubsub.ts 15.91 KB 15.91 KB 0.00 KB (0.00%)
queue.ts 12.70 KB 12.70 KB 0.00 KB (0.00%)
schedule.ts 11.71 KB 11.71 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 13.95 KB 13.95 KB 0.00 KB (0.00%)
schema-binary.ts 40.89 KB 40.89 KB 0.00 KB (0.00%)
schema-class.ts 21.48 KB 21.48 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.45 KB 29.45 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.83 KB 27.83 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.20 KB 15.20 KB 0.00 KB (0.00%)
schema-string.ts 12.20 KB 12.20 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.14 KB 16.14 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.80 KB 38.80 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.01 KB 26.01 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.76 KB 20.76 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.90 KB 20.90 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.00 KB 21.00 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.03 KB 26.03 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.08 KB 21.08 KB 0.00 KB (0.00%)
schema.ts 20.68 KB 20.68 KB 0.00 KB (0.00%)
stm.ts 14.04 KB 14.04 KB 0.00 KB (0.00%)
stream.ts 10.26 KB 10.26 KB 0.00 KB (0.00%)

This branch is waiting to be deployed

1 waiting deployment
fork — c306b30b Waiting Oct 25, 2026 by effect-bot via approval-gate #23229
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants