Skip to content

fix: dispose engine after import-time table creation#1256

Merged
zzstoatzz merged 1 commit into
mainfrom
fix/1255-dispose-engine-on-import
Dec 22, 2025
Merged

fix: dispose engine after import-time table creation#1256
zzstoatzz merged 1 commit into
mainfrom
fix/1255-dispose-engine-on-import

Conversation

@zzstoatzz
Copy link
Copy Markdown
Collaborator

Summary

Background

When marvin is imported, ensure_db_tables_exist() runs asyncio.run(create_db_and_tables()). This creates an async engine that gets cached. The underlying aiosqlite connection spawns a worker thread that could prevent Python from exiting cleanly if not properly disposed.

Changes

  • Added dispose_engine: bool = False parameter to create_db_and_tables()
  • When dispose_engine=True, the engine is disposed and removed from the cache after table creation
  • Updated ensure_db_tables_exist() to pass dispose_engine=True when called from import

Test plan

  • Added regression test test_create_db_and_tables_with_dispose_cleans_up_engine
  • Verified with reproduction script that no non-daemon threads remain after import
  • All existing database tests pass

🤖 Generated with Claude Code

when `ensure_db_tables_exist()` runs on import, it creates an async
engine via `asyncio.run()`. the engine's aiosqlite worker thread would
remain cached after the event loop closed, potentially preventing
python from exiting cleanly.

this adds a `dispose_engine` parameter to `create_db_and_tables()` which
disposes the engine and removes it from the cache after table creation.
`ensure_db_tables_exist()` now uses this to clean up after itself.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 21, 2025 23:44
@github-actions github-actions Bot added documentation Improvements or additions to documentation tests labels Dec 21, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #1255 where importing marvin causes the process to hang on exit. The fix adds a dispose_engine parameter to create_db_and_tables() to properly clean up the aiosqlite worker thread after table creation.

  • Adds dispose_engine parameter to create_db_and_tables() for controlled engine cleanup
  • Updates ensure_db_tables_exist() to dispose the engine after import-time table creation
  • Adds regression test to verify engine disposal and thread cleanup

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/marvin/database.py Adds dispose_engine parameter to create_db_and_tables() with disposal logic; updates ensure_db_tables_exist() to pass dispose_engine=True
tests/basic/engine/test_database.py Adds regression test test_create_db_and_tables_with_dispose_cleans_up_engine to verify engine disposal and thread cleanup
docs/api-reference/marvin-database.mdx Updates documentation to reflect new dispose_engine parameter

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@PrefectHQ PrefectHQ deleted a comment from Copilot AI Dec 21, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@zzstoatzz zzstoatzz merged commit ddb6822 into main Dec 22, 2025
9 of 10 checks passed
@zzstoatzz zzstoatzz deleted the fix/1255-dispose-engine-on-import branch December 22, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

process hangs on exit after importing marvin (aiosqlite worker thread not cleaned up)

2 participants