Skip to content

Conversation

@ubik2
Copy link
Contributor

@ubik2 ubik2 commented Oct 30, 2025

This changes the places where we use prepared statements to properly dispose of them.
A future improvement is to only prepare the EXPORT statement once, and attach it to the Session or store.
I also converted the selectFacts to not use a generator, since it isn't needed there, and complicates the statement disposal.


Summary by cubic

Finalize all prepared statements to prevent resource leaks and improve stability in memory store operations. Also change selectFacts to return an array so statements can be properly finalized.

  • Bug Fixes

    • Wrap EXPORT, CAUSE_CHAIN, and GET_FACT usage in try/finally with stmt.finalize().
    • Finalize the EXPORT statement in commit after get().
  • Refactors

    • Replace selectFacts generator with an array return to support safe finalization.

Written for commit f3d9ad7. Summary will update automatically on new commits.

This changes those to properly dispose of them.
A future improvement is to only prepare the EXPORT statement once, and attach it to the Session or store.
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="packages/memory/space.ts">

<violation number="1" location="packages/memory/space.ts:788">
Wrap this statement in a try/finally so stmt.finalize() always runs even if stmt.get throws; otherwise a failing query will leak the prepared statement.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

const of = transaction.sub;
const row = session.store.prepare(EXPORT).get({ the, of }) as
const stmt = session.store.prepare(EXPORT);
const row = stmt.get({ the, of }) as
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap this statement in a try/finally so stmt.finalize() always runs even if stmt.get throws; otherwise a failing query will leak the prepared statement.

Prompt for AI agents
Address the following comment on packages/memory/space.ts at line 788:

<comment>Wrap this statement in a try/finally so stmt.finalize() always runs even if stmt.get throws; otherwise a failing query will leak the prepared statement.</comment>

<file context>
@@ -763,9 +784,11 @@ const commit = &lt;Space extends MemorySpace&gt;(
   const of = transaction.sub;
-  const row = session.store.prepare(EXPORT).get({ the, of }) as
+  const stmt = session.store.prepare(EXPORT);
+  const row = stmt.get({ the, of }) as
     | StateRow
     | undefined;
</file context>

✅ Addressed in f3d9ad7

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a PR: #1991 Always finalize prepared statements by adding try/finally (merges into #1990)

Review and merge it to apply the changes.

@ubik2 ubik2 changed the title Our existing code created prepared statements without finalizing them. Finalize prepared statements Oct 30, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@ubik2 ubik2 requested a review from ellyxir October 30, 2025 22:33
Copy link
Contributor

@ellyxir ellyxir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks so much better, thanks! and i like getting rid of the generator even though i like generators, good call

@ubik2 ubik2 merged commit 9122b33 into main Oct 31, 2025
9 checks passed
@ubik2 ubik2 deleted the robin/stmt-cleanup branch October 31, 2025 17:39
jkomoros pushed a commit that referenced this pull request Nov 2, 2025
* Our existing code created prepared statements without finalizing them.
This changes those to properly dispose of them.
A future improvement is to only prepare the EXPORT statement once, and attach it to the Session or store.

* Removed comment about finalize, since it's no longer applicable now that I'm not using a generator function.

* better finally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants