Skip to content

fix(webui): keep disk-history tasks deletable when they have no video - #1706

Open
kapil971390 wants to merge 3 commits into
harry0703:mainfrom
kapil971390:fix/keep-tasks-deletable-no-live-record
Open

kapil971390 wants to merge 3 commits into
harry0703:mainfrom
kapil971390:fix/keep-tasks-deletable-no-live-record

Conversation

@kapil971390

@kapil971390 kapil971390 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

#1663 changed is_task_busy() so that any unparseable state (including a legacy string, or None) 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() in webui/Main.py sets state=None for disk-only task entries that never produced a video file. This is not a rare case — enable_redis = false is the shipped default (config.example.toml), which means task state lives only in MemoryState's in-process dict, and the WebUI's "active generation" registry lives only in Streamlit's session_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 with state=None.

After #1663, that None now reads as "busy" — even though nothing is actually running for a task with no live tracked state. Both the Delete button's disabled flag and _delete_task()'s own guard call is_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

# Real is_task_busy() from #1663, and the exact task shape
# _scan_history_tasks() builds for a no-video history entry
# (webui/Main.py: "state": const.TASK_STATE_COMPLETE if video_file else None)

orphaned_task = {"task_id": "old-task", "state": None, "progress": 0}

# before #1663: is_task_busy(orphaned_task) -> False  (deletable)
# after  #1663: is_task_busy(orphaned_task) -> True   (permanently blocked)

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:

  • In _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.
  • In the task list render loop, the same reasoning gates the Delete button's disabled flag — a row with source == "history" is never blocked by is_task_busy().

Tasks with a live Redis record, or ones actively generating in this session, keep exactly the conservative behavior #1663 added.

Testing

  • Verified the patched logic against 4 scenarios: the regression case (orphaned/no-video history task — now deletable), a genuinely live processing task (stays blocked), a live record with a legacy/corrupted string state matching fix(task): treat unparseable task state as busy #1663's original intent (stays blocked), and a history row that does have a video (unaffected).
  • test_is_task_busy_string_state.py is untouched and still passes, since is_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.

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.
@harry0703

Copy link
Copy Markdown
Owner

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 enable_redis = false, they have separate MemoryState instances, and the WebUI session does not know about an API task's active-generation entry. Before that task has a final video, it can therefore appear in the WebUI as source="history", state=None, even while it is still running.

I reproduced that setup locally with two processes. I paused a real task.start() at the audio-generation boundary, with the worker still alive at state 4 / progress 20, then called the WebUI's _delete_task() from the other process. The existing code refused deletion. With this patch, it returned True and removed the whole task directory, including script.json, while the worker was still running. This was a controlled pause to test deletion, rather than a full concurrent render.

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 _delete_task().

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.
@kapil971390

Copy link
Copy Markdown
Contributor Author

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 start() (the single entry point every pipeline run goes through, regardless of which process calls it): it writes .active.lock into the task directory before _run_pipeline() runs and removes it in a finally block, so it clears on success, failure, or an unhandled exception. is_task_actively_running() checks that lock and is now consulted at both WebUI call sites alongside the existing in-process checks -- so a task started by the API process (or any other process) still blocks deletion in the WebUI, even though the WebUI's own MemoryState and session registry know nothing about it.

A process that dies without reaching the finally (kill -9, OOM, host reboot) would leave the lock file behind, so it expires after an hour -- long enough for any real pipeline run, short enough that a crash doesn't make the task undeletable forever either.

Added test/services/test_task_active_lock.py, including a test that mocks _run_pipeline to pause mid-call and asserts the lock is held for that whole window and cleared once start() returns -- meant to match your repro as closely as I could without an actual second process. Also re-ran the original test_is_task_busy_string_state.py to confirm nothing there regressed.

@harry0703

Copy link
Copy Markdown
Owner

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 is_task_actively_running() returns false after 3600 seconds even if the worker is still alive. There isn't a one-hour limit on the pipeline itself.

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 _delete_task() returned True and removed the task directory and script.json. I simulated the elapsed hour by changing the timestamp; I didn't wait an hour or run a full concurrent render.

With the default enable_redis = false and separate API/WebUI processes sharing storage, a task running for over an hour can therefore look like an idle history entry and have its working files deleted. That can break video generation, and existing Compose users would be exposed after upgrading.

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:

  • The two tests in test_webui_headless_task_actions.py replace utils.task_dir with a zero-argument lambda. The new check passes task_id, causing a TypeError before the buttons render.
  • test_webui_delete_keeps_task_state_when_file_removal_fails uses a tm test double without is_task_actively_running, causing an AttributeError.

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.
@kapil971390

Copy link
Copy Markdown
Contributor Author

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:

  • the task_dir stub in the headless-task test only took zero args, but _active_lock_path now calls it with task_id - updated it to accept an optional arg like the real function
  • the ast-exec test double for _delete_task was missing is_task_actively_running on its tm stand-in, added it as a no-op False

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants