Skip to content

Make JavaScript SDK state writes reliable - #936

Merged
aojea merged 4 commits into
google:mainfrom
ersinkoc:audit/sdk-js-state-writes
Nov 28, 2026
Merged

aojea merged 4 commits into
google:mainfrom
ersinkoc:audit/sdk-js-state-writes

Conversation

@ersinkoc

Copy link
Copy Markdown
Contributor

The SDK uses the state store to persist its identity and member credential between runs. This change addresses three failure modes in that store.

  • Resolve browser writes when the IndexedDB transaction commits, and propagate a transaction abort.
  • Give overlapping Node.js writes separate temporary files, and remove a temporary file if publishing fails.
  • Let a subsequent browser operation reopen IndexedDB after an unsuccessful open while sharing an open already in progress.

Each fix has a regression test that failed on the previous behavior. The concurrency tests control the order of transaction completion and file publication.

Validation on Linux with Node.js 22: all 161 SDK tests passed. Type checking, the package build, the examples build, and the browser bundle also passed.

@ersinkoc
ersinkoc requested a review from aojea as a code owner November 24, 2026 21:44
@google-cla

google-cla Bot commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves the robustness of state storage writes in both Node.js and browser environments. In Node.js, state writes now utilize a unique temporary directory via mkdtemp to prevent conflicts during overlapping writes, with proper cleanup in a finally block. In the browser, database open failures are caught to allow subsequent retries, and state writes now correctly await the transaction's completion or abortion. The feedback suggests adding an onerror handler with event.preventDefault() to the IndexedDB transaction to prevent aborted transactions from bubbling up and polluting global error logs.

Comment on lines +53 to +57
await new Promise<void>((resolve, reject) => {
tx.oncomplete = () => resolve();
tx.onabort = () => reject(tx.error ?? new Error("IndexedDB write transaction aborted"));
tx.objectStore(STORE).put(new Uint8Array(data), name);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In IndexedDB, if a request inside a transaction fails, the error event bubbles up to the transaction, then to the database, and finally to the global scope (triggering window.onerror or unhandled rejection handlers) unless preventDefault() is called on the event. To prevent transaction failures from polluting the global error logs or causing test runner failures, it is highly recommended to add an onerror handler to the transaction that calls event.preventDefault().

      await new Promise<void>((resolve, reject) => {
        tx.oncomplete = () => resolve();
        tx.onabort = () => reject(tx.error ?? new Error("IndexedDB write transaction aborted"));
        tx.onerror = (event) => event.preventDefault();
        tx.objectStore(STORE).put(new Uint8Array(data), name);
      });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not take this one: calling preventDefault() on the request's error event changes what the write reports, not only what gets logged.

Per the IndexedDB spec, when a request fails the error event is dispatched with the request, the transaction and the connection in its path; if no listener cancels it, the transaction is aborted. With tx.onerror = (event) => event.preventDefault(), a failed put (for example a QuotaExceededError) no longer aborts the transaction, so it goes on to commit, oncomplete fires, and write() resolves as if the record had been stored. That is the silent lost write this change exists to remove.

As written, a failed put aborts the transaction and onabort rejects write() with tx.error, which is what the caller needs to see. The error event's path also ends at the IDBDatabase; it is not dispatched to window, so there is no global onerror noise to suppress.

@ersinkoc
ersinkoc force-pushed the audit/sdk-js-state-writes branch from eaf7419 to d9d8e3a Compare November 28, 2026 11:38
);
opened.onsuccess();
await queued;
console.log("CONTROL OK");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove these logs? there are 3 of them

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed all three in 1960c5b; they were leftovers from writing the tests.

@aojea
aojea merged commit b782239 into google:main Nov 28, 2026
20 checks passed
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.

2 participants