From 7806ed2b459d95b12876cfac327add582bcd1368 Mon Sep 17 00:00:00 2001 From: Guilhem Lemouel Date: Tue, 27 Oct 2026 01:20:39 -0700 Subject: [PATCH] fix: define a flow's run once, through completed jobs only Co-Authored-By: Claude Opus 5.5 (1M context) --- ...c9e8cec2b12402bca723aa1d4a3762b540090.json | 28 ------------ ...c2da2d42e36530a7c5deef42923600e9907be.json | 34 ++++++++++++++ ...abf1465d961326a18cbcf07cb8bb49d5a2486.json | 15 ------- backend/windmill-api-jobs/src/execution.rs | 20 +++++---- backend/windmill-common/src/jobs.rs | 44 +++++++++++++++++-- backend/windmill-worker/src/worker_flow.rs | 33 +++++--------- 6 files changed, 96 insertions(+), 78 deletions(-) delete mode 100644 backend/.sqlx/query-3674f638d7fc89ec11c4b8beb94c9e8cec2b12402bca723aa1d4a3762b540090.json create mode 100644 backend/.sqlx/query-4493fcace6d3bcc293d871bb1fdc2da2d42e36530a7c5deef42923600e9907be.json delete mode 100644 backend/.sqlx/query-f64307bc2dd518e554461911f83abf1465d961326a18cbcf07cb8bb49d5a2486.json diff --git a/backend/.sqlx/query-3674f638d7fc89ec11c4b8beb94c9e8cec2b12402bca723aa1d4a3762b540090.json b/backend/.sqlx/query-3674f638d7fc89ec11c4b8beb94c9e8cec2b12402bca723aa1d4a3762b540090.json deleted file mode 100644 index 1acaba557c..0000000000 --- a/backend/.sqlx/query-3674f638d7fc89ec11c4b8beb94c9e8cec2b12402bca723aa1d4a3762b540090.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "WITH RECURSIVE run AS (\n SELECT id FROM v2_job WHERE parent_job = $1\n UNION ALL\n SELECT j.id FROM v2_job j JOIN run r ON j.parent_job = r.id\n )\n SELECT j.id AS \"id!\", j.flow_step_id\n FROM run JOIN v2_job j USING (id) JOIN v2_job_completed c USING (id)", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id!", - "type_info": "Uuid" - }, - { - "ordinal": 1, - "name": "flow_step_id", - "type_info": "Varchar" - } - ], - "parameters": { - "Left": [ - "Uuid" - ] - }, - "nullable": [ - false, - true - ] - }, - "hash": "3674f638d7fc89ec11c4b8beb94c9e8cec2b12402bca723aa1d4a3762b540090" -} diff --git a/backend/.sqlx/query-4493fcace6d3bcc293d871bb1fdc2da2d42e36530a7c5deef42923600e9907be.json b/backend/.sqlx/query-4493fcace6d3bcc293d871bb1fdc2da2d42e36530a7c5deef42923600e9907be.json new file mode 100644 index 0000000000..d8af0e7c8f --- /dev/null +++ b/backend/.sqlx/query-4493fcace6d3bcc293d871bb1fdc2da2d42e36530a7c5deef42923600e9907be.json @@ -0,0 +1,34 @@ +{ + "db_name": "PostgreSQL", + "query": "WITH RECURSIVE run AS (\n SELECT j.id, j.flow_step_id, j.kind = 'flow' AS is_flow, false AS in_subflow\n FROM v2_job j JOIN v2_job_completed c ON c.id = j.id\n WHERE j.parent_job = $1\n UNION ALL\n SELECT j.id, j.flow_step_id, j.kind = 'flow', r.in_subflow OR r.is_flow\n FROM v2_job j JOIN v2_job_completed c ON c.id = j.id JOIN run r ON j.parent_job = r.id\n )\n SELECT id AS \"id!\", flow_step_id, in_subflow AS \"in_subflow!\" FROM run", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id!", + "type_info": "Uuid" + }, + { + "ordinal": 1, + "name": "flow_step_id", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "in_subflow!", + "type_info": "Bool" + } + ], + "parameters": { + "Left": [ + "Uuid" + ] + }, + "nullable": [ + null, + null, + null + ] + }, + "hash": "4493fcace6d3bcc293d871bb1fdc2da2d42e36530a7c5deef42923600e9907be" +} diff --git a/backend/.sqlx/query-f64307bc2dd518e554461911f83abf1465d961326a18cbcf07cb8bb49d5a2486.json b/backend/.sqlx/query-f64307bc2dd518e554461911f83abf1465d961326a18cbcf07cb8bb49d5a2486.json deleted file mode 100644 index aeefb39eba..0000000000 --- a/backend/.sqlx/query-f64307bc2dd518e554461911f83abf1465d961326a18cbcf07cb8bb49d5a2486.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "WITH RECURSIVE run AS (\n SELECT id, flow_step_id FROM v2_job WHERE parent_job = $1\n UNION ALL\n SELECT j.id, j.flow_step_id FROM v2_job j JOIN run r ON j.parent_job = r.id\n )\n UPDATE v2_job_completed SET result = '{}'::jsonb\n WHERE id IN (SELECT id FROM run WHERE flow_step_id = $2)", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Uuid", - "Text" - ] - }, - "nullable": [] - }, - "hash": "f64307bc2dd518e554461911f83abf1465d961326a18cbcf07cb8bb49d5a2486" -} diff --git a/backend/windmill-api-jobs/src/execution.rs b/backend/windmill-api-jobs/src/execution.rs index 06605ff32d..c977c78e2d 100644 --- a/backend/windmill-api-jobs/src/execution.rs +++ b/backend/windmill-api-jobs/src/execution.rs @@ -870,16 +870,18 @@ pub async fn delete_flow_result_after_read(db: &DB, job_uuid: Uuid) -> Result<() { delete_job_metadata_after_use(db, job_uuid).await?; if let Some(early_return) = flow_value.early_return.as_deref() { + let early_return_ids: Vec = + windmill_common::jobs::completed_flow_run_jobs(db, job_uuid) + .await? + .into_iter() + .filter(|j| { + !j.in_subflow && j.flow_step_id.as_deref() == Some(early_return) + }) + .map(|j| j.id) + .collect(); sqlx::query!( - "WITH RECURSIVE run AS ( - SELECT id, flow_step_id FROM v2_job WHERE parent_job = $1 - UNION ALL - SELECT j.id, j.flow_step_id FROM v2_job j JOIN run r ON j.parent_job = r.id - ) - UPDATE v2_job_completed SET result = '{}'::jsonb - WHERE id IN (SELECT id FROM run WHERE flow_step_id = $2)", - job_uuid, - early_return, + "UPDATE v2_job_completed SET result = '{}'::jsonb WHERE id = ANY($1)", + &early_return_ids, ) .execute(db) .await?; diff --git a/backend/windmill-common/src/jobs.rs b/backend/windmill-common/src/jobs.rs index 5ca70d3b58..2be074a5de 100644 --- a/backend/windmill-common/src/jobs.rs +++ b/backend/windmill-common/src/jobs.rs @@ -107,6 +107,40 @@ pub fn get_has_preprocessor_from_content_and_lang( Ok(has_preprocessor) } +/// A job of a completed flow's run, as returned by [`completed_flow_run_jobs`]. +pub struct FlowRunJob { + pub id: uuid::Uuid, + pub flow_step_id: Option, + /// Inside a nested flow, whose step ids are its own rather than the run's. + pub in_subflow: bool, +} + +/// The jobs of a completed flow's run, at any depth. Walks `parent_job`, because `root_job` +/// is stored NULL whenever it equals the job's parent or innermost root, which is the case +/// for most steps. Descends through completed jobs only: once the flow has completed, a job +/// still queued or running below it was launched by a step through the API, and that job and +/// everything under it are a separate run. +pub async fn completed_flow_run_jobs( + db: &DB, + flow_id: uuid::Uuid, +) -> crate::error::Result> { + Ok(sqlx::query_as!( + FlowRunJob, + r#"WITH RECURSIVE run AS ( + SELECT j.id, j.flow_step_id, j.kind = 'flow' AS is_flow, false AS in_subflow + FROM v2_job j JOIN v2_job_completed c ON c.id = j.id + WHERE j.parent_job = $1 + UNION ALL + SELECT j.id, j.flow_step_id, j.kind = 'flow', r.in_subflow OR r.is_flow + FROM v2_job j JOIN v2_job_completed c ON c.id = j.id JOIN run r ON j.parent_job = r.id + ) + SELECT id AS "id!", flow_step_id, in_subflow AS "in_subflow!" FROM run"#, + flow_id + ) + .fetch_all(db) + .await?) +} + pub async fn schedule_job_deletion( db: &DB, job_id: uuid::Uuid, @@ -270,8 +304,9 @@ pub async fn get_payload_tag_from_prefixed_path( None, ) } else { - let FlowVersionInfo { dedicated_worker, tag, version, labels, job_token_scopes, .. } = - get_latest_flow_version_info_for_path(None, &db, w_id, &path, true).await?; + let FlowVersionInfo { + dedicated_worker, tag, version, labels, job_token_scopes, .. + } = get_latest_flow_version_info_for_path(None, &db, w_id, &path, true).await?; ( JobPayload::Flow { path, @@ -554,7 +589,10 @@ pub struct RunInlinePreviewScriptFnParams { pub enum InlineScriptTarget { /// A script addressed by path, pinned to the version the caller resolved and checked. - Path { path: String, hash: i64 }, + Path { + path: String, + hash: i64, + }, Hash(i64), } diff --git a/backend/windmill-worker/src/worker_flow.rs b/backend/windmill-worker/src/worker_flow.rs index 52b0c45e5b..c32c7b7810 100644 --- a/backend/windmill-worker/src/worker_flow.rs +++ b/backend/windmill-worker/src/worker_flow.rs @@ -2187,35 +2187,22 @@ async fn delete_flow_run_data( secs: i32, early_return: Option<&str>, ) -> error::Result<()> { - // Walks parent_job rather than reading root_job: root_job is stored NULL whenever it - // equals the job's parent or innermost root, which is the case for most steps of a run. - // Only completed jobs: a step can leave a job it spawned through the SDK queued for later, - // and that job still needs its args. - let run = sqlx::query!( - r#"WITH RECURSIVE run AS ( - SELECT id FROM v2_job WHERE parent_job = $1 - UNION ALL - SELECT j.id FROM v2_job j JOIN run r ON j.parent_job = r.id - ) - SELECT j.id AS "id!", j.flow_step_id - FROM run JOIN v2_job j USING (id) JOIN v2_job_completed c USING (id)"#, - flow_job.id - ) - .fetch_all(db) - .await - .map_err(|e| { - Error::internal_err(format!( - "error fetching child jobs for flow-level deletion: {e:#}" - )) - })?; + let run = windmill_common::jobs::completed_flow_run_jobs(db, flow_job.id) + .await + .map_err(|e| { + Error::internal_err(format!( + "error fetching child jobs for flow-level deletion: {e:#}" + )) + })?; let mut all_ids: Vec = run.iter().map(|j| j.id).collect(); all_ids.push(flow_job.id); // A synchronous caller of an early-return flow reads the early-return node's result, // possibly after the flow has completed; it clears that result once read, like the flow's. - // The node can sit at any depth, inside a branch for instance. let result_ids: Vec = run .iter() - .filter(|j| early_return.is_none() || j.flow_step_id.as_deref() != early_return) + .filter(|j| { + j.in_subflow || early_return.is_none() || j.flow_step_id.as_deref() != early_return + }) .map(|j| j.id) .collect();