RTDB: overlapping writes orphan cancelled transactions and leave subsequent transactions stuck

Open
#10,570 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
firebase, node.js, typescript
Domain
databases

Research direction

Start in repoAbortTransactionsOnNode and trace how SENT_NEEDS_ABORT updates lastSent, then read the datastale response cleanup path. Run the deterministic browser regression cases in ChromeHeadless, including the multi-path and repeated-overlap cases. Done means the cancelled transaction settles, cleanup completes, and subsequent transactions use current server data; the issue notes that a proposed fix and regression tests already exist on another branch.

Written by the indexing model from the issue text.

Description

api: database bug
Operating System

Windows x64 (controlled reproduction); also observed in Linux server applications

Environment (if applicable)

Node.js v24.21.0; Firebase Admin standalone RTDB SDK and the self-contained firebase-database-compat.js browser bundle.

Firebase SDK Version

firebase 12.18.0 (@firebase/database 1.1.5); also firebase-admin 14.3.0 (@firebase/database-compat 2.1.4 / @firebase/database 1.1.3). The same defect is present on main.

Firebase SDK Product(s)

Database

Project Tooling

Minimal Node.js scripts using public SDK APIs, with no framework or network/timing hooks. The proposed PR also includes a deterministic core regression running in ChromeHeadless.

Detailed Problem Description

A transaction cancelled by overlapping writes can be removed from the SDK transaction queue before its outstanding request finishes. If the request then receives datastale, its pending local write is never removed and its completion callback never runs. Subsequent transactions repeatedly receive the same stale input and fail with maxretry, even though updated server data arrives. Reconnecting does not clear the orphaned write; creating a fresh app/database instance does.

In repoAbortTransactionsOnNode, the SENT_NEEDS_ABORT branch does not advance lastSent. A second abort pass therefore leaves lastSent at -1 and clears the transaction tree entry. One update() containing two child paths is enough because repoUpdate invokes transaction cancellation once per path. The datastale response handler searches the transaction tree to finish cleanup, but the removed transaction is no longer reachable.

The fix is lastSent = i for SENT_NEEDS_ABORT, retaining the transaction until its outstanding response can clean it up. Public-API live reproduction: original Admin SDK failed 10/10 multi-path trials; the correction passed all 10, with all single-path controls passing. The exact browser UMD bundle failed 5/5 original trials and passed 5/5 corrected trials.

The proposed fix and regression tests are on https://github.com/pkaminski/firebase-js-sdk/tree/codex/firebase-stuck-sdk. All four deterministic browser regression cases fail on the original source and pass with the correction in ChromeHeadless 156. They cover multi-path and repeated overlapping updates with applyLocally enabled and disabled, completion callbacks, listener cleanup, pending writes and subsequent transaction inputs.

Steps and code to reproduce issue

Run with Application Default Credentials and FIREBASE_DATABASE_URL pointing to a disposable RTDB test instance. This script writes and cleans up only its generated test path.

const {initializeApp, applicationDefault, deleteApp} = require('firebase-admin/app');
const {getDatabase} = require('firebase-admin/database');
const {randomUUID} = require('node:crypto');

async function main() {
  const options = {
    credential: applicationDefault(),
    databaseURL: process.env.FIREBASE_DATABASE_URL
  };
  const writer = initializeApp(options, 'writer');
  const victim = initializeApp(options, 'victim');
  const path = 'transaction-cancel-repro/' + randomUUID();
  const source = getDatabase(writer).ref(path);
  const target = getDatabase(victim).ref(path);
  try {
    await source.set({version: 0});
    let firstSettled = false;
    void target.transaction(value => value, undefined, false).then(
      () => { firstSettled = true; },
      () => { firstSettled = true; }
    );
    await target.update({'events/a': 1, 'events/b': 2});
    try {
      await target.transaction(value => value, undefined, false);
    } catch (error) {
      console.log(error.message); // maxretry
    }
    console.log({firstSettled}); // false
    console.log((await source.get()).val()); // correct server data still exists
  } finally {
    await source.remove();
    await Promise.all([deleteApp(writer), deleteApp(victim)]);
  }
}
main().catch(console.error);

Expected: the cancelled transaction settles, and the next transaction uses current server data and completes. Actual: the original callback remains pending and the next transaction retries unchanged input until maxretry. Updating just one child path avoids the bug.

Dominant language
TypeScript
Stars
5.2k
Forks
1k
Avg merge
2d 2h
Merged PRs (30d)
77

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/firebase-js-sdk

All issues in firebase/firebase-js-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.