Skip to content

Commit 22684f8

Browse files
kaizhu256kai.zhu
andauthored
- fix bug where worker tries to close database twice if you sequentially apply commands "close" and "open" (#379)
Co-authored-by: kai.zhu <[email protected]>
1 parent 3f9e99a commit 22684f8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
883883
* memory consumption will grow forever
884884
*/
885885
Database.prototype["close"] = function close() {
886+
// do nothing if db is null or already closed
887+
if (this.db === null) {
888+
return;
889+
}
886890
Object.values(this.statements).forEach(function each(stmt) {
887891
stmt["free"]();
888892
});

src/worker.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ function onModuleReady(SQL) {
6363
return postMessage(result);
6464
}
6565
case "close":
66-
return db && db.close();
66+
if (db) {
67+
db.close();
68+
}
69+
return postMessage({
70+
id: data["id"]
71+
});
6772
default:
6873
throw new Error("Invalid action : " + (data && data["action"]));
6974
}

test/test_worker.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ exports.test = async function test(SQL, assert) {
9191
var actual = "";
9292
for (let i = 0; i < header.length; i++) actual += String.fromCharCode(data.buffer[i]);
9393
assert.equal(actual, header, 'Data returned is an SQLite database file');
94+
95+
// test worker properly opens db after closing
96+
await worker.postMessage({ action: "close" });
97+
await worker.postMessage({ action: "open" });
98+
data = await worker.postMessage({ action: "exec", sql: "SELECT 1" });
99+
assert.deepEqual(data.results, [{"columns":["1"],"values":[[1]]}]);
94100
}
95101

96102
function obj2array(obj) {

0 commit comments

Comments
 (0)