Repository navigation
Make JavaScript SDK state writes reliable - #936
Conversation
|
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. |
There was a problem hiding this comment.
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.
| 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); | ||
| }); |
There was a problem hiding this comment.
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);
});There was a problem hiding this comment.
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.
43a0ec6 to
eaf7419
Compare
eaf7419 to
d9d8e3a
Compare
| ); | ||
| opened.onsuccess(); | ||
| await queued; | ||
| console.log("CONTROL OK"); |
There was a problem hiding this comment.
should we remove these logs? there are 3 of them
There was a problem hiding this comment.
Removed all three in 1960c5b; they were leftovers from writing the tests.
The SDK uses the state store to persist its identity and member credential between runs. This change addresses three failure modes in that store.
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.