Keep a project transfer when an after-commit callback raises

What does this MR do and why?

Follow-up to !262788 (merged), which stopped rolling back a transfer when a step after commit_transfer fails. This handles failures from run_after_commit blocks, which happen inside commit_transfer.

run_after_commit blocks run when the Gitlab::Database::WithLockRetries transaction commits, which is still inside the rescue Exception in Projects::TransferService#commit_transfer. If one raised, the transfer had already committed, but rollback_side_effects wrote the old repository path back, and Projects::TransferWorker cancelled the transfer, created a "transfer failed" to-do, and retried into "Project is already in this namespace".

commit_transfer now records the transaction it runs in. When the rescued error comes after that transaction committed, it skips the rollback and sends the error to error tracking (track_post_commit_failure, as the post-commit steps do). The transfer is reported as done, and the post-commit steps and refresh_permissions still run. A failure before the commit still rolls back.

Why check the transaction state

The commit happens when the outer WithLockRetries transaction ends, not at the end of the Project.transaction block, so a flag set at the end of the block cannot tell whether the commit happened. Rails marks a transaction as committed before it runs the record after_commit callbacks, so transaction.state.committed? is true only for failures that come after the commit. Transaction-level after_commit callbacks run after the record callbacks, so they cannot be used to set a flag.

Limitation: when one run_after_commit block raises, Rails skips the remaining after-commit callbacks of the whole transaction, for every record enrolled in it (for example the project-moved email and the sync event worker). The transfer is kept and returns true. The error is tracked with track_and_raise_for_dev_exception, and nothing retries the skipped callbacks.

References

Screenshots or screen recordings

Not applicable: no UI change.

How to set up and validate locally

  1. In a Rails console, make a run_after_commit block raise during a transfer:
    project = Project.find_by_full_path('<project>')
    group = Group.find_by_full_path('<target group>')
    service = Projects::TransferService.new(project, project.owner)
    service.define_singleton_method(:execute_system_hooks) { super(); project.run_after_commit { raise 'boom' } }
    service.execute(group)
  2. In development the error is raised (track_and_raise_for_dev_exception). Check that project.reload.namespace is the target group and project.project_repository.disk_path equals project.disk_path. Before this change, the repository path was written back to the old one.

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.

Edited by Aakriti Gupta

Merge request reports

Loading
Loading