Repository navigation
FIX: Reject concurrent SDK scenario runs and make attempt accounting atomic (#3748) - #3774
Open
Vardhman Gupta (Kaap10) wants to merge 2 commits into
Open
Vardhman Gupta (Kaap10) wants to merge 2 commits into
Vardhman Gupta (Kaap10) wants to merge 2 commits into
Conversation
This branch has not been deployed
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.
Description
Fixes #3748
When SDK callers execute concurrent tasks against the same
Scenarioinstance or multipleScenarioinstances sharing the same persistedscenario_result_id(viascenario_result_idresume), concurrentrun_async()executions bypass the planned concurrency limit, duplicating target calls and attack results while racing onnumber_tries.Changes:
Scenario(pyrit/scenario/core/scenario.py):_active_runs: ClassVar[set[str]]protected by_active_runs_lock: ClassVar[threading.Lock].run_async(), acquires ownership of_scenario_result_idbefore starting the retry loop or scheduling target work. If already active in another task or thread, raisesRuntimeErrorimmediately with a clear diagnostic message.finally:block ofrun_async(), guaranteeing cleanup on normal completion, unhandled exceptions, and cancellation._execute_atomic_attacks_parallel_async()are drained to ensure background calls have completed.number_trieswith atomic accounting viaincrement_scenario_number_tries_async().MemoryInterface(pyrit/memory/memory_interface.py):increment_scenario_number_tries_async(scenario_result_id: str)toMemoryInterfaceand database implementations using@legacy_sync_override.UPDATE ScenarioResult SET number_tries = number_tries + 1 WHERE id = :id.increment_scenario_number_tries(scenario_result_id: str)per PyRIT memory interface conventions.cc Roman Lutz (@romanlutz)
Tests and Documentation
test_concurrent_run_async_rejects_second_execution_before_target_workintests/unit/scenario/core/test_scenario.py. Verifies that two concurrent tasks with 6 objectives andmax_concurrency=2result in 1 task executing cleanly, the second task rejected withRuntimeErrorbefore target work, peak concurrency strictly <= 2, exactly 6 target calls, exactly 6 attack results, andnumber_tries == 1.test_concurrent_run_async_on_different_instances_same_run_id_rejects_secondto ensure multipleScenarioobjects sharing the same run ID cannot run concurrently.test_cancellation_cleans_up_guard_and_allows_legitimate_resumetesting with real SQLite to verify cancellation safely releases the guard and enables subsequent legitimate resumption to complete the remaining objectives and reachnumber_tries = 2.test_concurrent_run_async_distinct_scenarios_run_in_parallelensuring distinct scenarios run concurrently without mutual blocking.test_increment_scenario_number_tries_async,test_increment_scenario_number_tries_sync,test_increment_scenario_number_tries_missing_id_raises, andtest_increment_scenario_number_tries_concurrentintests/unit/memory/memory_interface/test_interface_scenario_results.py.uv run -m pytest tests/unit/scenario/core/test_scenario.py tests/unit/memory/memory_interface/test_interface_scenario_results.py: 167 passed in 25.97s.uv run -m ruff format: clean.uv run -m ruff check: 0 errors.