Skip to content

Commit 588937a

Browse files
fix memory issue (#17)
1 parent 365c0b8 commit 588937a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

.changeset/stale-cups-grow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@journeyapps/wa-sqlite': patch
3+
---
4+
5+
Fix bug where table change update notifications would access invalid memory locations under certain conditions.

src/sqlite-api.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const async = true;
2727

2828
const onTableChangeCallbacks = {};
2929
globalThis.__onTablesChanged = function(db, opType, tableName, rowId) {
30-
setTimeout(() => onTableChangeCallbacks[db]?.(opType, tableName, rowId), 0);
30+
onTableChangeCallbacks[db]?.(opType, tableName, rowId);
3131
};
3232

3333
/**
@@ -569,7 +569,12 @@ export function Factory(Module) {
569569
const stringBytes = new Uint8Array(Module.HEAPU8.buffer, tableNamePtr, length);
570570
const tableName = new TextDecoder().decode(stringBytes);
571571

572-
return callback(opType, tableName, rowId);
572+
/**
573+
* Call the callback inside a setTimeout to avoid blocking SQLite.
574+
* We use a setTimeout only after fetching data from the heap to avoid
575+
* accessing memory which has been freed.
576+
*/
577+
setTimeout(() => callback(opType, tableName, rowId), 0)
573578
};
574579
};
575580

0 commit comments

Comments
 (0)