[Bug]: Deleting a Mastery session can leave an orphaned turn holding the workspace activity lock
Related Module
API/Backend — Mastery sessions and workspace resource assignments
Environment
- DeepTutor v1.6.12, official commit
ef2d9e5c3c99fd073742c5aadc2bb9584b1e503b, without overlays. - Linux ARM64, Python 3.11.16, one backend process.
- Relevant deployed Python files were compared byte-for-byte with the official commit.
Observed behavior
Saving a workspace's knowledge-base assignments repeatedly returns HTTP 400:
Workspace data is busy. Wait for active requests and tasks before retrying.Deleting the Mastery path and conversation does not clear the condition. Read-only inspection of the affected backend found:
- A live Mastery turn suspended at
_wait_for_user_reply()/reply_queue.get(). - Its surrounding
data_activity()generator still owns asqlite3.Connection, and the OS reports the backend holding a shared lock onworkspace-activity.sqlite3. - The corresponding session and turn records are absent from the live SQLite stores. The initially inspected, different conversation had completed turns; querying durable nonterminal rows alone therefore missed this orphan.
- The orphan's
_coordinate_execution()coroutine is still alive. - A new read-only knowledge-base request completes normally without increasing the open activity-file descriptor count.
Inspection read code/frame metadata from the process without pausing it, injecting code, restarting it, or dumping local-variable values. No private conversation text, account IDs, session IDs, or host paths are included here.
Source-level cause
The start and deletion paths resolve different TurnRuntimeManager instances:
- Unified chat uses
container.turns;TurnApplicationService._resolve()resolves the application container'sRuntimeRegistry. Its runtime has the coordinator and owns the live execution. - Session deletion and Mastery cancellation instead call
get_turn_runtime_manager(), which maintains a separate_runtime_instancesdictionary and constructsTurnRuntimeManager(store=store)without a coordinator. - This second manager has no matching in-memory execution. Its fallback only handles durable status
running; forwaiting_input,cancel_turn()returnsFalsewithout cancelling the owning coroutine. - The session deletion route ignores that return value and deletes the records. Mastery cancellation also ignores it and releases the separate learning-path lease.
- The actual
_run_turn()remains suspended awaiting user input while@workspace_writerholds its activity lease. Its coordination loop renews the lease without checking whether the session/turn records were deleted. Workspace assignment updates require an exclusive activity lock and consequently continue failing.
Targeted reproduction scenario
The live orphan and source paths above were verified. The following clean-instance sequence is derived from them and has not yet been rerun as an end-to-end test:
- Start a Mastery chat through the unified chat transport.
- Let the tutor call
ask_user, leaving the turn inwaiting_input. - Delete that conversation through
DELETE /api/sessions/{session_id}. Separately exercise deleting/resetting its Mastery path, which shares the cancellation helper. - Try saving workspace resource assignments.
- Check that the owning runtime task is still waiting despite the session deletion, and that the assignment PATCH fails with the busy message.
Expected behavior
Deletion should cancel and await the execution that actually owns the turn before removing its records. A failed cancellation must not silently leave an inaccessible live task holding the account activity lock. The deletion/cancellation routes should use the same application runtime/coordinator as turn creation.
Validation boundaries
This report identifies the confirmed live orphan and a matching deterministic source-level cancellation defect. It does not claim that a freshly started end-to-end reproduction, a fix, or a restart recovery has been tested. The initial sequence that created this particular orphan was not captured in successful-request access logs.
Source: HKUDS/DeepTutor