Skip to content

Commit ac062d1

Browse files
address per comment
1 parent 93e0b42 commit ac062d1

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

tests/test_search_index_concurrency.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def indexed_tree(tmp_path, monkeypatch):
8383
}
8484

8585

86+
def _sentinel_hit_observed(term: str, result: IndexQueryResult) -> bool:
87+
if result["index_locked"] or not result["query_ok"]:
88+
return False
89+
if not result["hits"]:
90+
return False
91+
return any(term in (hit["text"] or "") for hit in result["hits"])
92+
93+
8694
def _assert_sentinel_reader_result(term: str, result: IndexQueryResult) -> None:
8795
if result["index_locked"]:
8896
return
@@ -168,8 +176,11 @@ def test_concurrent_rebuild_and_query(self, indexed_tree) -> None:
168176
errors: list[BaseException] = []
169177
barrier = threading.Barrier(_READER_THREADS + 1, timeout=_BARRIER_TIMEOUT_S)
170178
patches = _index_patches(indexed_tree["cache_root"])
179+
hits_lock = threading.Lock()
180+
sentinel_hits_observed = 0
171181

172182
def reader() -> None:
183+
nonlocal sentinel_hits_observed
173184
try:
174185
for _ in range(_CONCURRENT_ROUNDS):
175186
barrier.wait(timeout=_BARRIER_TIMEOUT_S)
@@ -179,6 +190,9 @@ def reader() -> None:
179190
max_results=10,
180191
)
181192
_assert_sentinel_reader_result(indexed_tree["term"], result)
193+
if _sentinel_hit_observed(indexed_tree["term"], result):
194+
with hits_lock:
195+
sentinel_hits_observed += 1
182196
except BaseException as exc:
183197
errors.append(exc)
184198

@@ -201,6 +215,9 @@ def writer() -> None:
201215
thread.join(timeout=_BARRIER_TIMEOUT_S + 90.0)
202216
assert not thread.is_alive(), f"{thread.name} did not finish (possible deadlock)"
203217
assert not errors, errors
218+
assert sentinel_hits_observed > 0, (
219+
"no reader round observed sentinel hits during concurrent rebuild"
220+
)
204221

205222
def test_queries_during_background_refresh(self, indexed_tree) -> None:
206223
errors: list[BaseException] = []

utils/search_index.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -813,18 +813,13 @@ def reset_background_for_tests() -> None:
813813
global _background_started, _background_lock_fd, _background_thread
814814
_background_stop.set()
815815
thread = _background_thread
816-
worker_stopped = True
817816
if thread is not None and thread.is_alive():
818817
thread.join(timeout=_BACKGROUND_JOIN_TIMEOUT_S)
819-
worker_stopped = not thread.is_alive()
820-
if not worker_stopped:
818+
if thread.is_alive():
821819
_logger.warning(
822820
"background search-index worker did not stop within %.0fs",
823821
_BACKGROUND_JOIN_TIMEOUT_S,
824822
)
825-
if not worker_stopped:
826-
_clear_usability_cache()
827-
return
828823
with _index_lock:
829824
_background_started = False
830825
_background_thread = None

0 commit comments

Comments
 (0)