Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions backend/api_v2/deployment_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from utils.constants import Account, CeleryQueue
from utils.local_context import StateStore
from workflow_manager.endpoint_v2.destination import DestinationConnector
from workflow_manager.endpoint_v2.result_cache_utils import ResultCacheUtils
from workflow_manager.endpoint_v2.source import SourceConnector
from workflow_manager.workflow_v2.dto import ExecutionResponse
from workflow_manager.workflow_v2.enums import ExecutionStatus
Expand Down Expand Up @@ -293,7 +294,14 @@ def execute_workflow(
logger.exception(f"Failed to mark execution {execution_id} as ERROR")

# Async job never started — release the rate limit slot and clean up.
APIDeploymentRateLimiter.release_slot(api.organization, str(execution_id))
# str(...organization_id), NOT the model instance: release_slot formats
# its argument into the Redis key, and acquire_slot built that key from
# str(organization.organization_id). Passing the instance ZREMs a
# non-member — it returns 0 and raises nothing, so the slot silently
# stays held for the full TTL. Same trap as undispatched_sweep.py:245.
APIDeploymentRateLimiter.release_slot(
str(api.organization.organization_id), str(execution_id)
)
DestinationConnector.delete_api_storage_dir(
workflow_id=workflow_id, execution_id=execution_id
)
Expand All @@ -306,6 +314,45 @@ def execute_workflow(
)
).data

# Staging rejected every file, so there is nothing to dispatch. The worker
# short-circuits an empty file set without writing a status back, which
# would strand this execution in PENDING — terminalise it here instead.
if not hash_values_of_files:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] [Lens 15] — The two fixes for the same scenario disagree on total_files

The worker sets total_files=0 (workers/api-deployment/tasks.py:230); this branch leaves it at the creation-time len(file_objs) (deployment_helper.py:241). An all-rejected run therefore lands COMPLETED with total_files=1 and zero file executions.

Cosmetic in the API response (which reads the result cache), but the executions list shows a completed run whose counts do not add up.

Suggested fix — have update_execution_completed zero the count, or accept a total_files argument, so both paths agree.

# Isolate the DB write the way the staging-failure path above does, so
# the rate limit slot and staging dir are released even if it raises.
execution = None
try:
execution = WorkflowExecutionServiceHelper.update_execution_completed(
str(execution_id),
total_files=len(file_objs),
failed_files=len(file_objs),
)
except Exception:
logger.exception(f"Failed to mark execution {execution_id} as COMPLETED")

APIDeploymentRateLimiter.release_slot(
str(api.organization.organization_id), str(execution_id)
)
DestinationConnector.delete_api_storage_dir(
workflow_id=workflow_id, execution_id=execution_id
)
Comment on lines +320 to +338

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] [Lens 3 · 8] — this branch can raise before its own cleanup runs

Failure mode. update_execution_completed catches only WorkflowExecution.DoesNotExist (execution.py:393-401). Any other DB failure — OperationalError, a statement or lock timeout on the select_for_update inside update_execution (models/execution.py:418-423), a deadlock, a dropped connection — propagates out of execute_workflow, so line 315 and lines 316-318 never run. The org's rate-limit slot stays held for the full 6h TTL and throttles every other API-deployment call for that org, the staging dir is never deleted, the row stays PENDING, and the caller gets a 500 with no execution id to poll.

Evidence. The sibling block immediately above (lines 289-308) deliberately isolates its DB write in an inner try/except with logger.exception so that cleanup always runs, and has a regression test pinning exactly that: test_staging_failure_cleanup_survives_db_marking_error (tests/test_deployment_helper.py:75-91). The new path copies the shape but not the guard, and has no equivalent test.

Suggested fix. Wrap the update_execution_completed call in its own try/except Exception: logger.exception(...) so release_slot and delete_api_storage_dir always execute, and add the mirror-image test.

Confidence: High.

# Report the stored status rather than asserting COMPLETED: the row may
# be missing, or the terminal guard may have refused the change. Claiming
# success here would only hide the stranded execution behind a 200 that a
# follow-up GET /status then contradicts.
return APIExecutionResponseSerializer(
ExecutionResponse(
workflow_id=workflow_id,
execution_id=execution_id,
execution_status=(
execution.status if execution else ExecutionStatus.ERROR.value
),
result=ResultCacheUtils.get_api_results(
workflow_id=str(workflow_id), execution_id=str(execution_id)
),
)
).data
Comment on lines +343 to +354

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] [Lens 3 · 10] — the response asserts COMPLETED whether or not the status write landed

Failure mode. There are three ways the call on line 314 returns normally without the row reaching COMPLETED:

  1. row missing — execution.py:399-401 logs and returns None;
  2. row vanished under the lock — models/execution.py:424-425, if locked is None: return, silent;
  3. row already terminal with a different value — models/execution.py:520-535 refuses, logs a warning, returns ([], False).

The return value is discarded and execution_status on line 323 is a hardcoded literal rather than the row's actual status. The API then answers COMPLETED while a follow-up GET /status/<execution_id> reads the DB and returns PENDING — the stranded-execution bug this PR exists to fix, now concealed behind a success response instead of being visible.

update_execution_completed was given a WorkflowExecution | None return type to carry exactly this signal, and no caller reads it.

Suggested fix. Bind the result: if it is None, or its status is not COMPLETED, log at error level and return the row's real status (or ERROR) rather than claiming COMPLETED.

Confidence: High.


try:
result = WorkflowHelper.execute_workflow_async(
workflow_id=workflow_id,
Expand Down Expand Up @@ -352,7 +399,9 @@ def execute_workflow(
# Dispatch failures are marked ERROR internally by execute_workflow_async;
# post-dispatch failures (enrichment/config) must not overwrite a running
# execution's status, so only release the slot and clean up storage here.
APIDeploymentRateLimiter.release_slot(api.organization, str(execution_id))
APIDeploymentRateLimiter.release_slot(
str(api.organization.organization_id), str(execution_id)
)

# Clean up storage
DestinationConnector.delete_api_storage_dir(
Expand Down
132 changes: 127 additions & 5 deletions backend/api_v2/tests/test_deployment_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def collaborators():
mocks[
"WorkflowExecutionServiceHelper"
].create_workflow_execution.return_value = execution_row
mocks["SourceConnector"].add_input_file_to_api_storage.side_effect = (
RuntimeError("boom")
mocks["SourceConnector"].add_input_file_to_api_storage.side_effect = RuntimeError(
"boom"
)
yield mocks

Expand All @@ -48,6 +48,7 @@ def _api() -> MagicMock:
api = MagicMock()
api.workflow.id = "wf-1"
api.id = "pipe-1"
api.organization.organization_id = "org-uuid-1"
return api


Expand All @@ -74,9 +75,9 @@ def test_staging_failure_marks_execution_error(collaborators) -> None:

def test_staging_failure_cleanup_survives_db_marking_error(collaborators) -> None:
"""If marking the row ERROR itself raises, cleanup must still run (not propagate)."""
collaborators["WorkflowExecutionServiceHelper"].update_execution_err.side_effect = (
RuntimeError("db down")
)
collaborators[
"WorkflowExecutionServiceHelper"
].update_execution_err.side_effect = RuntimeError("db down")

# Must NOT raise — a failed error-marking should not break cleanup.
dh.DeploymentHelper.execute_workflow(
Expand All @@ -89,3 +90,124 @@ def test_staging_failure_cleanup_survives_db_marking_error(collaborators) -> Non
# Cleanup still runs even though error-marking raised.
collaborators["APIDeploymentRateLimiter"].release_slot.assert_called_once()
collaborators["DestinationConnector"].delete_api_storage_dir.assert_called_once()


@pytest.fixture
def staging_rejects_everything():
"""Patch execute_workflow's collaborators; staging returns no dispatchable files."""
with mock.patch.multiple(
dh,
WorkflowExecutionServiceHelper=mock.DEFAULT,
SourceConnector=mock.DEFAULT,
DestinationConnector=mock.DEFAULT,
APIDeploymentRateLimiter=mock.DEFAULT,
WorkflowHelper=mock.DEFAULT,
ResultCacheUtils=mock.DEFAULT,
Tag=mock.DEFAULT,
logger=mock.DEFAULT,
) as mocks:
execution_row = MagicMock()
execution_row.id = "exec-123"
mocks[
"WorkflowExecutionServiceHelper"
].create_workflow_execution.return_value = execution_row
mocks["SourceConnector"].add_input_file_to_api_storage.return_value = {}
mocks["ResultCacheUtils"].get_api_results.return_value = [
{"file": "evil.pdf", "status": "Failed", "error": "unsupported MIME type"}
]
completed_row = MagicMock()
completed_row.status = "COMPLETED"
mocks[
"WorkflowExecutionServiceHelper"
].update_execution_completed.return_value = completed_row
yield mocks


def test_all_files_rejected_completes_without_dispatch(
staging_rejects_everything,
) -> None:
"""A request whose every file is rejected must reach a terminal status.

The worker short-circuits an empty file set without writing a status back, so
dispatching one strands the execution in PENDING and the caller polls forever.
"""
mocks = staging_rejects_everything
# A non-empty upload whose staging result is empty. Passing [] instead would
# leave the branch satisfied by `not file_objs` too, and the original bug -
# dispatching a request whose files were all rejected - would pass this test.
response = dh.DeploymentHelper.execute_workflow(
organization_name="org",
api=_api(),
file_objs=[MagicMock()],
timeout=-1,
)
Comment on lines +138 to +143

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] [Lens 13] — the guard's predicate is unpinned; the original bug can be reintroduced with the suite green

Failure mode. This test passes file_objs=[] with SourceConnector fully mocked, so it exercises the zero-files-uploaded path, not the zero-files-staged path the guard exists for. Nothing in the suite asserts that a non-empty upload whose staging result is empty takes the short-circuit, and nothing asserts the short-circuit does not fire when staging returns files.

Evidence (mutants run against the branch, then reverted):

  • if not hash_values_of_files: -> if not file_objs: at deployment_helper.py:3133/3 pass. That mutant is the production bug verbatim: one HTML file uploaded, staging rejects it and returns {}, file_objs is non-empty, control falls through to execute_workflow_async, execution stranded in PENDING.
  • if not hash_values_of_files: -> if True: — the whole backend/api_v2/tests/ suite is identical to baseline (48 passed).
  • Control: deleting the update_execution_completed call does fail this test, so it pins the branch body, not the branch condition.

Also worth noting: assert response["result"][0]["status"] == "Failed" on line 148 reads back the fixture's own literal from line 115, so it proves the branch forwards the cache verbatim, not what source.py writes.

Suggested fix. Pass a non-empty file_objs (a bare MagicMock() suffices — with SourceConnector mocked, the only read is len(file_objs) at deployment_helper.py:243) so the two cases become distinguishable, and add a sibling test with add_input_file_to_api_storage.return_value = {"good.pdf": MagicMock()} asserting execute_workflow_async is called and update_execution_completed is not. Parametrising timeout over {-1, 10} closes the untested synchronous path at negligible cost.

Confidence: High (mutants executed).


# Nothing is dispatched...
mocks["WorkflowHelper"].execute_workflow_async.assert_not_called()
# ...the row is terminalised here instead of being left PENDING, and the
# counters are written so the run does not read back as a clean success...
mocks[
"WorkflowExecutionServiceHelper"
].update_execution_completed.assert_called_once_with(
"exec-123", total_files=1, failed_files=1
)
# ...the slot and staging dir are released. The slot must be released by org
# id string: release_slot formats its argument into the Redis key, so passing
# the model instance removes a non-member and silently holds the slot.
mocks["APIDeploymentRateLimiter"].release_slot.assert_called_once_with(
"org-uuid-1", "exec-123"
)
mocks["DestinationConnector"].delete_api_storage_dir.assert_called_once()
# ...and the caller still sees why each file failed.
assert response["execution_status"] == "COMPLETED"
assert response["result"][0]["file"] == "evil.pdf"
assert response["result"][0]["status"] == "Failed"


def test_files_staged_successfully_are_dispatched(staging_rejects_everything) -> None:
"""The short-circuit must not fire when staging did return files.

Sibling to the test above: together they pin the branch to the staging result
rather than to the upload list.
"""
mocks = staging_rejects_everything
mocks["SourceConnector"].add_input_file_to_api_storage.return_value = {
"good.pdf": MagicMock()
}

dh.DeploymentHelper.execute_workflow(
organization_name="org",
api=_api(),
file_objs=[MagicMock()],
timeout=-1,
)

mocks["WorkflowHelper"].execute_workflow_async.assert_called_once()
mocks["WorkflowExecutionServiceHelper"].update_execution_completed.assert_not_called()


def test_all_files_rejected_cleanup_survives_db_marking_error(
staging_rejects_everything,
) -> None:
"""A failing status write must not strand the slot or the staging dir.

update_execution_completed only catches DoesNotExist, so a lock timeout or a
dropped connection propagates; without isolation the org's rate limit slot
stays held for its full TTL and throttles every other call for that org.
"""
mocks = staging_rejects_everything
mocks[
"WorkflowExecutionServiceHelper"
].update_execution_completed.side_effect = Exception("db is down")

response = dh.DeploymentHelper.execute_workflow(
organization_name="org",
api=_api(),
file_objs=[MagicMock()],
timeout=-1,
)

mocks["APIDeploymentRateLimiter"].release_slot.assert_called_once()
mocks["DestinationConnector"].delete_api_storage_dir.assert_called_once()
# The row never reached COMPLETED, so the response must not claim it did.
assert response["execution_status"] == "ERROR"
108 changes: 86 additions & 22 deletions backend/workflow_manager/endpoint_v2/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import os
import shutil
import uuid
from collections.abc import Collection
from hashlib import sha256
from io import BytesIO
Expand All @@ -25,7 +24,11 @@
SourceConstant,
SourceKey,
)
from workflow_manager.endpoint_v2.dto import FileHash, SourceConfig
from workflow_manager.endpoint_v2.dto import (
FileExecutionResult,
FileHash,
SourceConfig,
)
from workflow_manager.endpoint_v2.enums import AllowedFileTypes
from workflow_manager.endpoint_v2.exceptions import (
InvalidInputDirectory,
Expand All @@ -37,6 +40,7 @@
UnsupportedMimeTypeError,
)
from workflow_manager.endpoint_v2.models import WorkflowEndpoint
from workflow_manager.endpoint_v2.result_cache_utils import ResultCacheUtils
from workflow_manager.file_execution.models import WorkflowFileExecution
from workflow_manager.utils.workflow_log import WorkflowLog
from workflow_manager.workflow_v2.enums import ExecutionStatus
Expand Down Expand Up @@ -69,6 +73,15 @@ class SourceConnector(BaseConnector):
"""

READ_CHUNK_SIZE = 4194304 # Chunk size for reading files
# Most formats are identifiable from their leading bytes, so a small sample
# keeps the common path cheap.
MIME_DETECT_CHUNK_SIZE = 8192
# These two carry the real format in a structure libmagic can only reach by
# reading the whole file: the OLE2 directory sector and the zip central
# directory both sit at the end. A sample of any size reports the container
# rather than the .doc/.xls/.ppt or .docx/.xlsx/.pptx inside it, so these
# must never be resolved from the sample alone.
CONTAINER_MIME_TYPES = frozenset({"application/x-ole-storage", "application/zip"})

def __init__(
self,
Expand Down Expand Up @@ -1187,6 +1200,44 @@ def load_file(self, input_file_path: str) -> tuple[str, BytesIO]:

return os.path.basename(input_file_path), file_stream

@classmethod
def _detect_uploaded_file_mime_type(cls, file: UploadedFile) -> str:
"""Detect an uploaded file's MIME type from its own bytes.

The multipart Content-Type is supplied by the caller and never verified,
so it cannot be used to decide what is allowed into API storage.
"""
sample = file.read(cls.MIME_DETECT_CHUNK_SIZE)
file.seek(0)
if not sample:
# libmagic reports "application/x-empty" here, which would reject the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] [Lens 13] — The two deliberate new branches are the two without tests

This empty-upload branch exists specifically to preserve the downstream EmptyFileError path — it is the one branch of _detect_uploaded_file_mime_type reachable only with zero bytes, and nothing pins it. Delete it and the suite stays green while empty uploads start being relabelled as unsupported-type failures, which is the exact outcome the comment says to avoid.

Same for workers/api-deployment/tasks.py:225-231: nothing asserts the worker now persists the status, which is the whole point of that hunk.

Suggested fix — two assertions:

  • _stage([_upload("empty.pdf", b"", "application/pdf")]) returns the file staged with mime_type == "application/octet-stream".
  • A worker test asserting update_workflow_execution_status is called with COMPLETED on the empty short-circuit.

Noted in the PR's favour: the existing 5 tests were verified to discriminate (reverting the detection line fails 4), which is more than most PRs do.

# file as an unsupported type. An empty upload is a distinct failure
# and is reported as such once staging hands off, so let it pass.
return AllowedFileTypes.OCTET_STREAM.value

mime_type = magic.from_buffer(sample, mime=True)
if mime_type not in cls.CONTAINER_MIME_TYPES:
return mime_type
return cls._detect_container_mime_type(file, fallback=mime_type)

@classmethod
def _detect_container_mime_type(cls, file: UploadedFile, fallback: str) -> str:
"""Resolve a container format by classifying the file in full.

Django spills uploads over FILE_UPLOAD_MAX_MEMORY_SIZE to disk, so this
hands libmagic the path when there is one and only buffers the whole
upload for the in-memory case, where that ceiling already bounds it.
"""
temporary_file_path = getattr(file, "temporary_file_path", None)
if temporary_file_path is not None:
return magic.from_file(temporary_file_path(), mime=True)

content = file.read()
file.seek(0)
if not content:
return fallback
return magic.from_buffer(content, mime=True)

@classmethod
def add_input_file_to_api_storage(
cls,
Expand Down Expand Up @@ -1228,30 +1279,43 @@ def add_input_file_to_api_storage(
file_name = file.name
destination_path = os.path.join(api_storage_dir, file_name)

mime_type = file.content_type
logger.info(f"Detected MIME type: {mime_type} for file {file_name}")
if not mime_type:
logger.info(
f"MIME type not found for file {file_name}, using default MIME type: {AllowedFileTypes.OCTET_STREAM.value}"
try:
mime_type = cls._detect_uploaded_file_mime_type(file)
except Exception:
# Detection reads the upload, so a broken stream raises here. Fail
# this one file instead of the whole request, and say that detection
# failed rather than blaming the file's type - an I/O fault and an
# unsupported format need different follow-ups.
log_message = (
f"Rejecting file '{file_name}': could not determine its type"
)
mime_type = AllowedFileTypes.OCTET_STREAM.value
logger.exception(log_message)
workflow_log.log_error(logger=logger, message=log_message)
ResultCacheUtils.update_api_results(
workflow_id=workflow_id,
execution_id=execution_id,
api_result=FileExecutionResult(file=file_name, error=log_message),
)
continue

logger.info(f"Detected MIME type: {mime_type} for file {file_name}")

if not AllowedFileTypes.is_allowed(mime_type):
log_message = f"Skipping file '{file_name}' to stage due to unsupported MIME type '{mime_type}'"
workflow_log.log_info(logger=logger, message=log_message)
# Generate a clearly marked temporary hash to avoid reading the file content
# Helps to prevent duplicate entries in file executions
fake_hash = f"temp-hash-{uuid.uuid4().hex}"
file_hash = FileHash(
file_path=destination_path,
source_connection_type=connection_type,
file_name=file_name,
file_hash=fake_hash,
is_executed=True,
file_size=file.size,
mime_type=mime_type,
log_message = (
f"Rejecting file '{file_name}' with unsupported MIME type "
f"'{mime_type}'"
)
workflow_log.log_error(logger=logger, message=log_message)
# Rejected files are never dispatched, so nothing downstream will
# report on them - surface the failure in the API response here.
ResultCacheUtils.update_api_results(
workflow_id=workflow_id,
execution_id=execution_id,
api_result=FileExecutionResult(
file=file_name,
error=log_message,
),
Comment on lines +1309 to +1317

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[High] [Lens 5 · 10] — a rejected file leaves no durable record, and an all-rejected run is stored as a clean success

Failure mode. Before this change a rejected file produced a FileHash, was dispatched, and the worker's own libmagic check (workers/shared/workflow/execution/service.py:1223-1229) created a real WorkflowFileExecution row. After this change it produces no FileHash, no WorkflowFileExecution, no file-history row. The rejection exists only as an entry in the Redis list api_results:{workflow_id}:{execution_id}, which is deleted the first time anyone polls /status (workflow_helper.py:451-453), expires after EXECUTION_RESULT_TTL_SECONDS (3h default), and is gone on any eviction or restart. After any of those, nothing in Postgres can answer "why was my file not processed?".

Compounding it on the all-rejected path: deployment_helper.py:313-318 writes only status=COMPLETED. The row keeps total_files = len(file_objs) from line 243 while failed_files and successful_files stay NULL (models/execution.py:191-208, nullable, no default). is_failure_run is is_failure(status) or (failed_files or 0) > 0 (unstract/core/.../data_models.py:663), so COMPLETED + NULL reads as a success — the response body says every file Failed while the execution row says N files, zero failures. This is the hazard already written up at internal_views.py:546-550 ("a terminal status with failed_files=None ... silently bypasses notify_on_failures subscribers"). Run history is affected too: get_last_run_statuses derives PARTIAL_SUCCESS from these counters (models/execution.py:622-636).

Separately, the early return never reaches PipelineUtils.update_pipeline_status, the only dispatcher of API-deployment notifications (pipeline_utils.py:58 -> APIDeploymentUtils.send_notification), so an all-rejected request now sends no webhook at all where the dispatched-and-failed run previously alerted.

Note also that the worker-side check already raises UnsupportedMimeTypeError naming the file and the MIME type, which softens the PR description's premise that an unsupported file today "fails at extraction with an error that does not name the real cause".

Suggested fix. Write the aggregates alongside the status (failed_files=len(file_objs), successful_files=0), and keep a persisted per-file record for a rejected file — a WorkflowFileExecution row in terminal ERROR carrying the real MIME type — so the rejection is auditable after the cache entry is gone. If cache-only is deliberate, it is worth stating in the PR description as a support/audit trade-off.

Confidence: High.

)
file_hashes.update({file_name: file_hash})
continue

file_system = FileSystem(FileStorageType.API_EXECUTION)
Expand Down
Loading
Loading