-
-
Couldn't load subscription status.
- Fork 33.6k
sqlite: fix segfault SQLTagStore when db handle is garbage collected #60462
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
base: main
Are you sure you want to change the base?
Conversation
|
Review requested:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple comments, but LGTM.
| static BaseObjectPtr<StatementSync> PrepareStatement( | ||
| const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| BaseObjectWeakPtr<DatabaseSync> database_; | ||
| BaseObjectPtr<DatabaseSync> database_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the DatabaseSync object somehow refers back to the SQLTagStore object, this creates a memory leak.
Would it make sense to keep storing a BaseObjectWeakPtr but to keep it alive by also storing the associated JS object in an internal field of SQLTagStore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the DatabaseSync object somehow refers back to the SQLTagStore object, this creates a memory leak.
We're good. DatabaseSync holds no reference to SQLTagStore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geeksilva97 Why do we know that that's true?
DatabaseSync has an associated JS object and can refers to other JS objects (e.g. an authorizer callback) which in turn can refer to the SQLTagStore – what am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure I got it.
Are you referring to a circular dependency? If so, that's the part I'm unable to visualize. You mean like in JS-land, if one has an authorizer callback holding an sqltagstore in a closure or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not able to reproduce this scenario.
I don't know a lot about V8's garbage collector, but it seems to be able to do its job here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you referring to a circular dependency?
Yes, but this dependency here (the one represented by the BaseObjectPtr) isn't visible to GC.
You mean like in JS-land, if one has an authorizer callback holding an sqltagstore in a closure or something?
Yup!
I was not able to reproduce this scenario.
Just going to quote myself here:
DatabaseSynchas an associated JS object and can refers to other JS objects (e.g. an authorizer callback) which in turn can refer to the SQLTagStore – what am I missing?
Turning this into runnable code is pretty straightforward:
while (true) {
const sql = new DatabaseSync(':memory:').createTagStore();
sql.db.exec('CREATE TABLE test (data INTEGER)');
sql.db.setAuthorizer(() => void sql.db);
console.log(process.memoryUsage())
}before this PR this crashes, now this leaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof. Good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification, @addaleax
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #60462 +/- ##
==========================================
- Coverage 88.58% 88.58% -0.01%
==========================================
Files 704 704
Lines 207826 207826
Branches 40049 40043 -6
==========================================
- Hits 184112 184107 -5
- Misses 15757 15764 +7
+ Partials 7957 7955 -2
🚀 New features to boost your workflow:
|
Fixes #60448 by changing from a
BaseObjectWeakPtrto aBaseObjectPtr.A SQLTagStore should keep the database alive.