-
Notifications
You must be signed in to change notification settings - Fork 353
[HMA] Add explicit connection close for signal ops and clear temp refs #1968
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
Merged
Dcallies
merged 1 commit into
facebook:main
from
juanmrad:hma/add-explicit-memory-clear-for-db-and-index-refs
Apr 15, 2026
+60
−42
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -495,28 +495,34 @@ def commit_signal_index( | |
| store_start_time = time.time() | ||
| # Deep dark magic - direct access postgres large object API | ||
| raw_conn = db.engine.raw_connection() | ||
| l_obj = raw_conn.lobject(0, "wb", 0, tmpfile.name) | ||
| self._log( | ||
| "imported tmpfile as lobject oid %d took %s", | ||
| l_obj.oid, | ||
| duration_to_human_str(time.time() - store_start_time), | ||
| ) | ||
| if self.serialized_index_large_object_oid is not None: | ||
| if self.index_lobj_exists(): | ||
| old_obj = raw_conn.lobject(self.serialized_index_large_object_oid, "n") | ||
| self._log("deallocating old lobject %d", old_obj.oid) | ||
| old_obj.unlink() | ||
| else: | ||
| self._log( | ||
| "old lobject %d doesn't exist? " | ||
| + "This might be a previous partial failure", | ||
| self.serialized_index_large_object_oid, | ||
| level=logging.WARNING, | ||
| ) | ||
|
|
||
| self.serialized_index_large_object_oid = l_obj.oid | ||
| db.session.add(self) | ||
| raw_conn.commit() | ||
| try: | ||
| l_obj = raw_conn.lobject(0, "wb", 0, tmpfile.name) | ||
| self._log( | ||
| "imported tmpfile as lobject oid %d took %s", | ||
| l_obj.oid, | ||
| duration_to_human_str(time.time() - store_start_time), | ||
| ) | ||
| if self.serialized_index_large_object_oid is not None: | ||
| if self.index_lobj_exists(): | ||
| old_obj = raw_conn.lobject( | ||
| self.serialized_index_large_object_oid, "n" | ||
| ) | ||
| self._log("deallocating old lobject %d", old_obj.oid) | ||
| old_obj.unlink() | ||
| else: | ||
| self._log( | ||
| "old lobject %d doesn't exist? " | ||
| + "This might be a previous partial failure", | ||
| self.serialized_index_large_object_oid, | ||
| level=logging.WARNING, | ||
| ) | ||
|
|
||
| self.serialized_index_large_object_oid = l_obj.oid | ||
| db.session.add(self) | ||
| raw_conn.commit() | ||
| finally: | ||
| # explicitly close the raw connection to free memory | ||
| raw_conn.close() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might work too: https://docs.python.org/3/library/contextlib.html#contextlib.closing which lets you use a with statement. Reading https://docs.sqlalchemy.org/en/21/core/connections.html#working-with-the-dbapi-cursor-directly I think this is very likely missing the close |
||
|
|
||
| self._log( | ||
| "commited new index, %d signals %s took %s", | ||
|
|
@@ -546,28 +552,35 @@ def load_signal_index(self) -> SignalTypeIndex[int]: | |
| # I'm sorry future debugger finding this comment. | ||
| load_start_time = time.time() | ||
| raw_conn = db.engine.raw_connection() | ||
| l_obj = raw_conn.lobject(oid, "rb") | ||
| try: | ||
| l_obj = raw_conn.lobject(oid, "rb") | ||
|
|
||
| with tempfile.NamedTemporaryFile("rb") as tmpfile: | ||
| self._log("importing lobject oid %d to tmpfile %s", l_obj.oid, tmpfile.name) | ||
| l_obj.export(tmpfile.name) | ||
| tmpfile.seek(0, io.SEEK_END) | ||
| self._log( | ||
| "downloading %s to tmpfile took %s", | ||
| _human_friendly_bytesize(tmpfile.tell()), | ||
| duration_to_human_str(time.time() - load_start_time), | ||
| ) | ||
| tmpfile.seek(0) | ||
| with tempfile.NamedTemporaryFile("rb") as tmpfile: | ||
| self._log( | ||
| "importing lobject oid %d to tmpfile %s", l_obj.oid, tmpfile.name | ||
| ) | ||
| l_obj.export(tmpfile.name) | ||
| tmpfile.seek(0, io.SEEK_END) | ||
| self._log( | ||
| "downloading %s to tmpfile took %s", | ||
| _human_friendly_bytesize(tmpfile.tell()), | ||
| duration_to_human_str(time.time() - load_start_time), | ||
| ) | ||
| tmpfile.seek(0) | ||
|
|
||
| deserialize_start = time.time() | ||
| index = t.cast( | ||
| SignalTypeIndex[int], | ||
| SignalTypeIndex.deserialize(t.cast(t.BinaryIO, tmpfile.file)), | ||
| ) | ||
| self._log( | ||
| "deserialize took %s", | ||
| duration_to_human_str(time.time() - deserialize_start), | ||
| ) | ||
| finally: | ||
| # explicitly close the raw connection to free memory | ||
| raw_conn.close() | ||
|
|
||
| deserialize_start = time.time() | ||
| index = t.cast( | ||
| SignalTypeIndex[int], | ||
| SignalTypeIndex.deserialize(t.cast(t.BinaryIO, tmpfile.file)), | ||
| ) | ||
| self._log( | ||
| "deserialize took %s", | ||
| duration_to_human_str(time.time() - deserialize_start), | ||
| ) | ||
| self._log( | ||
| "loading signal index took %s", | ||
| duration_to_human_str(time.time() - load_start_time), | ||
|
|
||
Oops, something went wrong.
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.
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'm very suspicious this does anything, because it requires that the local variable is not properly garbage collected when it leaves scope, which stretches my understanding about how refcounting works.
Do we have any evidence that this is needed?
Random googling: