Repository navigation
Description
Problem
When deleting a node that has inline syntax (@mentions), a database locking error occurs:
[Error] [TauriNodeService] Failed to query nodes: {mentionedBy: "a3def39f-98bf-441e-920f-1f96752107ea"}
Error: Node operation failed: Query failed: Failed to prepare mentioned_by query: SQLite failure: `database is locked`
[Error] NodeReferenceService: Error cleaning up deleted node references
Steps to Reproduce
- Create two text nodes
- Add an @mention reference from Node A to Node B
- Delete Node B using backspace/delete
- Observe the database locking error
Expected Behavior
Node should delete cleanly without database locking errors.
Actual Behavior
Database reports "database is locked" when NodeReferenceService.cleanupDeletedNodeReferences() tries to query mentioned_by while the delete transaction is still active.
Root Cause Analysis
Race Condition Between Frontend and Backend:
-
Backend Delete Operation:
- Rust backend executes:
DELETE FROM nodes WHERE id = ? - Foreign key
ON DELETE CASCADEautomatically removesnode_mentionstable entries - Delete transaction holds locks on both tables
- Rust backend executes:
-
Frontend Cleanup (Races with Backend):
NodeReferenceServicesubscribes tonode:deletedevents- Calls
cleanupDeletedNodeReferences()viasetTimeout(..., 0) - Queries
queryNodes({ mentionedBy: deletedNodeId }) - This prepares SQL:
SELECT n.* FROM nodes n INNER JOIN node_mentions nm ON n.id = nm.node_id WHERE nm.mentions_node_id = ? - Fails because delete transaction is still active
Why It Only Happens with Mentions:
- Text-to-text nodes without mentions don't trigger cleanup query
- Nodes with mentions in
node_mentionstable trigger CASCADE delete + cleanup query simultaneously
Technical Details
Affected Files:
packages/desktop-app/src/lib/services/node-reference-service.ts:1084-1116(cleanup function)packages/desktop-app/src/lib/services/node-reference-service.ts:870-876(event handler)packages/core/src/services/node_service.rs:941-946(mentioned_by query)
Database Configuration:
- WAL mode enabled:
PRAGMA journal_mode = WAL - Busy timeout:
PRAGMA busy_timeout = 5000 - Foreign keys:
PRAGMA foreign_keys = ON
Proposed Solution
Part A: Remove Redundant Cleanup (Primary Fix)
The cleanupDeletedNodeReferences() function is unnecessary because:
- Database CASCADE deletes already handle
node_mentionstable cleanup - Frontend doesn't need to query and update - database already cleaned up
Change: Remove or disable the cleanup call in the node:deleted event handler
Part B: Add Query Timeout Protection (Defense in Depth)
Update query_nodes_simple() in Rust to use connection with busy timeout:
let conn = self.db.connect_with_timeout().await?;This prevents immediate failure on lock contention.
Test Coverage Needed
Create integration test that:
- Creates nodes with mention relationships
- Adds entries to
node_mentionstable - Deletes mentioned node
- Verifies no "database is locked" errors
- Confirms CASCADE cleanup worked correctly
Acceptance Criteria
- Database locking error no longer occurs when deleting nodes with mentions
- Delete operation completes successfully
-
node_mentionstable entries are cleaned up via CASCADE - Integration test covers this scenario
- All existing tests pass
- Code passes
bun run quality:fix
Activity
Fix: Remove database locking race condition on node deletion (closes #…
Fix: Remove database locking race condition on node deletion (closes #…
Fix: Update node-reference-service test to match Issue #190 fix
refactor: Apply all code review improvements to database persistence …
feat(phase-3): Add comprehensive test coverage for Phase 3 operations…
feat: Session 3 - Section 7: Database Persistence Tests (9 tests, 100…
Phase 3: Advanced & Regression Test Coverage (Sections 9-12, 28 tests) (
Fix 19 frontend test failures by adding database mode guards
Fix 19 frontend test failures by adding database mode guards (#410)
Fix: Remove database locking race condition on node deletion (closes #…
Fix: Update node-reference-service test to match Issue #190 fix
feat: Session 3 - Section 7: Database Persistence Tests (9 tests, 100…
Phase 3: Advanced & Regression Test Coverage (Sections 9-12, 28 tests) (
Fix 19 frontend test failures by adding database mode guards (#410)
Fix: Remove database locking race condition on node deletion (closes #…
Fix: Update node-reference-service test to match Issue #190 fix
feat: Session 3 - Section 7: Database Persistence Tests (9 tests, 100…
Phase 3: Advanced & Regression Test Coverage (Sections 9-12, 28 tests) (
Fix 19 frontend test failures by adding database mode guards (#410)
Fix: Remove database locking race condition on node deletion (closes #…
Fix: Update node-reference-service test to match Issue #190 fix
feat: Session 3 - Section 7: Database Persistence Tests (9 tests, 100…
Phase 3: Advanced & Regression Test Coverage (Sections 9-12, 28 tests) (
Fix 19 frontend test failures by adding database mode guards (#410)