Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,7 @@ BaseObjectPtr<SQLTagStore> SQLTagStore::Create(
.ToLocal(&obj)) {
return nullptr;
}
obj->SetInternalField(kDatabaseObject, database->object());
return MakeBaseObject<SQLTagStore>(env, obj, std::move(database), capacity);
}

Expand All @@ -2728,9 +2729,8 @@ void SQLTagStore::CapacityGetter(const FunctionCallbackInfo<Value>& args) {
}

void SQLTagStore::DatabaseGetter(const FunctionCallbackInfo<Value>& args) {
SQLTagStore* store;
ASSIGN_OR_RETURN_UNWRAP(&store, args.This());
args.GetReturnValue().Set(store->database_->object());
args.GetReturnValue().Set(
args.This()->GetInternalField(kDatabaseObject).As<Value>());
}

void SQLTagStore::SizeGetter(const FunctionCallbackInfo<Value>& args) {
Expand Down
5 changes: 5 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ class Session : public BaseObject {

class SQLTagStore : public BaseObject {
public:
enum InternalFields {
kDatabaseObject = BaseObject::kInternalFieldCount,
kInternalFieldCount
};

SQLTagStore(Environment* env,
v8::Local<v8::Object> object,
BaseObjectWeakPtr<DatabaseSync> database,
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-sqlite-template-tag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
// Flags: --expose-gc

const { skipIfSQLiteMissing } = require('../common');
skipIfSQLiteMissing();

Expand Down Expand Up @@ -124,3 +126,35 @@ test('sql error messages are descriptive', () => {
message: /no such table/i,
});
});

test('a tag store keeps the database alive by itself', () => {
const sql = new DatabaseSync(':memory:').createTagStore();

sql.db.exec('CREATE TABLE test (data INTEGER)');

global.gc();

// eslint-disable-next-line no-unused-expressions
sql.run`INSERT INTO test (data) VALUES (1)`;
});

test('tag store prevents circular reference leaks', async () => {
const { gcUntil } = require('../common/gc');

const before = process.memoryUsage().heapUsed;

// Create many SQLTagStore + DatabaseSync pairs with circular references
for (let i = 0; i < 1000; i++) {
const sql = new DatabaseSync(':memory:').createTagStore();
sql.db.exec('CREATE TABLE test (data INTEGER)');
// eslint-disable-next-line no-void
sql.db.setAuthorizer(() => void sql.db);
}

// GC until memory stabilizes or give up after 20 attempts
await gcUntil('tag store leak check', () => {
const after = process.memoryUsage().heapUsed;
// Memory should not grow significantly (allow 50% margin for noise)
return after < before * 1.5;
}, 20);
});
Loading