Skip to content

Fix database lock issue: allow external access after sync finish#4888

Open
meichuanyi wants to merge 1 commit into
ankitects:mainfrom
meichuanyi:main
Open

Fix database lock issue: allow external access after sync finish#4888
meichuanyi wants to merge 1 commit into
ankitects:mainfrom
meichuanyi:main

Conversation

@meichuanyi
Copy link
Copy Markdown

Problem

The sync server holds an open database connection after sync operations complete, preventing external tools (like REST APIs, backup scripts, or monitoring tools) from accessing collection.anki2.

Root causes:

  1. finish handler only clears sync_state but doesn't close the Collection
  2. server_finish doesn't execute WAL checkpoint
  3. SQLite busy_timeout=0 causes immediate failure on lock contention

Solution

Three minimal changes:

1. Close Collection after finish (handlers.rs)

user.sync_state = None;
user.col = None; // Close collection to release database lock

2. Force WAL checkpoint on finish (finish.rs)

col.storage.checkpoint()?; // Force WAL checkpoint to release database lock

3. Set reasonable busy_timeout (sqlite.rs)

db.busy_timeout(std::time::Duration::from_secs(5))?; // Allow 5 seconds for lock contention

Testing

  • ✅ Compiled successfully with Rust 1.92.0
  • ✅ Health check passes
  • ✅ Database accessible during sync
  • ✅ Database accessible after finish

Impact

  • Minimal code changes (3 files, 4 lines)
  • No change to sync protocol or client behavior
  • Allows external tools to access database safely after sync completes
  • busy_timeout=5s provides graceful handling for brief lock contention

Use Case

This enables scenarios like:

  • REST APIs for AI + Anki integration
  • Automated backup scripts
  • Database monitoring tools
  • External card import/export tools

All can now operate without conflicting with sync server's database connection.

- Close Collection after sync finish to release database lock
- Force WAL checkpoint during sync finish
- Set SQLite busy_timeout to 5 seconds for lock contention

This fixes the issue where external tools (like REST API) could not
access the Anki database after sync operations due to the sync server
holding an open connection.
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