denodrivers / sqlite3

The fastest and correct SQLite3 module for Deno runtime
https://jsr.io/@db/sqlite
Apache License 2.0
265 stars 22 forks source link

Segfault after close() (regression from 0.6.1) #78

Closed nsf closed 1 year ago

nsf commented 1 year ago

I've noticed the lib started to segfault after version 0.6.1 somewhere. My guess is that it's due to finalization code here: https://github.com/denodrivers/sqlite3/blob/a1c7e4d276ae48e6aae1282a47c8e2d523af79c5/src/database.ts#L652-L657 Because it doesn't unregister global finalizers as it does here: https://github.com/denodrivers/sqlite3/blob/a1c7e4d276ae48e6aae1282a47c8e2d523af79c5/src/statement.ts#L649-L655

A rough code sample that causes it:

import { Database } from "https://deno.land/x/sqlite3@0.7.1/mod.ts";

const db = new Database(":memory:");
db.exec(`
  CREATE TABLE foo (
    id INTEGER NOT NULL
  );
`);

const s = db.prepare("INSERT INTO foo (id) VALUES (?)");
for (let i = 0; i < 1000000; i++) {
  s.run(i);
}

db.close();

const a: number[] = [];
for (let i = 0; i < 1000000; i++) {
  a.push(i);
}

console.log(a.reduce((s, v) => s + v, 0));

I actually don't know why it happens, but feels finalizer related. The random array code chunk at the end is important as it creates pressure on the GC.

Most likely "double free" happens somewhere.

Can somebody reproduce it?

DjDeveloperr commented 1 year ago

Could reproduce this on macOS however not on Windows. Anyway, the issue will be fixed in next patch release (0.7.2)

nsf commented 1 year ago

Thanks.