Make concurrent initialize_jobs dedup test deterministic#369
Merged
Conversation
`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>
Contributor
There was a problem hiding this comment.
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_taskto force concurrent callers to overlap deterministically whenTORC_TEST_INITIALIZE_JOBS_BARRIERis set. - Add
start_server_with_env()(and underlyingstart_process_with_env()) to spawn an isolatedtorc-serverprocess 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 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
test_initialize_jobs_async_concurrent_requests_return_same_taskis flaky on CI (e.g. run 27048505244):The test fires two concurrent
initialize_jobs?async=truerequests 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 toqueued/running) no longer blocks it and a second task is created — yielding ids1and2.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— addtest_initialize_jobs_rendezvous(), called at both success returns ofcreate_or_get_initialize_jobs_task. WhenTORC_TEST_INITIALIZE_JOBS_BARRIER=Nis set, the first N task-creation calls block before returning (so no worker is spawned yet and the winning task staysqueued) 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— addstart_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
cargo fmt --check,cargo clippy --all --all-targets --all-features -- -D warnings, anddprint checkall clean.This is independent of PR #366 (the flaky test is pre-existing), so it can merge on its own.
🤖 Generated with Claude Code