move-codex-session leaks database connections when a later open throws #139

Open
opened 2026-10-10 03:57:26 +00:00 by jercik · 0 comments
Owner

At 1a85c77, several functions in move-codex-session.ts open SQLite connections before the try whose finally closes them. If a later open or setup statement throws, the connections opened so far are never closed.

The case from #132 is deleteSourceDatabases. It opens the source state database, then opens the destination connections inside a .map() (lines 1717-1721), all before the try at line 1722. If the second or third destination fails to open, the earlier connections stay open. #132 added the two destination connections; #138 reshaped them into this array.

The same shape is older (#52) in:

  • copyState, copyHistory and copyThreadDatabase: two opens, then ATTACH, CREATE TEMP TABLE and an insert loop, all before the try.
  • deleteThreadDatabaseRows and deleteDestinationDatabases: the same setup before the try.
  • verifyDatabaseContents and verifySourceRemoved: two opens back to back.
  • main: the preflight connections (lines 2075-2083) and the reads after them, with the try at line 2093.

The impact is probably small. The script is a one-shot process that exits after printing the error, and the OS releases the handles then. No effect was measured.

Evidence: code read only. Not reproduced.

A fix would open inside the try and close whatever was opened in the finally.

At `1a85c77`, several functions in `move-codex-session.ts` open SQLite connections before the `try` whose `finally` closes them. If a later open or setup statement throws, the connections opened so far are never closed. The case from #132 is [`deleteSourceDatabases`](https://code.j4k.dev/j4k-oss/agent-skills/src/commit/1a85c77277b4f8924278686aa2c2969477c4eece/skills/move-codex-session/scripts/move-codex-session.ts#L1673-L1871). It opens the source state database, then opens the destination connections inside a `.map()` (lines 1717-1721), all before the `try` at line 1722. If the second or third destination fails to open, the earlier connections stay open. #132 added the two destination connections; #138 reshaped them into this array. The same shape is older (#52) in: - `copyState`, `copyHistory` and `copyThreadDatabase`: two opens, then `ATTACH`, `CREATE TEMP TABLE` and an insert loop, all before the `try`. - `deleteThreadDatabaseRows` and `deleteDestinationDatabases`: the same setup before the `try`. - `verifyDatabaseContents` and `verifySourceRemoved`: two opens back to back. - `main`: the preflight connections (lines 2075-2083) and the reads after them, with the `try` at line 2093. The impact is probably small. The script is a one-shot process that exits after printing the error, and the OS releases the handles then. No effect was measured. Evidence: code read only. Not reproduced. A fix would open inside the `try` and close whatever was opened in the `finally`.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
j4k-oss/agent-skills#139
No description provided.