EscrowService.refund and poolWithdraw bypass withLockedEscrow, so a concurrent refund and release both pass the LOCKED check and both move funds
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 58/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- postgresql, typescript
調査の方向性
Read src/escrow/escrow.service.ts, especially refund, poolWithdraw, and withLockedEscrow, then inspect the related transaction patterns in release and releasePartial. Check the callers mentioned in src/milestones/milestones.service.ts and src/escrow/escrow.service.spec.ts. Done means both fund moving methods serialize on the escrow row and save their ledger changes through the transaction manager, with tests covering those behaviors.
索引モデルが issue の本文から書いたものです。
説明
Location
src/escrow/escrow.service.ts:440—EscrowService.refundsrc/escrow/escrow.service.ts:407—EscrowService.poolWithdraw- compare
src/escrow/escrow.service.ts:484—withLockedEscrow
Problem
The pessimistic row lock added for #303 was applied to three of the five fund-moving methods. release, splitRelease and releasePartial all route through withLockedEscrow, which takes SELECT ... FOR UPDATE on the escrow row and asserts the LOCKED precondition under that lock:
const escrow = await mgr.findOne(Escrow, {
where: { id: escrowId },
lock: { mode: 'pessimistic_write' },
});
if (!escrow) throw new NotFoundException(...);
this.assertLocked(escrow);
return fn(escrow, mgr);
refund and poolWithdraw do not. Both still use the unlocked read-then-check-then-write pattern the lock was introduced to eliminate:
async refund(escrowId: string): Promise<Escrow> {
const escrow = await this.getOrThrow(escrowId); // plain findOne, no lock
this.assertLocked(escrow); // check on an unlocked read
const result = await this.invokeOnLockedEscrow(escrow, 'refund', () => ...);
escrow.status = EscrowStatus.REFUNDED;
...
return this.escrowRepo.save(escrow); // save() on a possibly-stale entity
}
getOrThrow (line 463) is a bare escrowRepo.findOne({ where: { id } }) with no lock option and no transaction.
Why it matters
The lock in withLockedEscrow only serialises callers that take it. A refund holds no lock, so it is invisible to a concurrent release and vice versa:
release(E)opens its transaction, locks rowE, readsstatus = LOCKED, and begins its on-chainreleaseinvocation. The row lock is held until that transaction commits — and it spans the whole Soroban round trip, whichIdempotencyInterceptor's 90s stale window (#90) tells us can take tens of seconds.- Meanwhile
refund(E)reads rowEwithout the lock.SELECTwithoutFOR UPDATEis not blocked by another transaction's row lock in Postgres under the default READ COMMITTED isolation — it reads the last committed version, which still saysLOCKED. assertLockedpasses.refundinvokes the contract'srefundfor the same escrow key.- Both on-chain calls are now in flight for one escrow: the full amount to the recipient and the full amount back to the funder.
Then the ledger diverges regardless of which on-chain call wins:
refundfinishes withthis.escrowRepo.save(escrow)on the entity it read beforereleasecommitted. TypeORM'ssaveissues a full-columnUPDATE, so it overwritesrelease'sstatus = RELEASED,releaseTxHashandreleasedAtwith the stale pre-release values plusREFUNDED. ThePaymentrow written byreleasesurvives, so the escrow readsREFUNDEDwhile a confirmed full-amountPaymentexists against it — the exact double-accounting #154 and #303 were meant to close.
On a correctly-implemented contract one of the two on-chain calls would fail, which bounds the loss — but the local ledger is corrupted either way, and in dry-run mode (SorobanClientService.invoke succeeding unconditionally) both "succeed" and the double payout is recorded as real. The same unlocked-read problem applies to poolWithdraw, which additionally runs concurrently with itself by design (a pool stays LOCKED across many rewards) and which MaintenancePoolService.assignReward is the sole caller of.
refund is reachable from three places, so this is not a narrow window: POST /escrow/:id/refund, BountiesService.refund (src/bounties/bounties.service.ts:270), and MilestonesService.close (src/milestones/milestones.service.ts:272). A merged-PR payout racing a sponsor's refund of the same bounty is a realistic sequence, not a contrived one — the bounty state machine permits MERGED -> REFUNDED (src/bounties/bounty-state-machine.ts:32-36).
Reproduction / failure path
- Fund a bounty:
POST /bounties→B,POST /bounties/B/fund→ escrowE,status = LOCKED. - Claim it and merge the PR so
BreachesMERGED. - Fire two requests concurrently (or add an artificial delay inside
soroban.invoketo widen the window):POST /escrow/E/releasewith{ "recipientAddress": "G...C" }POST /escrow/E/refund
- Both return
200.Paymenthas a confirmed row for the full amount toG...C, andE.statusisREFUNDEDwithreleaseTxHash/releasedAtcleared byrefund's stalesave(). - Reversing the order gives the mirror image:
E.status = RELEASEDwith arefundTxHashwritten and then clobbered.
Serialising the two requests produces the correct 400 Escrow E is not in LOCKED state on the second, which confirms the only thing missing is the lock.
Suggested fix
Route both methods through the existing helper, exactly as release does:
async refund(escrowId: string): Promise<Escrow> {
return this.withLockedEscrow(escrowId, async (escrow, manager) => {
const result = await this.invokeOnLockedEscrow(
escrow, 'refund',
() => this.soroban.invoke('refund', [u64(this.onChainKeyFor(escrow))], this.contractOpts(escrow)),
manager,
);
escrow.status = EscrowStatus.REFUNDED;
escrow.refundTxHash = result.txHash;
escrow.refundedAt = new Date();
return manager.save(Escrow, escrow);
});
}
and for poolWithdraw, take the lock and write the Payment through the transaction manager (manager.save(Payment, ...)) instead of this.paymentRepo.save(...), so the withdraw and its ledger row are one atomic span.
Add an optional manager?: EntityManager passthrough on refund so MilestonesService.close can bring it into its own transaction, mirroring releasePartial's signature.
Tests to add in src/escrow/escrow.service.spec.ts: assert that refund and poolWithdraw call findOne with lock: { mode: 'pessimistic_write' } (the release specs already assert this shape), and that both write through the transaction manager rather than the bare repository.
- 主要言語
- TypeScript
- スター
- 2
- フォーク
- 90
- 平均マージ
- 31分
- マージ済み PR(30日)
- 20
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
MergeFi/backend のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
bug security
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
bug testing
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
bug security
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
MergeFi/backend の issue をすべて見る
似ている issue
-
Codex使用可 コード
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ryuki-moro/Nicomaru#218 ·
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
sveltejs/svelte-eslint-parser#1008 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
silverbucket/inbox-rs#351 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: The H2 series' and the H3C's fan 3 is labelled "Chamber Fan", Bambu calls it the exhaust fanオープンarea:ui
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
bug front P2
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
invoicerr-app/invoicerr#964 ·
メンテナーはふだん 1 日以内に返信