Skip to content

Commit

Permalink
fix memory issue (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney authored Oct 29, 2024
1 parent 365c0b8 commit 588937a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit 588937a

Please sign in to comment.