From 6e8c9e3b1833dd10df98746cb0a67f7e30660cdc Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Fri, 23 Oct 2026 16:32:32 +0200 Subject: [PATCH] fix: close the job pause propagation window and surface read errors Co-Authored-By: Claude Opus 5.5 --- ...85c73537bde89e52ebf9374e3512e4275d886.json | 20 ++++++ backend/src/monitor.rs | 8 +-- backend/windmill-common/src/job_pause.rs | 65 ++++++++++++------- .../windmill-trigger/src/global_handler.rs | 2 +- cli/src/commands/sync/sync.ts | 2 + cli/src/commands/workspace/jobsPause.ts | 9 +++ docs/workspace-job-pause.md | 6 ++ .../(logged)/workspace_settings/+page.svelte | 4 +- 8 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 backend/.sqlx/query-c1e17da7e9e2df6366f417e5f5685c73537bde89e52ebf9374e3512e4275d886.json diff --git a/backend/.sqlx/query-c1e17da7e9e2df6366f417e5f5685c73537bde89e52ebf9374e3512e4275d886.json b/backend/.sqlx/query-c1e17da7e9e2df6366f417e5f5685c73537bde89e52ebf9374e3512e4275d886.json new file mode 100644 index 0000000000..3153a607b9 --- /dev/null +++ b/backend/.sqlx/query-c1e17da7e9e2df6366f417e5f5685c73537bde89e52ebf9374e3512e4275d886.json @@ -0,0 +1,20 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT workspace_id FROM workspace_job_pause WHERE paused_until > now()", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "workspace_id", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false + ] + }, + "hash": "c1e17da7e9e2df6366f417e5f5685c73537bde89e52ebf9374e3512e4275d886" +} diff --git a/backend/src/monitor.rs b/backend/src/monitor.rs index e206c907d3..5a0f206c43 100644 --- a/backend/src/monitor.rs +++ b/backend/src/monitor.rs @@ -4467,11 +4467,11 @@ pub async fn monitor_db( } }; - let resume_expired_job_pauses_f = async { + let maintain_job_pauses_f = async { if server_mode && !initial_load { if let Some(db) = conn.as_sql() { - if let Err(e) = windmill_common::job_pause::resume_expired(&db).await { - tracing::error!("Error resuming expired workspace job pauses: {e:#}"); + if let Err(e) = windmill_common::job_pause::maintain_pauses(&db).await { + tracing::error!("Error maintaining workspace job pauses: {e:#}"); } } } @@ -4639,7 +4639,7 @@ pub async fn monitor_db( pipeline_freshness_watchdog_f, reconcile_unarmed_schedules_f, stranded_jobs_f, - resume_expired_job_pauses_f, + maintain_job_pauses_f, ); } diff --git a/backend/windmill-common/src/job_pause.rs b/backend/windmill-common/src/job_pause.rs index ff630823aa..d53c0ff8e6 100644 --- a/backend/windmill-common/src/job_pause.rs +++ b/backend/windmill-common/src/job_pause.rs @@ -35,37 +35,31 @@ lazy_static::lazy_static! { pub const JOB_PAUSE_CHANGE_CHANNEL: &str = "notify_workspace_job_pause_change"; /// The deadline of the workspace's pause, `None` when it is not paused. A pause past its deadline -/// reads as over even before the monitor deletes it. Fails open: a job is never held because the -/// pause could not be read. Takes the caller's connection: `push` reads it inside its own -/// transaction, where asking the pool for a second connection can starve it. +/// reads as over even before the monitor deletes it. Takes the caller's connection: `push` reads it +/// inside its own transaction, where asking the pool for a second connection can starve it, and +/// where a failed read has aborted the transaction anyway, so the error is returned. pub async fn jobs_paused_until<'c, E: sqlx::PgExecutor<'c>>( db: E, w_id: &str, -) -> Option> { +) -> Result>> { let now = Utc::now(); let cached = JOB_PAUSE_CACHE .get(w_id) .filter(|(_, cached_at)| now.timestamp() - cached_at < CACHE_TTL_SECS); let until = match cached { Some((until, _)) => until, - None => match sqlx::query_scalar!( - "SELECT paused_until FROM workspace_job_pause WHERE workspace_id = $1", - w_id - ) - .fetch_optional(db) - .await - { - Ok(until) => { - JOB_PAUSE_CACHE.insert(w_id.to_string(), (until, now.timestamp())); - until - } - Err(e) => { - tracing::error!("could not read the job pause of workspace {w_id}: {e:#}"); - None - } - }, + None => { + let until = sqlx::query_scalar!( + "SELECT paused_until FROM workspace_job_pause WHERE workspace_id = $1", + w_id + ) + .fetch_optional(db) + .await?; + JOB_PAUSE_CACHE.insert(w_id.to_string(), (until, now.timestamp())); + until + } }; - until.filter(|until| *until > now) + Ok(until.filter(|until| *until > now)) } pub fn evict(w_id: &str) { @@ -154,7 +148,7 @@ pub async fn check_push<'c, E: sqlx::PgExecutor<'c>>( if policy == Some(PausePolicy::Exempt) { return Ok(None); } - let Some(until) = jobs_paused_until(db, w_id).await else { + let Some(until) = jobs_paused_until(db, w_id).await? else { return Ok(None); }; match pause_decision(kind, trigger_kind, trigger_path, is_admin) { @@ -180,6 +174,12 @@ pub async fn hold_queued_jobs(tx: &mut Transaction<'_, Postgres>, w_id: &str) -> ) .execute(&mut **tx) .await?; + sweep_queued_jobs(tx, w_id).await +} + +/// The sweep half of [`hold_queued_jobs`], also run by the monitor while a pause lasts: a server +/// or worker whose cached read still says "not paused" queues jobs for up to the cache TTL. +async fn sweep_queued_jobs(tx: &mut Transaction<'_, Postgres>, w_id: &str) -> Result { let held = sqlx::query_scalar!( r#"WITH candidates AS ( SELECT q.id, q.scheduled_for @@ -286,9 +286,24 @@ pub async fn resume(tx: &mut Transaction<'_, Postgres>, w_id: &str) -> Result Result<()> { +/// Holds what slipped through since the last pass in workspaces still paused, ends every pause past +/// its deadline, and releases the jobs of workspaces that are no longer paused. Safe to run on +/// every server at once: the sweep skips locked rows and each release deletes the rows it acts on. +pub async fn maintain_pauses(db: &DB) -> Result<()> { + let paused = sqlx::query_scalar!( + "SELECT workspace_id FROM workspace_job_pause WHERE paused_until > now()" + ) + .fetch_all(db) + .await?; + for w_id in paused { + let mut tx = db.begin().await?; + let held = sweep_queued_jobs(&mut tx, &w_id).await?; + tx.commit().await?; + if held > 0 { + tracing::info!("job pause of workspace {w_id} held {held} more queued jobs"); + } + } + let workspaces = sqlx::query_scalar!( r#"SELECT workspace_id AS "workspace_id!" FROM workspace_job_pause WHERE paused_until <= now() UNION diff --git a/backend/windmill-trigger/src/global_handler.rs b/backend/windmill-trigger/src/global_handler.rs index 39e605438e..1fc65c7449 100644 --- a/backend/windmill-trigger/src/global_handler.rs +++ b/backend/windmill-trigger/src/global_handler.rs @@ -178,7 +178,7 @@ pub async fn resume_suspended_trigger_jobs( // Released in place mid-pause, a job would start during the deploy the pause protects; a // repushed one goes through `push`, which applies the pause itself. let workspace_paused = windmill_common::job_pause::jobs_paused_until(&mut *tx, &w_id) - .await + .await? .is_some(); for job in jobs { diff --git a/cli/src/commands/sync/sync.ts b/cli/src/commands/sync/sync.ts index dcc85e1601..d13105f16c 100644 --- a/cli/src/commands/sync/sync.ts +++ b/cli/src/commands/sync/sync.ts @@ -70,6 +70,7 @@ import { pauseWorkspaceJobs, resumeWorkspaceJobs, waitForDependencyJobs, + waitForPauseToPropagate, } from "../workspace/jobsPause.ts"; import { deepEqual, @@ -5848,6 +5849,7 @@ export async function push( "wmill sync push", ) : undefined; + if (jobsPause) await waitForPauseToPropagate(); log.info(colors.gray(`Applying changes to files ...`)); let stateful = opts.stateful; diff --git a/cli/src/commands/workspace/jobsPause.ts b/cli/src/commands/workspace/jobsPause.ts index 66662dc657..3d6f9450c2 100644 --- a/cli/src/commands/workspace/jobsPause.ts +++ b/cli/src/commands/workspace/jobsPause.ts @@ -39,6 +39,15 @@ export async function pauseWorkspaceJobs( return pause; } +// Servers and workers cache whether a workspace is paused for up to 10s; until then they can still +// start a new job. Deploying only after that keeps such a job on the pre-deploy state. +const PAUSE_PROPAGATION_MS = 11_000; + +export async function waitForPauseToPropagate() { + log.info(colors.gray("Waiting for the pause to reach every server ...")); + await new Promise((r) => setTimeout(r, PAUSE_PROPAGATION_MS)); +} + export async function resumeWorkspaceJobs(workspaceId: string) { const released = await wmill.resumeJobs({ workspace: workspaceId }); log.info( diff --git a/docs/workspace-job-pause.md b/docs/workspace-job-pause.md index 3cab6135a0..8b069daa05 100644 --- a/docs/workspace-job-pause.md +++ b/docs/workspace-job-pause.md @@ -37,6 +37,12 @@ synchronous entry point needs the same wrapper. The policy is a task-local: it d `scheduled_for = GREATEST(held due time, now())`, so that run keeps its slot. - The pause is runtime state: it is not in the settings document (a settings push must not clear it, a fork must not start paused) and is read through a 10s cache evicted by `notify_event`. + For up to that long after a pause starts, another server or worker can still queue or start a + job; the monitor re-sweeps paused workspaces every pass, and `sync push --pause-jobs` waits out + the TTL before deploying so a job that started in that window runs on the pre-deploy state. +- A job a waiting caller (`run_wait_result`, a sync route) pushed before the pause and that has not + started yet is held by the sweep like any queued job: that caller times out rather than getting + a 503, and the job runs on resume. Telling those jobs apart would need per-job state. A pause past its deadline reads as over immediately; `monitor_db` deletes it and releases its jobs, and also releases held rows left without a pause (a push that read a stale cache). - `MAX_PAUSE_DAYS` (7) must stay well under the 30-day horizon after which a worker fails an diff --git a/frontend/src/routes/(root)/(logged)/workspace_settings/+page.svelte b/frontend/src/routes/(root)/(logged)/workspace_settings/+page.svelte index 2d6d68af67..57432e056a 100644 --- a/frontend/src/routes/(root)/(logged)/workspace_settings/+page.svelte +++ b/frontend/src/routes/(root)/(logged)/workspace_settings/+page.svelte @@ -1872,9 +1872,7 @@ onclick={saveSlackSigningSecret} disabled={!slackSigningSecret.trim() && !slackSigningSecretSet} > - {slackSigningSecret.trim() || !slackSigningSecretSet - ? 'Save' - : 'Remove'} + {slackSigningSecret.trim() || !slackSigningSecretSet ? 'Save' : 'Remove'}