firestore: BulkWriter.close() never resolves when a write retried after close() belongs to a batch cut for a same-document write
メンテナーはふだん 1 日以内に返信
@cherylEnkidu がすでに取り組んでいます。
2026年9月28日 から。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 72/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- node.js, typescript
調査の方向性
Start in build/src/bulk-writer.js by tracing _sendFn, _scheduleCurrentBatch, and _sendBatch through the provided reproduction, especially the retry after a batch is cut with flush=false. Verify the behavior with the emulator-based script and confirm that retries scheduled after close() or flush() are sent and that close() resolves.
索引モデルが issue の本文から書いたものです。
説明
Environment details
- Package:
@google-cloud/firestore9.2.0 (latest);build/src/bulk-writer.js - Node.js: v24.21.0
- OS: macOS 27.2 arm64
- Backend: Firestore emulator v1.21.0 and v1.22.0 (both reproduce it deterministically)
Summary
BulkWriter.close() (and flush()) never resolves when a write gets a retryable error and it sits in a batch that _sendFn scheduled with flush=false, i.e. the batch that was cut because the next write targets a document already in it.
_sendFn does this when two writes target the same document:
if (this._bulkCommitBatch.has(op.ref)) {
this._scheduleCurrentBatch(); // flush = false
}
Once that batch commits, _sendBatch calls if (flush) this._scheduleCurrentBatch(flush), which is skipped here. The failing op's onError calls sendFn again, which puts the retry into the new current _bulkCommitBatch. Nothing ever schedules that batch:
close()already ran,- the op is not
flushed(only buffered ops getmarkFlushed()), - and its batch was not a flush batch.
_lastOp therefore never settles, and close() hangs forever. The docs for close() promise the opposite: "Any retries scheduled as part of an onWriteError() handler will be run before the close() promise resolves."
With the default error handler the same thing happens for any retryable code. We hit it through ABORTED, where two writes to one document in one BulkWriter raced each other. The repro below uses a custom onWriteError so it is deterministic.
Steps to reproduce
// npm i @google-cloud/firestore
// FIRESTORE_EMULATOR_HOST=127.0.0.1:8080 node bulkwriter-lost-retry.js
const { Firestore } = require('@google-cloud/firestore');
const db = new Firestore({ projectId: `bw-lost-retry-${Date.now()}` });
const t0 = Date.now();
const log = (...a) => console.log(`+${Date.now() - t0}ms`, ...a);
(async () => {
const bw = db.bulkWriter();
// Retry every error up to 3 attempts, NOT_FOUND included.
bw.onWriteError(err => {
log(`write error code=${err.code} attempt=${err.failedAttempts}`);
return err.failedAttempts < 3;
});
const missing = db.doc('c/missing');
const other = db.doc('c/other');
bw.update(missing, { a: 1 }).catch(e => log('update rejected', e.code));
bw.set(other, { a: 1 });
// Second write to `other`: _sendFn schedules the current batch (update +
// first set) with flush=false and starts a new batch for this write.
bw.set(other, { a: 2 });
await bw.close();
log('close() resolved');
process.exit(0);
})();
setTimeout(() => {
log('BUG: close() still pending after 15s; the retried update was never sent');
process.exit(1);
}, 15000);
Actual:
+64ms write error code=5 attempt=1
+15001ms BUG: close() still pending after 15s; the retried update was never sent
Control: remove the second bw.set(other, …), so no batch gets cut. The retries then run and close() resolves:
+64ms write error code=5 attempt=1
+1200ms write error code=5 attempt=2
+2927ms write error code=5 attempt=3
+2927ms update rejected 5
+2927ms close() resolved
With setLogFunction the last line is BulkWriter.errorFn ... shouldRetry: true, and no further request is sent.
Expected
A retry should be sent even when it belongs to a batch scheduled with flush=false, and close() should resolve once it has run, as documented.
A possible fix: have _sendBatch also reschedule the current batch after a commit when close()/flush() has already been called, for example by tracking a "flush requested" flag on the writer instead of relying on each batch's flush argument. Alternatively, _sendFn could schedule immediately when it re-enqueues a retry after a flush was requested.
Real-world impact
A service that writes one document twice in one BulkWriter (set-merge plus a follow-up update) hung forever in close(). Firestore emulator v1.22.0 started returning ABORTED Transaction lock timeout. for one of the two concurrent batchWrite calls (https://github.com/firebase/firebase-tools/issues/11160), and the retry of that ABORTED write was lost as described above. Real Firestore can also return ABORTED on contention, so production code is exposed too.
- 主要言語
- TypeScript
- スター
- 3.2k
- フォーク
- 732
- 平均マージ
- 2日 11時間
- マージ済み PR(30日)
- 99
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
googleapis/google-cloud-node のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
googleapis/google-cloud-node#9854 ·
メンテナーはふだん 1 日以内に返信
-
pubsub: "Operation attempted on ended Span" when a modack is flushed after the message was ackedオープンapi: pubsub
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
googleapis/google-cloud-node#9853 ·
メンテナーはふだん 1 日以内に返信
-
storage: V4 getSignedUrl sorts canonical headers with localeCompare, so they disagree with X-Goog-SignedHeaders対応中かも @itsrayeedscodespace が 9 日前に担当しました。 オープンapi: storage
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
googleapis/google-cloud-node#9840 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
firestore: getPartitions() emits gax AutopaginateTrueWarning in every process対応中かも @TheRealDanii が 12 日前に担当しました。 オープンapi: firestore
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
googleapis/google-cloud-node#9736 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
googleapis/google-cloud-node#9723 ·
メンテナーはふだん 1 日以内に返信
googleapis/google-cloud-node の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
oxc-project/tsrs#1010 ·
メンテナーはふだん 1 日以内に返信
-
area:infra area:storage bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
THU-MAIC/OpenMAIC#2311 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
docs documentation need-triage SaaS
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
enhancement from-app needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
nightly-labs/openbot#10973 ·
メンテナーはふだん 1 日以内に返信
-
status/need-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
google-gemini/gemini-cli#30328 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信