Skip to content

Close SQLite log connections when minimize returns - #714

Open
hmgaudecker wants to merge 2 commits into
optimagic-dev:mainfrom
hmgaudecker:close-sqlite-log-connections
Open

hmgaudecker wants to merge 2 commits into
optimagic-dev:mainfrom
hmgaudecker:close-sqlite-log-connections

Conversation

@hmgaudecker

Copy link
Copy Markdown
Member

Closes #713.

Problem

The SQLite log stores keep pooled connections open until the OptimizeResult is garbage collected. In WAL mode, log.db therefore still changes after minimize returns (the WAL is checkpointed into the file when the last connection closes). Tools that hash outputs right after a function returns see a different file than the one that ends up on disk; e.g. pytask re-runs every estimation task that writes an optimagic log.

Changes

  • LogStore.close() (no-op default; the SQLite store disposes of its engines). minimize/maximize call it after the optimization, before creating the LogReader.
  • Dispose of the temporary engine used in SQLAlchemyConfig.metadata and of each store's engine after table reflection.
  • Dispose of the engine after reads, so a LogReader (e.g. res.logger.read_history()) does not hold the file open. Reads during the optimization are rare (step bookkeeping only), so pooling for the hot write path is kept.

I first tried NullPool; it doubled the logging overhead (Nelder–Mead, 2000 logged evaluations: 1.08 s vs 0.52 s on main). With this PR: 0.50–0.54 s.

Tests

Two new tests in tests/optimagic/logging/test_logger.py; both fail on main (a log.db-wal file exists after minimize returns / after reading the history). tests/optimagic/logging tests/optimagic/optimization tests/estimagic: 1426 passed, 11 skipped.

I did not add a CHANGES.md entry since entries reference the PR number; happy to add one.

https://claude.ai/code/session_01Y3CuwvNUhYTSpriKYSwqmq

hmgaudecker and others added 2 commits October 16, 2026 07:38
The log stores kept pooled SQLite connections open until the
OptimizeResult was garbage collected. In WAL mode, the database file
therefore changed after minimize returned (checkpoint on close), which
breaks tools that hash outputs right after a task (e.g. pytask re-runs
the task every time).

- Close the logger's engines at the end of optimization.
- Dispose of the engines used for table reflection and after reads, so
  a LogReader does not hold the file open either.

Closes optimagic-dev#713.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y3CuwvNUhYTSpriKYSwqmq
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y3CuwvNUhYTSpriKYSwqmq
@hmgaudecker
hmgaudecker requested a review from janosg October 16, 2026 05:59
@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/optimagic/logging/logger.py 94.11% <100.00%> (+0.19%) ⬆️
src/optimagic/logging/sqlalchemy.py 95.70% <100.00%> (+0.10%) ⬆️
src/optimagic/optimization/optimize.py 92.17% <100.00%> (+0.06%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

BUG: SQLite log connection stays open after minimize returns; log.db changes afterwards (WAL not checkpointed)

1 participant