Skip to content

Cancel Snowflake queries when a user kills the deferred task#69635

Open
steveahnahn wants to merge 1 commit into
apache:mainfrom
steveahnahn:snowflake-cancel-queries-on-user-kill-deferred
Open

Cancel Snowflake queries when a user kills the deferred task#69635
steveahnahn wants to merge 1 commit into
apache:mainfrom
steveahnahn:snowflake-cancel-queries-on-user-kill-deferred

Conversation

@steveahnahn

@steveahnahn steveahnahn commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Problem

  • SnowflakeSqlApiOperator.on_kill cancels the running queries, but after defer() the worker is gone and it can never run.
  • SnowflakeSqlApiTrigger had no on_kill (base default is a no op), so killing a deferred task leaves the statements executing on the warehouse, burning credits.
  • Sibling triggers already cancel on user kill: EMR in the Amazon provider, BigQuery/Dataproc/Dataflow in the Google provider.

Change

  • Add on_kill to SnowflakeSqlApiTrigger: cancels the running query ids.
  • The SQL API cancel is a blocking POST with no async variant, so on_kill runs cancel_queries through sync_to_async, building the hook inside that worker so no connection work touches the event loop.
  • cancel_on_kill flag (default True) on operator and trigger; operator threads it into the trigger at the defer site.
  • Best effort: cancel failures are logged and swallowed. Fires only on user kill, never on triggerer restart, redistribution, or timeout.

Live verification

  1. Deferrable operator submitted a statement; task deferred, trigger polling (GET /api/v2/statements/{id} returning 202).

deferred task, trigger polling the statement

  1. Marked the task failed from the UI. Task log:
Trigger cancelled by user action, invoking on_kill
Cancelling Snowflake query ids ['069b68f4-8e5e-404a-aef9-0eca86eb396d']
Snowflake query ids ['069b68f4-8e5e-404a-aef9-0eca86eb396d'] cancelled

task failed, on_kill cancelling the Snowflake query

  1. The endpoint received POST /api/v2/statements/{id}/cancel and the statement transitioned to ABORTED. Without the change it stays running.

Tests

  • Trigger: cancel on kill, disabled/empty id no ops, error swallowing, serialization of the flag.
  • Operator: cancel_on_kill=False guard, flag threaded into the trigger on defer.
  • Every new assertion fails without the source change.

Was generative AI tooling used to co-author this PR?
  • Yes, Claude Code (Fable 5)

Generated-by: Claude Code (Fable 5) following the guidelines

@boring-cyborg boring-cyborg Bot added area:providers provider:snowflake Issues related to Snowflake provider labels Jul 9, 2026
@steveahnahn steveahnahn closed this Jul 9, 2026
@steveahnahn steveahnahn reopened this Jul 10, 2026
@steveahnahn steveahnahn force-pushed the snowflake-cancel-queries-on-user-kill-deferred branch from a7c612b to a878441 Compare July 10, 2026 19:40
@steveahnahn steveahnahn marked this pull request as ready for review July 10, 2026 22:57
@steveahnahn steveahnahn requested a review from potiuk as a code owner July 10, 2026 22:57
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Jul 11, 2026
A deferred SnowflakeSqlApiOperator parks its running query ids in the triggerer,
so the operator's own on_kill no longer runs once the task is deferred. When a
user marks that task failed, clears it, or marks it success, SnowflakeSqlApiTrigger
had no on_kill hook, so the Snowflake statements kept executing on the warehouse,
burning compute credits, even though the operator already cancels the queries on
kill in the non-deferred path.

This adds on_kill to SnowflakeSqlApiTrigger to cancel the running query ids when
the user acts on the deferred task, matching the behaviour already shipped for the
EMR, Dataproc, BigQuery, and Dataflow triggers. The Snowflake SQL API cancel is a
blocking POST with no async variant, so it runs through sync_to_async off the
triggerer event loop, and the hook is built inside that worker so no connection
work touches the loop. A cancel_on_kill flag on both the operator and the trigger
lets users opt out.
@steveahnahn steveahnahn force-pushed the snowflake-cancel-queries-on-user-kill-deferred branch from a878441 to 0bad94d Compare July 16, 2026 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:snowflake Issues related to Snowflake provider ready for maintainer review Set after triaging when all criteria pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants