Fix latest diff race in the resolve conflicts feature spec
What does this MR do and why?
Fixes the flaky Edit inline examples in spec/features/merge_request/user_resolves_conflicts_spec.rb. The spec reloaded the MR diff while the page's requests were still running on the same DB connection. When the diff save joined one of their transactions, Rails skipped the callback that marks the new diff as the latest, so the diffs page kept showing the old version and the example failed on One morning. The spec now reloads the diff only after the page's requests finish, with new ones blocked.
Detailed context for AI agents
Failure evidence
- Job https://gitlab.com/gitlab-org/gitlab/-/jobs/17593207523 (master, 2026-10-29) failed on the first run and on the retry. Its page dump lists two versions: id 40 (head
acb443a9, 3 commits, so the resolution commit is there) withlatest: false, and id 39 withlatest: true. The page streameddiff_id=39. - The same failure hit jobs https://gitlab.com/gitlab-org/gitlab/-/jobs/17524122724, https://gitlab.com/gitlab-org/gitlab/-/jobs/17541903669, https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/17553100335 and https://gitlab.com/gitlab-org/gitlab/-/jobs/17613636932.
- The job's
log/gitlab_transaction_stack.logshows Puma threads running markdown cache refresh transactions (refresh_markdown_cache!) interleaved with the spec thread'screate_merge_request_diff.
Mechanism (Rails 7.2.4)
- Feature specs pin one DB connection that the spec thread and Puma's request threads share, so they share one transaction stack.
DatabaseStatements#transactioncheckscurrent_transaction.joinable?before it takes the connection lock. The spec thread'sMergeRequestDiff#save!sees a request thread's open transaction and joins it, andadd_to_transactionregisters the diff with that transaction.- The INSERT then waits on the connection monitor, which
within_new_transactionholds for the request's whole block. - The request's transaction commits first.
prepare_instances_to_run_callbacks_onskips the diff becausetrigger_transactional_callbacks?needspersisted?, and the row isn't inserted yet. - The INSERT runs afterwards, with no transaction tracking the record.
after_create_commit :set_as_latest_diffnever fires, solatest_merge_request_diff_idkeeps pointing at the old diff.
A local probe (not committed) reproduced this deterministically by opening a transaction on another thread just before MergeRequestDiff#save!. The new diff was created, but latest_merge_request_diff_id stayed at the old id. Without the other thread, the same reload_diff moved it to the new diff.
Fix
reload_diff_once_requests_settle runs wait_for_all_requests, then block_and_wait_for_requests_complete { merge_request.reload_diff }. The first wait lets the post-redirect page load finish, so blocking does not return 503s to its follow-up requests (an empty 503 body shows up as a JSON parse alert). The blocked section then makes sure no request is mid-transaction during the save.
Scope notes
- The Interactive mode examples use the same helper. Their assertions also pass on the pre-resolution diff ("Use ours" keeps the source branch's lines), so they could not detect this race. Tightening them is out of scope.
- The skipped
xitexample in the markers context is unchanged. - The other error group in the issue (
NoMethodErrorforai_service_account?) came only from the pipelines of !261164 (merged), which renamed that method and merged on 2026-10-26. It is unrelated. - Any feature spec that writes from the spec thread while page requests are in flight can hit the same callback loss. This MR only fixes this spec.
Verification
- The probe described above.
- Locally, the full file fails 12 of 16 in an isolated rspec slot with
wait_for_requeststimeouts, mostly in the spec's unchangedbeforehooks. The MR page never reports idle there and shows an "Unexpected end of JSON input" alert, with no server-side errors logged. Two of those timeouts were in the newwait_for_all_requests, with the same symptom. This looks environmental, but it is not proven, so this MR relies on CI for the spec run.
References
Screenshots or screen recordings
Not applicable, spec-only change.
How to set up and validate locally
Not applicable, spec-only change.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.