Reset file worker status when a synchronous refresh is cancelled (#775)

* Reset file worker status when a synchronous refresh is cancelled

refresh_sync runs inside request handlers.  When Sanic cancelled one on
response timeout, the phase flag cleared as its `with` block unwound but
the shared status did not, so the worker reported 'modeling' forever while
nothing ran.  Reset to idle on cancellation.

Deleting outdated Zims also ran the global modelers inside the request,
which is what timed out.  Skip post-processing there and update the
outdated_zims flag directly, the only cleanup step it relied on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* tests: observe the modeling flag in the cancelled refresh_sync test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-10-04 11:25:05 -06:00 • committed by Roland
parent bed25b429d
commit 8713a6136b
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 56 additions and 5 deletions

View file

@ -586,8 +586,10 @@ async def remove_outdated_zim_files(path: pathlib.Path = None) -> int:
deleted_count += 1
if deleted_count:
# Refresh synchronously to clean up DB before returning.
await file_worker.refresh_sync(list(outdated))
# Refresh synchronously to clean up DB before returning. Deleting needs no modeling, and the
# global modelers would work through the whole unindexed backlog inside this request.
await file_worker.refresh_sync(list(outdated), post_processing=False)
flag_outdated_zim_files()
# Trigger Kiwix restart so the deleted zim files are no longer served.
restart_kiwix_handler.activate_switch()

View file

@ -7,6 +7,7 @@ from modules.zim import lib
from modules.zim.models import Zim
from wrolpi.api_utils import api_app
from wrolpi.files.models import FileGroup
from wrolpi.files.worker import file_worker
@pytest.mark.asyncio
@ -145,3 +146,28 @@ async def test_remove_outdated_zim_files_no_restart_when_nothing_deleted(async_c
assert deleted_count == 0
switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {}
assert 'restart_kiwix' not in switches
@pytest.mark.asyncio
async def test_remove_outdated_zim_files_skips_post_processing(async_client, test_session, test_directory,
zim_path_factory):
"""Deleting outdated Zims must not run the global modelers inside the request.
`DELETE /api/zim/outdated` awaits this; modeling the whole unindexed backlog there exceeded the
response timeout and the cancelled request stranded the file worker status."""
zim_dir = test_directory / 'zims'
zim_dir.mkdir(exist_ok=True)
test_zim_bytes = zim_path_factory().read_bytes()
paths = [zim_dir / 'wikipedia_en_all_maxi_2020-01.zim', zim_dir / 'wikipedia_en_all_maxi_2020-02.zim']
for path in paths:
path.write_bytes(test_zim_bytes)
await file_worker.refresh_sync(paths)
assert test_session.query(FileGroup).count() == 2
with mock.patch('wrolpi.files.worker.apply_modelers') as apply_modelers:
deleted_count = await lib.remove_outdated_zim_files(zim_dir)
assert deleted_count == 1
apply_modelers.assert_not_called()
# The deleted Zim is still removed from the DB.
assert test_session.query(FileGroup).count() == 1