Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix: closing db on finalizer and memory leak #177
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
fix: closing db on finalizer and memory leak #177
Changes from 2 commits
034fe55
364d8da
717dbae
e13a431
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Isn't this the same as setting it on the way in? Why not do that so that you don't have to do the lookup each time you set it?
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.
It would have the same effect, but we are only setting this finalizer in the very specific case that a
sqlair.Statement
is executed with arguments that cause different SQL to be generate to previous executions, as can happen with a bulk insert or slice query.This way, we are only setting the finalizer if this conflict has occurred, i.e. we don't set this finalizer on most
driverStmt
s. Setting it on the way in would mean we set it for alldriverStmt
s we create.If we made this change we would avoid the lookup, but we would still have to set a finalizer here on the newly created
driverStmt
We already set a finalizer on the sqlair.Statement when it is created but this could not be replaced with finalizers on the
driverStmt
s because thedriverStmt
s would then never be removed from the cache.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.
It would be safer if this was not a closure.
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.
Technically you set if for all statements -1 i.e. except for the last one to (ever) enter the cache.
It is not unsafe to set it unconditionally (if no one references it, they can't use it), but you drop the lookup. The added simplicity is worth the change.
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 don't see how we set this for all statements -1. This is only set when you have a
sqlair.Statement
that has been executed with new arguments that cause the SQL generated inBindInputs
to come out different to the SQL that was generated for the previous execution. e.g. if you have a bulk insert inserting 3 items instead of 2.In that case, when the bulk insert is run with 3 items, the lookup in
Query
will fail, which will cause thestmtCache.driverPrepareStmt
to be run. When it hits theif
statement that sets the finalizer, it will find the bulk insert sql with 2 arguments, set a finalizer on it, and implicitly evict it by overwriting it below.In the case of a
sqlair.Statement
that never changes the SQL it generatesdriverPrepareStmt
will only be run once (as the lookup will always subsequently succeed), and when it is theif
statement that conditionally sets the finalizer will fail as the cacheID of thesqlair.Statement
is not yet in the cache.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.
Changed to be a function with a comment, also updated the comment above the setting of this finalizer to make things clearer.