Skip to content

fix: cancel_task returns 404 after successful cancellation and KeyError for forum in search#659

Open
octo-patch wants to merge 1 commit into
666ghj:mainfrom
octo-patch:fix/issue-630-cancel-task-404
Open

fix: cancel_task returns 404 after successful cancellation and KeyError for forum in search#659
octo-patch wants to merge 1 commit into
666ghj:mainfrom
octo-patch:fix/issue-630-cancel-task-404

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #630

Problem

Two bugs were reported in issue #630:

1. cancel_task always returns 404 after cancelling the current running task

When current_task matched the given task_id and its status was "running":

  1. current_task.update_status("cancelled", ...) changed the status to "cancelled".
  2. current_task = None cleared the reference.
  3. task = tasks_registry.get(task_id) retrieved the same object from the registry.
  4. if task and task.status == 'running' — failed, because status is now "cancelled".
  5. The function fell through to the else branch and returned 404, even though cancellation succeeded.

2. KeyError crash in the search endpoint when forum is running

api_ports = {'insight': 8501, 'media': 8502, 'query': 8503} has no 'forum' key, but running_apps is built from all processes entries (which includes 'forum'). The direct dict access api_ports[app_name] raised KeyError when forum was running.

Solution

Bug 1: Introduce a cancelled flag. Set it to True immediately after cancelling current_task, and check it before falling through to the registry lookup. The registry check (task.status == 'running') is only performed when current_task was not responsible for the cancellation.

Bug 2: Replace api_ports[app_name] with api_ports.get(app_name). If the port is None (e.g. for forum, which has no searchable web API), skip that app with continue.

Testing

Added tests/test_cancel_task.py with 5 tests that verify:

  • Cancelling the active running current_task now returns 200 (regression test for the main bug)
  • Non-existent tasks still return 404
  • Running tasks in the registry (not current) are cancelled successfully
  • Already-completed tasks correctly return 404
  • A non-running current_task is cleared from the global but not re-cancelled

… missing forum key in api_ports

Fixes 666ghj#630

Two bugs are fixed:

1. cancel_task in ReportEngine/flask_interface.py: When the current running
   task was cancelled, its status was updated to 'cancelled' before the
   registry lookup checked `task.status == 'running'`, causing the check to
   fail and the endpoint to return 404 even though cancellation succeeded.
   Fixed by tracking whether cancellation already happened via a `cancelled`
   flag before falling through to the registry check.

2. app.py search endpoint: `api_ports` lacked a 'forum' key but
   `running_apps` can include 'forum', causing a KeyError crash. Fixed by
   using `.get()` and skipping apps without a searchable API port (forum is a
   background monitor with no web API).
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cancel_task returns 404 even after successfully cancelling a task

1 participant