Skip to content

Make concurrent initialize_jobs dedup test deterministic#369

Merged
daniel-thom merged 2 commits into
mainfrom
fix/init-jobs-concurrent-dedup-test
Jun 6, 2026
Merged

Make concurrent initialize_jobs dedup test deterministic#369
daniel-thom merged 2 commits into
mainfrom
fix/init-jobs-concurrent-dedup-test

Conversation

@daniel-thom
Copy link
Copy Markdown
Collaborator

Problem

test_initialize_jobs_async_concurrent_requests_return_same_task is flaky on CI (e.g. run 27048505244):

assertion `left == right` failed: concurrent initialize_jobs?async=true should be
idempotent: both callers should receive the same task id
  left: 1
 right: 2

The test fires two concurrent initialize_jobs?async=true requests and asserts both receive the same task id. It relied on initialization being slow enough (50 jobs) for the second request to land while the first task was still active. On fast/jittery runners the first task often completes before the second request's INSERT, so the partial unique index (idx_async_handle_unique_active_workflow, scoped to queued/running) no longer blocks it and a second task is created — yielding ids 1 and 2.

The server dedup logic is correct; the flakiness was purely the test's timing assumption.

Fix

Replace the timing assumption with a deterministic server-side rendezvous:

  • src/server/http_server.rs — add test_initialize_jobs_rendezvous(), called at both success returns of create_or_get_initialize_jobs_task. When TORC_TEST_INITIALIZE_JOBS_BARRIER=N is set, the first N task-creation calls block before returning (so no worker is spawned yet and the winning task stays queued) until all N have run their INSERT/dedup, then proceed together. This guarantees the second request's INSERT sees the first task still active and takes the dedup path. The hook is a no-op whenever the env var is unset (always, in normal operation), and is bounded by a 30s timeout so a misconfiguration can't hang the server.
  • tests/common.rs — add start_server_with_env() (+ start_process_with_env) so the test spins up its own dedicated gated server, leaving the shared per-file #[once] fixture untouched.
  • tests/test_tasks.rs — enable the barrier on the test's dedicated server; job count no longer affects determinism.

Verification

  • Previously-flaky test: 10/10 deterministic passes locally (and faster — no longer waiting on job-count slowness).
  • Sibling async-task tests still pass.
  • cargo fmt --check, cargo clippy --all --all-targets --all-features -- -D warnings, and dprint check all clean.

This is independent of PR #366 (the flaky test is pre-existing), so it can merge on its own.

🤖 Generated with Claude Code

`test_initialize_jobs_async_concurrent_requests_return_same_task` was flaky
on CI: it fired two concurrent `initialize_jobs?async=true` requests and
asserted both received the same task id, relying on initialization being slow
enough (50 jobs) for the second request to land while the first task was still
active. On fast/jittery runners the first task often completed before the
second request's INSERT, so the partial unique index no longer blocked it and a
second task was created -- yielding ids 1 and 2.

Replace the timing assumption with a deterministic server-side rendezvous. When
`TORC_TEST_INITIALIZE_JOBS_BARRIER=N` is set, the first N task-creation calls
block inside `create_or_get_initialize_jobs_task` (before returning, so no
worker is spawned yet and the winning task stays `queued`) until all N have run
their INSERT/dedup, then proceed together. This guarantees the second request's
INSERT sees the first task still active and takes the dedup path. The hook is a
no-op in normal operation and bounded by a timeout so a misconfiguration cannot
hang the server.

The test now starts its own dedicated server with the hook enabled (via the new
`start_server_with_env` helper) so the shared per-file fixture is unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes the previously flaky concurrent initialize_jobs?async=true deduplication integration test deterministic by adding a server-side rendezvous hook (enabled only via a test env var) and spinning up a dedicated server instance for that test so it can safely enable the hook without affecting other tests.

Changes:

  • Add a test-only rendezvous in create_or_get_initialize_jobs_task to force concurrent callers to overlap deterministically when TORC_TEST_INITIALIZE_JOBS_BARRIER is set.
  • Add start_server_with_env() (and underlying start_process_with_env()) to spawn an isolated torc-server process with additional environment variables.
  • Update the concurrent-dedup test to use the dedicated server + barrier, removing reliance on “slow enough” initialization timing.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/server/http_server.rs Adds a test-only rendezvous hook in the initialize-jobs task creation path to deterministically coordinate concurrent calls.
tests/common.rs Adds helpers to start a dedicated server process with extra environment variables for test hooks.
tests/test_tasks.rs Switches the flaky concurrent test to a dedicated server with rendezvous enabled; reduces job count since timing no longer matters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/server/http_server.rs
Comment on lines +424 to +437
async fn test_initialize_jobs_rendezvous() {
static BARRIER: std::sync::OnceLock<Option<Arc<tokio::sync::Barrier>>> =
std::sync::OnceLock::new();
let barrier = BARRIER.get_or_init(|| {
std::env::var("TORC_TEST_INITIALIZE_JOBS_BARRIER")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.filter(|&n| n >= 2)
.map(|n| Arc::new(tokio::sync::Barrier::new(n)))
});
if let Some(barrier) = barrier {
let _ = tokio::time::timeout(std::time::Duration::from_secs(30), barrier.wait()).await;
}
}
tokio::sync::Barrier is reusable, so the previous hook blocked on every
group of N calls and could stall an unmatched later call for the full
30s timeout. Track participation with an AtomicUsize so only the first N
callers await the barrier, matching the documented behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@daniel-thom daniel-thom merged commit cec7980 into main Jun 6, 2026
9 checks passed
@daniel-thom daniel-thom deleted the fix/init-jobs-concurrent-dedup-test branch June 6, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants