Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(chat): release crashed research slots on session deletion (#7218)

LearningCircuitb0ac4dea5a40
diff --git a/changelog.d/+chat-delete-crashed-worker.bugfix.md b/changelog.d/+chat-delete-crashed-worker.bugfix.md
new file mode 100644
index 0000000000..635066a45e
--- /dev/null
+++ b/changelog.d/+chat-delete-crashed-worker.bugfix.md
@@ -0,0 +1 @@
+Deleting a chat session now releases concurrency slots left by crashed research workers while preserving their research history and stopping any delayed worker startup.
diff --git a/src/local_deep_research/chat/service.py b/src/local_deep_research/chat/service.py
index 6508e87557..5c569426de 100644
--- a/src/local_deep_research/chat/service.py
+++ b/src/local_deep_research/chat/service.py
@@ -29,6 +29,7 @@ from ..database.models import (
)
from ..database.session_context import get_user_db_session
from ..security.log_sanitizer import redact_and_bound_for_log
+from ..web.research_state import user_research_start_gate
from ..web.routes.globals import (
cleanup_research,
is_research_thread_alive,
@@ -898,7 +899,12 @@ class ChatService:
# but set the (in-memory, non-transactional) termination flags only
# AFTER the delete commits. Flagging before the commit would, on a
# commit failure, kill the research of a session that still exists.
- with get_user_db_session(self.username) as db:
+ # Share the admission lock with worker registration. A thread
+ # cannot start between the liveness check and committed cleanup.
+ with (
+ user_research_start_gate(self.username),
+ get_user_db_session(self.username) as db,
+ ):
session = db.query(ChatSession).filter_by(id=session_id).first()
if not session:
return False
@@ -910,10 +916,39 @@ class ChatService:
)
.all()
)
+ crashed = {
+ rid
+ for (rid,) in in_flight
+ if not is_research_thread_alive(rid)
+ }
+ if crashed:
+ # No worker remains to run its finally block. Keep the
+ # research history, but release its active slot in the
+ # same transaction as the session deletion. The status
+ # predicate preserves a concurrently completed result.
+ db.query(ResearchHistory).filter(
+ ResearchHistory.id.in_(crashed),
+ ResearchHistory.status == ResearchStatus.IN_PROGRESS,
+ ).update(
+ {
+ "status": ResearchStatus.SUSPENDED,
+ "completed_at": datetime.now(UTC).isoformat(),
+ },
+ synchronize_session=False,
+ )
+ db.query(UserActiveResearch).filter(
+ UserActiveResearch.username == self.username,
+ UserActiveResearch.research_id.in_(crashed),
+ ).delete(synchronize_session=False)
db.delete(session)
db.commit()
for (rid,) in in_flight:
set_termination_flag(rid)
+ if rid in crashed:
+ # An admitted request may still be waiting to register
+ # its worker. Preserve the stop request so that late
+ # worker exits at its first cancellation checkpoint.
+ cleanup_research(rid, preserve_termination_flag=True)
# Include the (truncated) username so a stolen-token bulk
# delete leaves a forensic trail tying each deletion to an
# account, not just an opaque session id (L_SEC1).
diff --git a/tests/chat/test_chat_delete_terminates_research.py b/tests/chat/test_chat_delete_terminates_research.py
index ff22a43bd4..d6a11f97fa 100644
--- a/tests/chat/test_chat_delete_terminates_research.py
+++ b/tests/chat/test_chat_delete_terminates_research.py
@@ -6,9 +6,8 @@ had its ``chat_session_id`` set to NULL by the FK (ON DELETE SET NULL),
but the worker thread itself was left running — burning LLM cycles on a
conversation the user had already discarded.
-The fix collapses the termination-flag set and the DELETE into one
-transaction (chat/service.py:delete_session) so the worker is signalled
-to terminate before the session row is removed.
+The service signals termination after the DELETE commits, so a failed
+transaction cannot stop research belonging to a session that still exists.
This test exercises the full integration path:
@@ -17,7 +16,7 @@ This test exercises the full integration path:
status="in_progress" (simulating a research that was just spawned).
3. Call ChatService.delete_session(...).
4. Assert that ``set_termination_flag`` was called with the in-flight
- research id BEFORE the row was removed.
+ research id after the row was removed.
The test deliberately patches ``set_termination_flag`` so we can assert
on call args without needing a real worker thread.
diff --git a/tests/chat/test_chat_service_contracts.py b/tests/chat/test_chat_service_contracts.py
index 2074718878..f81a005810 100644
--- a/tests/chat/test_chat_service_contracts.py
+++ b/tests/chat/test_chat_service_contracts.py
@@ -41,7 +41,7 @@ from unittest.mock import MagicMock, patch
import pytest
from sqlalchemy import create_engine, event
-from sqlalchemy.exc import IntegrityError
+from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.orm import sessionmaker
from local_deep_research.chat.service import (
@@ -1132,12 +1132,6 @@ class TestAttemptDeletion:
# 5. Session deletion cleanup
# ---------------------------------------------------------------------
-_DELETE_SESSION_SLOT_DEFECT = (
- "delete_session flags in-flight research for termination but leaves "
- "its UserActiveResearch row IN_PROGRESS, so a research whose worker "
- "is already dead keeps holding a per-user concurrency slot"
-)
-
class TestDeleteSessionCleanup:
@pytest.fixture
@@ -1220,21 +1214,24 @@ class TestDeleteSessionCleanup:
assert service.get_session(sibling)["title"] == "sibling"
assert service.get_in_progress_research_id(sibling) == "del-sibling"
- @pytest.mark.xfail(strict=True, reason=_DELETE_SESSION_SLOT_DEFECT)
def test_delete_releases_the_concurrency_slot_of_a_crashed_research(
self, user_dbs, deletable
):
"""A termination flag is only a request; a worker that already
died will never act on it, so deletion must release the cap row
itself (as ``delete_attempt`` does)."""
- service, target, _sibling = deletable
+ service, target, sibling = deletable
with (
patch(f"{SERVICE}.set_termination_flag"),
patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
+ patch(f"{SERVICE}.cleanup_research") as cleanup,
):
assert service.delete_session(target) is True
+ cleanup.assert_called_once_with(
+ "del-target", preserve_termination_flag=True
+ )
with user_dbs.raw(OWNER) as db:
assert (
db.query(UserActiveResearch)
@@ -1242,30 +1239,32 @@ class TestDeleteSessionCleanup:
.count()
== 0
)
+ history = db.get(ResearchHistory, "del-target")
+ assert history.chat_session_id is None
+ assert history.status == ResearchStatus.SUSPENDED
+ assert datetime.fromisoformat(history.completed_at).tzinfo is UTC
+ assert (
+ db.query(UserActiveResearch)
+ .filter_by(research_id="del-sibling")
+ .count()
+ == 1
+ )
+ assert service.get_in_progress_research_id(sibling) == "del-sibling"
- def test_blast_radius_a_crashed_researchs_slot_survives_deletion(
+ def test_a_live_research_keeps_its_slot_until_worker_cleanup(
self, user_dbs, deletable
):
- """Current behaviour, pinned.
-
- ``delete_session`` sets the termination flag and stops. It never
- calls ``cleanup_research``, never flips ResearchHistory to a
- terminal status, and never deletes the ``UserActiveResearch`` row
- the per-user cap is counted from. For a live worker that is fine
- -- its finally block cleans up. For a research whose thread is
- already dead nothing here does, and the row keeps counting against
- the cap until an unrelated later request happens to run
- ``reclaim_stale_user_active_research`` (send_message / followup
- only). Recoverable, but not by anything on this code path.
- """
+ """A stop request must not free capacity still held by a live worker."""
service, target, sibling = deletable
with (
patch(f"{SERVICE}.set_termination_flag"),
- patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
+ patch(f"{SERVICE}.is_research_thread_alive", return_value=True),
+ patch(f"{SERVICE}.cleanup_research") as cleanup,
):
service.delete_session(target)
+ cleanup.assert_not_called()
with user_dbs.raw(OWNER) as db:
stale = (
db.query(UserActiveResearch)
@@ -1293,6 +1292,151 @@ class TestDeleteSessionCleanup:
# The sibling's slot is legitimately held and still reachable.
assert service.get_in_progress_research_id(sibling) == "del-sibling"
+ def test_failed_commit_preserves_session_slot_and_process_state(
+ self, user_dbs, deletable
+ ):
+ service, target, _ = deletable
+
+ @contextmanager
+ def failing_session(username):
+ with user_dbs.raw(username) as db:
+ with patch.object(
+ db, "commit", side_effect=SQLAlchemyError("commit failed")
+ ):
+ yield db
+
+ with (
+ patch(f"{SERVICE}.get_user_db_session", failing_session),
+ patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
+ patch(f"{SERVICE}.set_termination_flag") as terminate,
+ patch(f"{SERVICE}.cleanup_research") as cleanup,
+ ):
+ assert service.delete_session(target) is False
+
+ terminate.assert_not_called()
+ cleanup.assert_not_called()
+ with user_dbs.raw(OWNER) as db:
+ assert db.get(ChatSession, target) is not None
+ history = db.get(ResearchHistory, "del-target")
+ assert history.status == ResearchStatus.IN_PROGRESS
+ assert history.chat_session_id == target
+ assert history.completed_at is None
+ assert db.query(UserActiveResearch).count() == 2
+ assert (
+ db.query(ChatMessage).filter_by(session_id=target).count() == 1
+ )
+
+ def test_cleanup_follows_commit_and_preserves_the_stop_request(
+ self, user_dbs, deletable
+ ):
+ from local_deep_research.web import research_state
+
+ service, target, _ = deletable
+ rid = "del-target"
+ research_state.set_active_research(rid, {"thread": None})
+
+ def terminate_after_commit(research_id):
+ with user_dbs.raw(OWNER) as db:
+ assert db.get(ChatSession, target) is None
+ assert (
+ db.query(UserActiveResearch)
+ .filter_by(research_id=research_id)
+ .count()
+ == 0
+ )
+ research_state.set_termination_flag(research_id)
+
+ try:
+ with patch(
+ f"{SERVICE}.set_termination_flag", terminate_after_commit
+ ):
+ assert service.delete_session(target) is True
+ assert not research_state.is_research_active(rid)
+ assert research_state.is_termination_requested(rid)
+ finally:
+ research_state.cleanup_research(rid)
+
+ def test_cleanup_preserves_a_result_completed_before_the_write(
+ self, user_dbs, deletable
+ ):
+ service, target, _ = deletable
+ completed_at = "2026-10-01T12:00:00+00:00"
+
+ def finished_worker(research_id):
+ with user_dbs.raw(OWNER) as db:
+ history = db.get(ResearchHistory, research_id)
+ history.status = ResearchStatus.COMPLETED
+ history.completed_at = completed_at
+ history.report_content = "Completed answer"
+ db.commit()
+ return False
+
+ with (
+ patch(f"{SERVICE}.is_research_thread_alive", finished_worker),
+ patch(f"{SERVICE}.set_termination_flag"),
+ patch(f"{SERVICE}.cleanup_research"),
+ ):
+ assert service.delete_session(target) is True
+
+ with user_dbs.raw(OWNER) as db:
+ history = db.get(ResearchHistory, "del-target")
+ assert history.status == ResearchStatus.COMPLETED
+ assert history.completed_at == completed_at
+ assert history.report_content == "Completed answer"
+
+ def test_registration_waits_for_cleanup_and_observes_cancellation(
+ self, user_dbs, deletable
+ ):
+ from local_deep_research.web import research_state
+
+ service, target, _ = deletable
+ rid = "del-target"
+ attempted = threading.Event()
+ observed = threading.Event()
+ cancellation = []
+ registration = []
+
+ def worker_entry():
+ cancellation.append(research_state.is_termination_requested(rid))
+ observed.set()
+
+ worker = threading.Thread(target=worker_entry)
+
+ def register():
+ attempted.set()
+ registration.append(
+ research_state.check_and_start_research(
+ rid, {"thread": worker, "settings": {"username": OWNER}}
+ )
+ )
+
+ registrar = threading.Thread(target=register)
+
+ def crashed_worker(research_id):
+ assert research_id == rid
+ registrar.start()
+ assert attempted.wait(5)
+ assert not observed.wait(0.05), (
+ "registration must wait while session cleanup holds admission"
+ )
+ return False
+
+ try:
+ with patch(f"{SERVICE}.is_research_thread_alive", crashed_worker):
+ assert service.delete_session(target) is True
+ registrar.join(5)
+ assert not registrar.is_alive()
+ assert observed.wait(5)
+ worker.join(5)
+ assert registration == [True]
+ assert cancellation == [True]
+ finally:
+ if registrar.ident is not None:
+ registrar.join(5)
+ if worker.ident is not None:
+ worker.join(5)
+ research_state.cleanup_research(rid)
+
# ---------------------------------------------------------------------
# 6. Title generation