-
-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
db::serialize() crashing with Electron #981
Comments
I cannot reproduce this with better-sqlite3 8.2.0 and Electron 23.2.1 with the following main.js: const Database = require('better-sqlite3');
const db = new Database(':memory:');
console.log(db.serialize()); What is my repro missing? Does it work with Electron <= 20 for you? If so, then this is possibly caused by the memory cage, which I was hoping better-sqlite3 was not affected by: https://www.electronjs.org/blog/v8-memory-cage So while Blobs are already using the correct APIs and copy the buffer (instead of passing SQLite's memory directly to Node.js),
This is an entirely different sandbox concept. The error message comes from V8. The option you're setting affects renderer sandboxing. But you're using better-sqlite3 in the main script. |
better-sqlite3/src/objects/database.lzz Lines 285 to 294 in 37b7714
@JoshuaWise do you agree with ChatGPT? |
Never mind, my repro above created a Buffer of size 0, this repros: const Database = require('better-sqlite3');
const db = new Database(':memory:');
db.exec('CREATE TABLE foo (id)');
console.log(db.serialize());
|
Also see electron/electron#35801 (electron/electron#35801 (comment)), because you're probably thinking "Great, now we need twice the memory in Node.js as well just to fix this for Electron" |
I have implemented something similar for |
Sorry, I forgot to mention my versions:
|
Maybe another solution would be to use unsigned char* data = sqlite3_serialize(db->db_handle, *attached_name, &length, SQLITE_SERIALIZE_NOCOPY);
if (data != nullptr) {
info.GetReturnValue().Set(
node::Buffer::Copy(isolate, reinterpret_cast<char*>(data), length).ToLocalChecked()
);
} else {
// Need to copy from SQLite3.
data = sqlite3_serialize(db->db_handle, *attached_name, &length, 0);
if (data == nullptr) {
ThrowError("Out of memory");
return;
}
std::unique_ptr<unsigned char, decltype(&sqlite3_free)> data_freer(data, &sqlite3_free);
auto buffer = node::Buffer::NewOrCopy(isolate, reinterpret_cast<char*>(data), length, &FreeSerialization);
// Ownership was transferred. Node is now responsible to free this memory.
if (buffer.data() == reinterpret_cast<char*>(data)) {
data_freer.release();
}
info.GetReturnValue().Set(std::move(buffer).ToLocalChecked());
} |
As this is an issue with Electron and not Node.js, I won't consider this a bug in |
This is happening ti me just now. I'm trying to run this sample code: import Database from "better-sqlite3";
import { join } from "path";
export const initDatabase = (dbDir: string): void => {
const dbPath = join(dbDir, "BlackBoxDB");
const db = new Database(dbPath);
const dbBuffer = db.serialize();
console.log(dbBuffer.toString());
db.close();
}; With: Does anyone have/know a fix for this? |
I am trying to use the library with Electron. Whenever I try to call
db.serialize()
the application crashes with the following error message:This problem persist with the sandbox option enabled or disabled in Electron, e.g.:
I can use the database normally (insert or read values), but it crashes when using
serialize()
.The text was updated successfully, but these errors were encountered: