Skip to content

fix(export_l5x): close SQLite connection after .project is built - #49

Merged
hutcheb merged 1 commit into
hutcheb:mainfrom
colinlove:fix/sqlite-connection-never-closed
Nov 3, 2026
Merged

hutcheb merged 1 commit into
hutcheb:mainfrom
colinlove:fix/sqlite-connection-never-closed

Conversation

@colinlove

Copy link
Copy Markdown
Contributor

This fix is part of a series found with AI assistance (Claude Code) — see the first PR in this batch for the full disclosure.


ExportL5x's sqlite connection is never closed — leaks a temp dir per file, can fill the disk

Symptom

Converting many .ACD files in one process (e.g. batch-converting a folder)
leaves behind a growing pile of acd_load_* / acd-build-* temp
directories under the OS temp folder — one per file converted, never
cleaned up. Left running, this can fill the disk. This is not a
theoretical concern: running a batch conversion of ~770 real project files
in one process filled a 951 GB disk to 100% before it finished.

Root cause

ExportL5x.__post_init__ opens a sqlite connection (self._db = sqlite3.connect(...)) against a database file inside its temp directory,
and nothing in the class ever calls self._db.close(). The typical caller
pattern (see acd.api.load_acd) is:

cleanup = temp_dir is None
if cleanup:
    temp_dir = tempfile.mkdtemp(prefix="acd_load_")
try:
    exporter = ExportL5x(str(path), temp_dir, ...)
    return exporter.project
finally:
    if cleanup:
        shutil.rmtree(temp_dir, ignore_errors=True)

On Windows, an open sqlite connection holds an OS-level file lock on its
database file. exporter goes out of scope when the function returns, but
Python's reference counting/GC doesn't guarantee the connection object (and
its file lock) is released by the time shutil.rmtree runs immediately
afterward — and shutil.rmtree(..., ignore_errors=True) silently swallows
the resulting PermissionError. The result: the temp directory is left on
disk, forever, every single call, with no error or warning of any kind.

Fix

Close self._db once the object tree is fully built and nothing will query
it again — at the end of the project property getter, right before it
returns the built RSLogix5000Content. Nothing after that point in
ExportL5x (or in the returned project) needs the sqlite connection.

Suggested validation

Convert the same file in a loop (say, 50 times) in one process and confirm
the temp directory count under the OS temp folder doesn't grow. Before this
fix, it grows by one directory per call; after, it stays flat (each
directory is removed once its ExportL5x instance's connection is closed).

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

2 participants