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

Mechanism (Rails 7.2.4)

  1. Feature specs pin one DB connection that the spec thread and Puma's request threads share, so they share one transaction stack.
  2. DatabaseStatements#transaction checks current_transaction.joinable? before it takes the connection lock. The spec thread's MergeRequestDiff#save! sees a request thread's open transaction and joins it, and add_to_transaction registers the diff with that transaction.
  3. The INSERT then waits on the connection monitor, which within_new_transaction holds for the request's whole block.
  4. The request's transaction commits first. prepare_instances_to_run_callbacks_on skips the diff because trigger_transactional_callbacks? needs persisted?, and the row isn't inserted yet.
  5. The INSERT runs afterwards, with no transaction tracking the record. after_create_commit :set_as_latest_diff never fires, so latest_merge_request_diff_id keeps 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 xit example in the markers context is unchanged.
  • The other error group in the issue (NoMethodError for ai_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_requests timeouts, mostly in the spec's unchanged before hooks. 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 new wait_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.

Merge request reports

Loading
Loading