Skip to content

Fix: Resolve DuckDB lock conflicts between CLI commands and running application#29

Merged
mahesh-solanke merged 3 commits into
mainfrom
fix/db-lock-issue-conflicting-api-call-and-cli-command
Jan 11, 2026
Merged

Fix: Resolve DuckDB lock conflicts between CLI commands and running application#29
mahesh-solanke merged 3 commits into
mainfrom
fix/db-lock-issue-conflicting-api-call-and-cli-command

Conversation

@mahesh-solanke

Copy link
Copy Markdown
Owner

Fix: Resolve DuckDB lock conflicts between CLI commands and running application

Problem

CLI commands (stat, query, init) fail when the database is locked by a running application due to DuckDB's file-level locking mechanism.

Solution

  • Added HTTP API fallback for stat, query, and init commands when database lock is detected
  • init command now checks database status via API before prompting for overwrite
  • Improved error messages with lock detection and process information
  • Fixed --force flag to properly exit after successful API reset

Changes

  • cli.py: Added API fallback logic with check_db_initialized_via_api() helper
  • database.py: Improved connection handling for read/write mode transitions
  • Enhanced user experience with clear messaging when database is locked

Testing

All CLI commands tested with running application to verify graceful fallback behavior.

- Introduced a new `parse_lock_error` function to extract process information from DuckDB lock error messages, improving error handling during database operations.
- Updated the `init` function to support resetting the database via an HTTP API if the application is running, enhancing user experience and flexibility.
- Improved error messages for locked databases, providing clearer guidance on resolving lock issues.
- Refactored the `DevTrackDB` class to support read-only and write modes, allowing for better control over database access.
- Updated various commands and middleware to utilize the new read/write capabilities, ensuring consistent behavior across the application.
…dling

- Added a new function `check_db_initialized_via_api` to verify if the database is initialized through the HTTP API, providing users with real-time feedback on database status.
- Updated the `init` function to utilize the new API check before prompting for database overwrite, improving user experience when the application is running.
- Enhanced error handling in the `query` and `stat` functions to attempt fetching logs and stats via the HTTP endpoint if the database is locked, ensuring better resilience and user guidance.
- Improved console output messages to inform users about the database state and actions they can take, such as resetting the database.
@mahesh-solanke mahesh-solanke self-assigned this Jan 11, 2026
…lock error handling

- Added a pytest fixture to automatically mock network requests, preventing hangs during CI and allowing tests to override specific behaviors as needed.
- Updated the `test_init_locked_database` function to provide clearer error messages for locked databases and ensure graceful handling of lock scenarios during initialization.
- Improved the mocking of database initialization errors to include process information for better debugging.
@mahesh-solanke mahesh-solanke merged commit 6c60a56 into main Jan 11, 2026
2 checks passed
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.

1 participant