GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sqlite: prevent reentrant session.close() · nodejs/node@0a6f2ee

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • sqlite.md
  • src
    • node_sqlite.cc
    • node_sqlite.h
  • test/parallel
    • test-sqlite-session.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/sqlite.md‎

Copy file name to clipboardExpand all lines: doc/api/sqlite.md
+2-1Lines changed: 2 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,8 @@ wrapper around [`sqlite3session_patchset()`][].
947947

948948
### `session.close()`
949949

950-
Closes the session. An exception is thrown if the database or the session is not open. This method is a
950+
Closes the session. An exception is thrown if the database or the session is not open,
951+
or if the session is currently generating a changeset or patchset. This method is a
951952
wrapper around [`sqlite3session_delete()`][].
952953

953954
### `session[Symbol.dispose]()`
Collapse file

‎src/node_sqlite.cc‎

Copy file name to clipboardExpand all lines: src/node_sqlite.cc
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,6 +3921,10 @@ void Session::Changeset(const FunctionCallbackInfo<Value>& args) {
39213921
THROW_AND_RETURN_ON_BAD_STATE(
39223922
env, session->session_ == nullptr, "session is not open");
39233923

3924+
session->is_generating_changeset_ = true;
3925+
auto changeset_guard =
3926+
OnScopeLeave([&] { session->is_generating_changeset_ = false; });
3927+
39243928
int nChangeset;
39253929
void* pChangeset;
39263930
int r = sqliteChangesetFunc(session->session_, &nChangeset, &pChangeset);
@@ -3946,6 +3950,8 @@ void Session::Close(const FunctionCallbackInfo<Value>& args) {
39463950
env, !session->database_->IsOpen(), "database is not open");
39473951
THROW_AND_RETURN_ON_BAD_STATE(
39483952
env, session->session_ == nullptr, "session is not open");
3953+
THROW_AND_RETURN_ON_BAD_STATE(
3954+
env, session->is_generating_changeset_, "session is currently in use");
39493955

39503956
session->Delete();
39513957
}
Collapse file

‎src/node_sqlite.h‎

Copy file name to clipboardExpand all lines: src/node_sqlite.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ class Session : public BaseObject {
361361
void Delete();
362362
sqlite3_session* session_;
363363
BaseObjectPtr<DatabaseSync> database_; // The Parent Database
364+
bool is_generating_changeset_ = false;
364365

365366
friend class DatabaseSync;
366367
};
Collapse file

‎test/parallel/test-sqlite-session.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-session.js
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,28 @@ test('session.close() - closing twice', (t) => {
605605
});
606606
});
607607

608+
test('session.close() - while generating changes throws exception', (t) => {
609+
for (const method of ['changeset', 'patchset']) {
610+
const database = new DatabaseSync(':memory:');
611+
database.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
612+
613+
const session = database.createSession({ table: 'data' });
614+
database.exec("INSERT INTO data VALUES (1, 'a'), (2, 'b'), (3, 'c')");
615+
database.setAuthorizer(() => {
616+
session.close();
617+
return constants.SQLITE_OK;
618+
});
619+
620+
t.assert.throws(() => session[method](), {
621+
code: 'ERR_INVALID_STATE',
622+
message: 'session is currently in use',
623+
});
624+
625+
database.setAuthorizer(null);
626+
t.assert.notStrictEqual(session[method]().length, 0);
627+
}
628+
});
629+
608630
test('session - keeps its database alive after the db handle is dropped', async (t) => {
609631
const { gcUntil, onGC } = require('../common/gc');
610632

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.