Skip to content

Commit e6ebce4

Browse files
authored
Add IDBFS.onAutoPersistStateChanged callback. Fixes flakiness in browser.test_fs_idbfs_sync_autopersist (#26895)
Add IDBFS.onAutoPersistStateChanged callback that can be used to detect when IDBFS autopersistence operations start and finish. Fixes test failures in test browser.test_test_fs_idbfs_sync_autopersist, by giving the persistence logic time to complete before quitting the first phase of test.
1 parent 36cf010 commit e6ebce4

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ See docs/process.md for more on how version tagging works.
3838
binaryen for wasm). (#26677)
3939
- The `-m64` compiler flag is now honored, and works as an alias for
4040
`-sMEMORY64` and/or `--target=wasm64`. (#26765)
41+
- The autopersistence feature in IDBFS mount now supports registering a global
42+
callback `IDBFS.onAutoPersistStateChanged = active => {}`, which will be
43+
notified of all IDBFS sync start and end events. (#26895)
4144

4245
5.0.7 - 04/30/26
4346
----------------

src/lib/libidbfs.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,31 @@ addToLibrary({
1818
DB_VERSION: 21,
1919
DB_STORE_NAME: 'FILE_DATA',
2020

21+
// When using the autopersistence mechanism, users can set
22+
// IDBFS.onAutoPersistStateChanged callback to receive notification events
23+
// for when persistence operations are in-flight. Use the following syntax:
24+
/*
25+
IDBFS.onAutoPersistStateChanged = autoPersistActive => {
26+
if (autoPersistActive) {
27+
console.log('IDBFS persistence operation has started.');
28+
} else {
29+
console.log('IDBFS persistence operation has finished.');
30+
}
31+
};
32+
*/
33+
2134
// Queues a new VFS -> IDBFS synchronization operation
2235
queuePersist: (mount) => {
2336
function onPersistComplete() {
2437
if (mount.idbPersistState === 'again') startPersist(); // If a new sync request has appeared in between, kick off a new sync
25-
else mount.idbPersistState = 0; // Otherwise reset sync state back to idle to wait for a new sync later
38+
else {
39+
mount.idbPersistState = 0; // Otherwise reset sync state back to idle to wait for a new sync later
40+
IDBFS.onAutoPersistStateChanged?.(false);
41+
}
2642
}
2743
function startPersist() {
2844
mount.idbPersistState = 'idb'; // Mark that we are currently running a sync operation
45+
IDBFS.onAutoPersistStateChanged?.(true);
2946
IDBFS.syncfs(mount, /*populate:*/false, onPersistComplete);
3047
}
3148

test/fs/test_idbfs_sync.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,24 @@ void test() {
152152
#endif
153153

154154
#ifdef IDBFS_AUTO_PERSIST
155+
156+
#if FIRST
157+
// Register an IDBFS autopersist completion callback to detect when the
158+
// filesystem sync operation has finished.
159+
EM_ASM({
160+
IDBFS.onAutoPersistStateChanged = (autoPersistActive) => {
161+
if (autoPersistActive) {
162+
console.log('IDBFS persistence operation has started.');
163+
} else {
164+
console.log('IDBFS persistence operation has finished.');
165+
callUserCallback(_finish);
166+
}
167+
}
168+
});
169+
#else
155170
finish();
171+
#endif
172+
156173
#else
157174
// sync from memory state to persisted and then
158175
// run 'finish'

0 commit comments

Comments
 (0)