You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid duplicate parse dispatch when S3 notification races confirm-upload #286
A file upload can dispatch two parse_task messages when the S3 ObjectCreated notification races the explicit POST /v1/jobs/{job_id}/confirm-upload fallback.
This was observed during the 2026-08-14 staging Fargate queue-drain drill for job_27204d831b6a:
confirm-upload read the Job in waiting-file and dispatched task 3c0331ef-f6ef-4c5c-aee7-f321696eceed.
The S3 SNS event had concurrently loaded the same waiting-file Job. After one CAS retry, its handoff accepted pending → pending and dispatched task 4bb9aa00-037a-4674-b052-ceaaf8d6b562.
RedisJobLock allowed only one task to process. The duplicate retried after 120 seconds, observed terminal state done, and skipped.
The Job completed once and billing remained exactly one page (0.015 credits).
The current safety layers prevent duplicate processing and billing, but the redundant dispatch consumes worker/broker capacity and creates avoidable retry noise.
Confirmed cause
DocumentIngestionConfirmationService permits both waiting-file and pending, while the S3 upload handler guards on a previously loaded waiting-file row. Both paths call DocumentIngestionHandoffService.start_uploaded_file_workflow(). Same-state transitions are valid, and a successful pending → pending outcome still calls the worker dispatcher.
This is an existing application-level race, not a Fargate/Redis queue-persistence failure, and is non-blocking for Ontos-AI/knowhere-api-infra#19.
Acceptance criteria
A concurrent S3 upload notification and confirm-upload request produce one worker parse dispatch for a Job.
Repeated S3 notifications and repeated confirm-upload requests are idempotent at the dispatch boundary.
confirm-upload remains a functional fallback when the storage event is absent.
Add contract tests at the real handoff/dispatch seam, including the concurrent S3-plus-confirm case.
Preserve one terminal Job result and one billing charge.
Do not weaken RedisJobLock or the worker terminal-state gate; they remain defense-in-depth for broker redelivery and other at-least-once delivery paths.
Scope
Track and fix this separately from the infrastructure cost-reduction work. Do not expand the Fargate migration to include this application optimization.
Investigation: duplicate parse dispatch on S3 vs confirm-upload
I reproduced this on current main without changing schema. Asking maintainers to confirm the proposed fix before opening a PR.
Race window
Upload completion has two triggers that both call DocumentIngestionHandoffService.start_uploaded_file_workflow():
POST /api/v1/internal/s3-events → process_upload_events() (trigger=s3_upload_completed)
POST /api/v1/jobs/{job_id}/confirm-upload → DocumentIngestionConfirmationService (trigger=manual_upload_completed)
confirm-upload allows both waiting-file and pending. The S3 handler only proceeds when the loaded row is waiting-file. Handoff already no-ops when the in-memory snapshot is not waiting-file, and when CAS rejects with invalid_transition from a non-waiting-file state.
That misses the production path. is_valid_transition() treats same-state as valid. After a CAS miss, transition_outcome() retries (up to 3), reloads pending, and accepts pending → pending with succeeded=True. Handoff then dispatches a second parse_task.
This matches the staging note on job_27204d831b6a. RedisJobLock and the worker terminal-state gate still prevent double processing/billing; the extra dispatch is broker/worker noise.
Ordered delivery is already idempotent at this seam: S3-then-confirm and confirm-then-S3 each dispatch once. confirm-upload still works as a fallback when the storage event is absent.
Proposed fix (no schema / Alembic / public API change)
In start_uploaded_file_workflow(), treat dispatch ownership as: only the transition whose from_state is waiting-file may call start_uploaded_file_parse(). A successful pending → pending (or any other same-state retry) is an idempotent no-op.
Side effect ordering is unchanged: transition_outcome(auto_commit=True) commits before returning; Celery apply_async stays after commit.
We would not change global is_valid_transition() same-state rules (retry paths rely on them), the worker lock, or the terminal-state gate.
Rolling deploy: old API replicas can still double-dispatch until replaced; new replicas will not. Existing worker defenses remain in depth.
Contract tests are already written against the real handoff/dispatch seam (HTTP interleaving plus a direct pending → pending outcome). They fail on current main and are intended to pass with the guard above.
Happy to implement this as a small follow-up PR if this approach looks right.
Activity
Frankie-Xu commentedon Aug 25, 2026
Investigation: duplicate parse dispatch on S3 vs confirm-upload
I reproduced this on current
mainwithout changing schema. Asking maintainers to confirm the proposed fix before opening a PR.Race window
Upload completion has two triggers that both call
DocumentIngestionHandoffService.start_uploaded_file_workflow():POST /api/v1/internal/s3-events→process_upload_events()(trigger=s3_upload_completed)POST /api/v1/jobs/{job_id}/confirm-upload→DocumentIngestionConfirmationService(trigger=manual_upload_completed)confirm-uploadallows bothwaiting-fileandpending. The S3 handler only proceeds when the loaded row iswaiting-file. Handoff already no-ops when the in-memory snapshot is notwaiting-file, and when CAS rejects withinvalid_transitionfrom a non-waiting-filestate.That misses the production path.
is_valid_transition()treats same-state as valid. After a CAS miss,transition_outcome()retries (up to 3), reloadspending, and acceptspending → pendingwithsucceeded=True. Handoff then dispatches a secondparse_task.Observed locally (synthetic job, mocked object-storage HEAD, mocked Celery dispatcher):
This matches the staging note on
job_27204d831b6a.RedisJobLockand the worker terminal-state gate still prevent double processing/billing; the extra dispatch is broker/worker noise.Ordered delivery is already idempotent at this seam: S3-then-confirm and confirm-then-S3 each dispatch once.
confirm-uploadstill works as a fallback when the storage event is absent.Proposed fix (no schema / Alembic / public API change)
In
start_uploaded_file_workflow(), treat dispatch ownership as: only the transition whosefrom_stateiswaiting-filemay callstart_uploaded_file_parse(). A successfulpending → pending(or any other same-state retry) is an idempotent no-op.Side effect ordering is unchanged:
transition_outcome(auto_commit=True)commits before returning; Celeryapply_asyncstays after commit.We would not change global
is_valid_transition()same-state rules (retry paths rely on them), the worker lock, or the terminal-state gate.Rolling deploy: old API replicas can still double-dispatch until replaced; new replicas will not. Existing worker defenses remain in depth.
Contract tests are already written against the real handoff/dispatch seam (HTTP interleaving plus a direct
pending → pendingoutcome). They fail on currentmainand are intended to pass with the guard above.Happy to implement this as a small follow-up PR if this approach looks right.