Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

## [Unreleased]

## [v0.51.680] — 2026-06-26 — Release YJ (session-index rebuild can't be clobbered by a late worker)

### Fixed

- **A late-finishing background session-index rebuild can no longer clobber a newer rebuild's state.** When a session-index rebuild worker finished, it cleared the rebuild bookkeeping globals if only the target tuple matched — so an older worker completing after a newer rebuild had already taken over could wipe the newer owner's registration. The cleanup now also requires that the finishing worker is still the registered owner thread (`_SESSION_INDEX_REBUILD_THREAD is current_thread`, checked under the rebuild lock), so an out-of-order older worker leaves the newer owner's state intact while the genuine owner still clears normally. Thanks @rodboev. (#4993, #3894)

## [v0.51.679] — 2026-06-26 — Release YI (faster fresh sidebar boot — parallel session/project fetches)

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def _session_dir_has_persisted_session_files() -> bool:

def _rebuild_session_index_background(expected_session_dir: Path, expected_index_file: Path) -> None:
global _SESSION_INDEX_REBUILD_THREAD, _SESSION_INDEX_REBUILD_THREAD_TARGET
current_thread = threading.current_thread()
try:
with _SESSION_INDEX_REBUILD_LOCK:
if SESSION_DIR != expected_session_dir or SESSION_INDEX_FILE != expected_index_file:
Expand All @@ -213,7 +214,7 @@ def _rebuild_session_index_background(expected_session_dir: Path, expected_index
logger.debug("Background session-index rebuild failed", exc_info=True)
finally:
with _SESSION_INDEX_REBUILD_LOCK:
if _SESSION_INDEX_REBUILD_THREAD_TARGET == (
if _SESSION_INDEX_REBUILD_THREAD is current_thread and _SESSION_INDEX_REBUILD_THREAD_TARGET == (
expected_session_dir,
expected_index_file,
):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_session_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,3 +1666,30 @@ def _switch_globals_then_write(*args, **kwargs):
assert not new_index_file.exists()
rows = _read_index(original_index_file)
assert [row["session_id"] for row in rows] == ["late_switch_sid"]


def test_background_rebuild_old_thread_finally_preserves_new_same_target_owner(tmp_path, monkeypatch):
session_dir = tmp_path / "sessions"
session_dir.mkdir(exist_ok=True)
index_file = session_dir / "_index.json"
target = (session_dir, index_file)

monkeypatch.setattr(models, "SESSION_DIR", session_dir)
monkeypatch.setattr(models, "SESSION_INDEX_FILE", index_file)

old_thread = object()
new_thread = object()
monkeypatch.setattr(models, "_SESSION_INDEX_REBUILD_THREAD", old_thread)
monkeypatch.setattr(models, "_SESSION_INDEX_REBUILD_THREAD_TARGET", target)
monkeypatch.setattr(models.threading, "current_thread", lambda: old_thread)

def _handoff_then_write(*args, **kwargs):
monkeypatch.setattr(models, "_SESSION_INDEX_REBUILD_THREAD", new_thread)
monkeypatch.setattr(models, "_SESSION_INDEX_REBUILD_THREAD_TARGET", target)

monkeypatch.setattr(models, "_write_session_index", _handoff_then_write)

models._rebuild_session_index_background(*target)

assert models._SESSION_INDEX_REBUILD_THREAD is new_thread
assert models._SESSION_INDEX_REBUILD_THREAD_TARGET == target
Loading