Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-cups-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@journeyapps/wa-sqlite': patch
---

Fix bug where table change update notifications would access invalid memory locations under certain conditions.
9 changes: 7 additions & 2 deletions src/sqlite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const async = true;

const onTableChangeCallbacks = {};
globalThis.__onTablesChanged = function(db, opType, tableName, rowId) {
setTimeout(() => onTableChangeCallbacks[db]?.(opType, tableName, rowId), 0);
onTableChangeCallbacks[db]?.(opType, tableName, rowId);
};

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

return callback(opType, tableName, rowId);
/**
* Call the callback inside a setTimeout to avoid blocking SQLite.
* We use a setTimeout only after fetching data from the heap to avoid
* accessing memory which has been freed.
*/
setTimeout(() => callback(opType, tableName, rowId), 0)
};
};

Expand Down
Loading