Skip to content

Bug: Database locking error when deleting nodes with inline mentions #190

Closed

Description

@malibio

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

  1. Create two text nodes
  2. Add an @mention reference from Node A to Node B
  3. Delete Node B using backspace/delete
  4. 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:

  1. Backend Delete Operation:

    • Rust backend executes: DELETE FROM nodes WHERE id = ?
    • Foreign key ON DELETE CASCADE automatically removes node_mentions table entries
    • Delete transaction holds locks on both tables
  2. Frontend Cleanup (Races with Backend):

    • NodeReferenceService subscribes to node:deleted events
    • Calls cleanupDeletedNodeReferences() via setTimeout(..., 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_mentions table 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_mentions table 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_mentions table
  • 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_mentions table entries are cleaned up via CASCADE
  • Integration test covers this scenario
  • All existing tests pass
  • Code passes bun run quality:fix

Activity

  1. added
    bug
    on Oct 8, 2025
  2. self-assigned this
    on Oct 8, 2025
  3. added a commit that references this issue on Oct 8, 2025
    b03540f
  4. added 6 commits that reference this issue on Oct 8, 2025
    07a2787
    50d4d94
    7245efb
    27061ad
    f82b2f2
    0a3d477
  5. added 10 commits that reference this issue on Feb 4, 2026
    535b38e
    541c771
    a9b7b18
    d66b18e
    672199a
    bf5c5d5
    f78ec36
    cae3fe7
    806ef4e
    3d91ea5
  6. added 5 commits that reference this issue on Feb 26, 2026
    4cda179
    a6d8aad
    04a1345
    8c74b72
    1f461e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdatabasereferences

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions