sqlite: prevent database close during callbacks · nodejs/node@d4eb6c6 · GitHub
Skip to content

Commit d4eb6c6

Browse files
mcollinaasroyxCySec
authored andcommitted
sqlite: prevent database close during callbacks
Co-authored-by: Asroy Cristian Sitorus <asroycristiansitorus@gmail.com> Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64743 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent cef9085 commit d4eb6c6

3 files changed

Lines changed: 92 additions & 7 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 35 additions & 7 deletions

‎src/node_sqlite.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ class DatabaseSync : public BaseObject {
229229
void SetIgnoreNextSQLiteError(bool ignore);
230230
bool ShouldIgnoreSQLiteError();
231231

232+
void IncrementCallbackDepth() { ++callback_depth_; }
233+
void DecrementCallbackDepth() { --callback_depth_; }
234+
bool IsInCallback() const { return callback_depth_ > 0; }
235+
232236
SET_MEMORY_INFO_NAME(DatabaseSync)
233237
SET_SELF_SIZE(DatabaseSync)
234238

@@ -242,6 +246,7 @@ class DatabaseSync : public BaseObject {
242246
bool enable_load_extension_;
243247
sqlite3* connection_;
244248
bool ignore_next_sqlite_error_;
249+
int callback_depth_ = 0;
245250

246251
std::set<BackupJob*> backups_;
247252
std::unordered_set<Session*> sessions_;
@@ -406,6 +411,19 @@ class SQLTagStore : public BaseObject {
406411
friend class StatementExecutionHelper;
407412
};
408413

414+
class CallbackDepthGuard {
415+
public:
416+
explicit CallbackDepthGuard(DatabaseSync* db) : db_(db) {
417+
db_->IncrementCallbackDepth();
418+
}
419+
~CallbackDepthGuard() { db_->DecrementCallbackDepth(); }
420+
CallbackDepthGuard(const CallbackDepthGuard&) = delete;
421+
CallbackDepthGuard& operator=(const CallbackDepthGuard&) = delete;
422+
423+
private:
424+
DatabaseSync* db_;
425+
};
426+
409427
class UserDefinedFunction {
410428
public:
411429
UserDefinedFunction(Environment* env,
Lines changed: 39 additions & 0 deletions

0 commit comments

Comments
 (0)