Repository navigation
fix(webui): keep disk-history tasks deletable when they have no video - #1706
kapil971390 wants to merge 3 commits into
Conversation
harry0703#1663 made is_task_busy() treat any unparseable state (including None) as busy, to stop the delete API from canceling a task whose live state came back as an unreadable legacy string. That part is correct and its own test (test_is_task_busy_string_state.py) still passes unchanged. But _scan_history_tasks() sets state=None for disk-only task entries that never produced a video file (orphaned or pre-dating Redis tracking), and that None now reads as 'busy' under the new rule too, even though nothing is actually running for a task with no live record. Both the Delete button's disabled state and _delete_task()'s own guard call is_task_busy() on these entries, so they become permanently undeletable from the WebUI, with a misleading '(Task Status Processing)' tooltip. This only changes the two WebUI call sites, not is_task_busy() itself: a task is only treated as busy when it has a live tracked record (sm.state.get_task() returned something) or is in this session's active- generation registry. A disk-history entry with neither has nothing that could be processing or cross-posting it, regardless of what state label the history scan assigned it.
|
Thanks for looking into this. The orphaned-history cleanup is useful, but I found a running-task deletion case in local testing that needs fixing before we merge. Both Docker Compose setups run the API and WebUI separately while sharing storage. With the default I reproduced that setup locally with two processes. I paused a real So a user deleting what looks like an old history entry can remove an active API task's files and break video generation. Existing users of the default Compose setup would be exposed to this after upgrading. A missing record in the WebUI process is not enough to establish that a task has stopped. Could you account for activity in other processes before allowing deletion, for example through shared task state or an inter-process task lock, and add a regression test for this case alongside the orphaned-task cleanup test? The delete-button condition needs the same protection as I'll hold off on merging until this is addressed. Thanks again for the contribution! |
With the default enable_redis=false config, each process (API, WebUI, CLI) keeps its own isolated in-memory task registry via MemoryState. The previous commit's fix only checked THIS process's view of a task (sm.state.get_task() and the WebUI session's active-generation registry), so a task started in a different process -- not yet produced a video, therefore showing up here as source="history", state=None -- looked identical to a genuinely idle orphaned task. Added a disk-based lock (app/services/task.py: _mark_task_active() / _clear_task_active(), held for the whole duration of start(), the single entry point every caller's pipeline run goes through) that any process can see, since the task directory is the one thing they all share without Redis. is_task_actively_running() checks this lock and is now consulted alongside the existing in-process checks at both WebUI call sites. A stale lock (left behind by a process that died without reaching start()'s finally block) expires after an hour so a crash doesn't block deletion forever either. Added test/services/test_task_active_lock.py, including a test that pauses start() mid-pipeline (mocking _run_pipeline) and asserts the lock is held for that whole window and cleared afterward, matching the reported repro.
|
Good catch, thanks for reproducing that -- you're right, and I should have thought about the separate-process case more carefully. Added a disk-based lock in A process that dies without reaching the Added |
|
Thanks for the update. The new lock does block deletion in the short-running two-process case. I retested b6698a0, but there's still a problem with the one-hour expiry. The lock is written once and never refreshed, so I reproduced this with two separate processes, using the same controlled pause at the audio-generation boundary as before. With a fresh lock, the WebUI refused deletion. After setting the lock's mtime to 3601 seconds ago, the worker was still alive at state 4 / progress 20, but the WebUI's With the default Could you use a cross-process lock or a renewable lease with reliable ownership/liveness checks, rather than treating the age alone as proof that the task has stopped? Please add a two-process regression covering a worker that's still alive past the expiry, alongside crash recovery and orphan cleanup. I also reproduced the three failures currently reported by CI:
Please update those fixtures and rerun the checks as well. I'll keep this PR on hold until these are addressed. Thanks again for working through the cross-process case! |
…s-process liveness The disk lock's age alone couldn't tell "still running, just slow" apart from "the process that held this died." A task running past the one-hour staleness cutoff would have its lock treated as stale even while the worker was still alive in another process, letting the WebUI delete its files out from under it. start() now refreshes the lock every 300 seconds for as long as the pipeline runs, so a genuinely alive task's lock never actually reaches the staleness cutoff -- only a lock that stops being refreshed (because the process that held it really did die) ages out, same as before. Also fixes the three CI failures from the previous round: - utils.task_dir is called with a task_id argument now (by _active_lock_path), so the test fixture's zero-arg stub needs to accept one too. - the ast-exec test double for _delete_task needs an is_task_actively_running stub on its tm namespace now that the real function calls it.
|
Yeah, that's a real gap in the last patch - the lock was basically a timestamp, not a lease, so anything past an hour looked dead whether or not it actually was. Fixed it properly this time: start() now kicks off a background thread that re-touches the lock every 5 minutes for as long as the pipeline is running, and stops it in the finally block right alongside the existing cleanup. So a task that's still alive keeps renewing its own lock and never actually hits the staleness cutoff - only a lock that stops getting renewed (because the process really did die) ages out, same as before. Wrote a test that follows your exact repro: backdates the lock past the cutoff while the pipeline is still "running" inside start(), confirms it reads as stale at that moment, then waits for a real heartbeat tick from the actual background thread and confirms it reads as live again. I also double-checked this one for real by temporarily commenting out the heartbeat_thread.start() call and confirming the test fails with the right assertion message, so it's not just checking something that'd always pass. Also fixed the three CI failures:
Full test suite is green locally (ran the whole thing with uv + ffmpeg installed to match CI, 2437 passed, one unrelated failure in test_video_project.py that I confirmed is pre-existing on main too - looks like a local ffmpeg build difference, not anything to do with this PR). |
What this fixes
#1663 changed
is_task_busy()so that any unparseablestate(including a legacy string, orNone) is treated as busy, to stop the delete API from canceling a task whose live record came back in a shape it couldn't read. That part is correct, and its own test (test_is_task_busy_string_state.py) still passes unchanged with this PR.The side effect:
_scan_history_tasks()inwebui/Main.pysetsstate=Nonefor disk-only task entries that never produced a video file. This is not a rare case —enable_redis = falseis the shipped default (config.example.toml), which means task state lives only inMemoryState's in-process dict, and the WebUI's "active generation" registry lives only in Streamlit'ssession_state. Both are wiped on every restart. So with the default config, any task that was still generating when the app restarted (crash, redeploy, manual restart) — and hadn't produced a video yet — comes back after restart as a disk-only history entry withstate=None.After #1663, that
Nonenow reads as "busy" — even though nothing is actually running for a task with no live tracked state. Both the Delete button'sdisabledflag and_delete_task()'s own guard callis_task_busy()on these entries, so they end up permanently undeletable from the WebUI's "All Tasks" view, with a misleading "(Task Status Processing)" tooltip on a task that was never processing and never will be again.Reproduction
I can attach the full standalone script if useful — it imports nothing from the project and just runs both the before/after
is_task_busy()bodies against this exact task shape, asserting the before/after difference.The fix
Only the two WebUI call sites change, not
is_task_busy()itself:_delete_task(), the busy check now only runs when the task has a live tracked record (sm.state.get_task()returned something) or is in this session's active-generation registry. A disk-history entry with neither has nothing that could be processing or cross-posting it, whatever state label the history scan assigned it.disabledflag — a row withsource == "history"is never blocked byis_task_busy().Tasks with a live Redis record, or ones actively generating in this session, keep exactly the conservative behavior #1663 added.
Testing
test_is_task_busy_string_state.pyis untouched and still passes, sinceis_task_busy()itself isn't modified.Found via VeriQA, a tool I've been building on the side to catch these kinds of cross-file bugs automatically — manually verified the root cause and fix before submitting this PR.