Avoid machine validation result/heartbeat deadlocks#3533
Draft
martinraumann wants to merge 1 commit into
Draft
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
dc25393 to
e333ff8
Compare
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.
Summary
machine_validationrow withFOR NO KEY UPDATE.find_by_id()read path unchanged.Why
This fixes a machine-validation deadlock caused by inconsistent lock ordering between concurrent validation paths.
Before this change, result persistence could lock child rows first and the parent run later:
persist_validation_result()calledrecord_result().record_result()wrotemachine_validation_run_itemsandmachine_validation_attempts.machine_validation_resultsfired a trigger that updated the parentmachine_validationrow.Effective order: run item / attempt -> parent run.
The heartbeat path already used the opposite order:
record_heartbeat()updated the parentmachine_validationrow.machine_validation_run_items.Effective order: parent run -> run item.
Those two paths can deadlock when a result post and heartbeat for the same validation run/item overlap. This PR makes result persistence acquire the parent
machine_validationlock beforerecord_result(), so it follows the same parent-run -> run-item/attempt order as heartbeat.The stale-attempt reconciler also writes attempt/run-item state before completing the parent validation. This PR updates that path to acquire the parent run lock first as well, so the touched machine-validation paths share the same ordering:
That removes the observed result-vs-heartbeat lock inversion and avoids introducing a result-vs-stale-reconciler inversion.
Validation
cargo fmtpasses. Stable rustfmt emits existing repo warnings for nightly-only rustfmt options.cargo clippy -p carbide-api-db --lib -- -D warningspasses.cargo clippy -p carbide-api-core --no-default-features --lib -- -D warningspasses.git diff --checkpasses.CancellationTokenidentifiers.cargo clippy -p carbide-api-core --all-targets -- -D warningsis blocked locally on Apple Silicon becausetss-esapi-sys v0.6.0does not support theaarch64, darwintarget without generated bindings.cargo test -p carbide-api-core --no-default-features test_machine_validation_m1_persists_selected_test_and_idempotent_resultis blocked by the same localtss-esapi-systarget limitation through test/dev dependencies.Security Evidence
idvia sqlx.bind(id).CancellationTokenmatchedtoken.cargo denyis not installed in this local environment.