Skip to content

Conversation

@dustinbyrne
Copy link
Contributor

@dustinbyrne dustinbyrne commented Apr 17, 2025

We're still seeing some cases where the database is locked upon startup. This change aims to resolve this entirely by enforcing the process hold a lock file in order to migrate and setting a busy timeout of 3s on all db connections.

This change adds proper database migration handling to the thread index service. Previously, idempotent migrations were run every time the RPC service started, which could lead to race conditions when multiple instances tried to migrate simultaneously.

Now:

  • A single process will manage database migrations using file-based locking
  • Migrations are only run when necessary (schema version mismatch)
  • Added proper error handling and rollback support
  • Migrations are wrapped in a transaction for atomicity
  • Added retry logic with a reasonable timeout
  • Database connections use a busy timeout to handle concurrent access

This change improves reliability and prevents potential database corruption when multiple AppMap instances are running.

Dependencies:

  • Added proper-lockfile for file locking
  • Added corresponding TypeScript type definitions

@dustinbyrne dustinbyrne requested a review from Copilot April 17, 2025 17:59
Copy link
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 reviewed 2 out of 3 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • packages/cli/package.json: Language not supported
Comments suppressed due to low confidence (2)

packages/cli/src/rpc/navie/services/threadIndexService.ts:127

  • Consider wrapping the lock release call in a try-catch block to handle potential errors during the lock release process.
if (lockRelease) { await lockRelease(); }

packages/cli/src/rpc/navie/services/threadIndexService.ts:109

  • Confirm that setting the journal mode outside of a transaction aligns with the intended behavior, especially under concurrent startup scenarios.
this.db.exec('PRAGMA journal_mode = WAL');

@dustinbyrne dustinbyrne force-pushed the fix/thread-index-migration-lock branch from 9c5ef44 to 0130ef8 Compare April 17, 2025 18:14
A single process will now migrate the database when needed. Migrations
will no longer be run every time the RPC service starts.
@dustinbyrne dustinbyrne force-pushed the fix/thread-index-migration-lock branch from 0130ef8 to 5754e31 Compare April 17, 2025 18:47
@dustinbyrne dustinbyrne requested a review from dividedmind April 17, 2025 19:10
CREATE INDEX IF NOT EXISTS idx_thread_id ON project_directories (thread_id);
PRAGMA user_version = ${SCHEMA_VERSION};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Neat! I didn't know about this feature.

@dustinbyrne dustinbyrne merged commit 0e114d9 into main Apr 21, 2025
22 checks passed
@dustinbyrne dustinbyrne deleted the fix/thread-index-migration-lock branch April 21, 2025 21:12
appland-release added a commit that referenced this pull request Apr 22, 2025
# [@appland/appmap-v3.188.5](https://github.com/getappmap/appmap-js/compare/@appland/appmap-v3.188.4...@appland/appmap-v3.188.5) (2025-04-22)

### Bug Fixes

* Thread index database migrations are now atomic ([#2280](#2280)) ([0e114d9](0e114d9))
@appland-release
Copy link
Contributor

🎉 This PR is included in version @appland/appmap-v3.188.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants