SqlClient: a fiber forked inside a transaction keeps using its connection after the transaction ends

オープン
#10,431 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@effect-bot がすでに取り組んでいます。

2026年11月6日 から。

  • #10433 @jpowersdev による — オープン
  • #10449 @effect-bot による — オープン

評価

この issue はまだ評価されていません。

説明

bug
What version of Effect is running?

effect 4.1.2, @effect/sql-sqlite-bun 4.1.2, Bun 1.4.2

What steps can reproduce the bug?

A fiber forked inside withTransaction outlives it. Its context still holds the TransactionConnection, so its
statements go straight to that connection, without the client's lock. Here its plain INSERT runs inside another
fiber's transaction, which then rolls back: the INSERT reported success and the row is gone.

import { SqliteClient } from "@effect/sql-sqlite-bun"
import * as Deferred from "effect/Deferred"
import * as Effect from "effect/Effect"
import * as Fiber from "effect/Fiber"
import { SqlClient } from "effect/sql"

const program = Effect.gen(function*() {
  const sql = yield* SqlClient.SqlClient
  yield* sql`CREATE TABLE t (who TEXT)`
  const go = yield* Deferred.make<void>()
  const done = yield* Deferred.make<void>()

  // Forked inside a transaction, runs after it has committed.
  const stale = yield* sql.withTransaction(
    Effect.forkChild(Effect.gen(function*() {
      yield* Deferred.await(go)
      yield* sql`INSERT INTO t VALUES ('stale fiber')`
      yield* Deferred.succeed(done, undefined)
    }))
  )

  const other = yield* Effect.forkChild(Effect.ignore(sql.withTransaction(Effect.gen(function*() {
    yield* sql`INSERT INTO t VALUES ('other transaction')`
    yield* Deferred.succeed(go, undefined)
    yield* Deferred.await(done)
    return yield* Effect.fail("rollback")
  }))))

  yield* Fiber.join(stale)
  yield* Fiber.join(other)
  console.log(yield* sql`SELECT who FROM t`)
})

Effect.runPromise(program.pipe(Effect.scoped, Effect.provide(SqliteClient.layer({ filename: ":memory:" }))))
What is the expected behavior?

The stale fiber's statement fails, as a statement on a released pin does in @effect/sql-pg since #9089, or it
takes the connection through the client's lock like any other statement. Either way, [ { who: 'stale fiber' } ]
or an error, never a lost row.

What do you see instead?
[]

The INSERT succeeded and was rolled back with the other fiber's transaction. A nested withTransaction in such
a fiber does the same with a SAVEPOINT: SQLite starts a transaction for it outside the lock, and the next
withTransaction to take the lock fails with cannot start a transaction within a transaction.

Additional information

getConnection and makeWithTransaction in SqlClient.ts take the connection from the TransactionConnection
found in the context, with no check that its transaction is still open. #9084 was the same situation on sql-pg,
fixed in the driver by marking a released pin. The SQLite clients share one connection and have nothing to mark,
so the check would have to live where the connection is taken from the context, as the one afterCommit already
does ("the transaction has already ended").

We hit this in a runtime on Effect Cluster, where a handler forks long-lived fibers inside WithTransaction, and
now fork with Effect.updateContext(effect, Context.omit(sql.transactionService)).

主要言語
TypeScript
スター
16.7k
フォーク
808
平均マージ
12時間 16分
マージ済み PR(30日)
809

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Effect-TS/effect のほかの issue

Effect-TS/effect の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。