Loading
Reset project namespace when a project transfer rolls back
What does this MR do and why?
A failed async project transfer can leave the project namespace's parent_id (and traversal_ids) pointing at the transfer target while projects.namespace_id stays on the old namespace. This is the remaining source of the drift that !251257 cleans up, so the BBM there does not need to be requeued after this lands.
Root cause
Projects::TransferServicecallssave_project!insideProject.transaction.Project#ensure_project_namespace_in_synccopies the newnamespace_idonto the cachedproject.project_namespaceasparent_id, and autosave writes it.- A later step in the transaction raises, so the database changes roll back. The in-memory
ProjectNamespacekeeps the newparent_idas a dirty attribute. rollback_side_effectsonly resetsproject, not its project namespace.Projects::TransferWorkerholds that same project namespace object and callscancel_transfer!on failure, both when the service returnsfalseand in itsrescue StandardError. The state-machine save persists the staleparent_id, andtraversal_idsare recomputed from it.
Since the async worker became the only transfer path, every transfer that fails after save_project! produces one drifted row.
This MR makes two changes to Projects::TransferService:
rollback_side_effectsnow resets the project namespace, alongside the existingproject.reset. Every exception raised inside the transaction passes through it, so both worker failure paths are covered.rollback_side_effectsnow runs only for failures inside the transaction.remove_paid_features,update_pending_buildsandpost_update_hooksrun after the transaction has committed. Before, a failure in one of them still ran the rollback: it rewrote the repository configuration to the old path, and the worker cancelled a transfer that had already happened. It then created a "transfer failed" to-do, and Sidekiq retried the job, which failed with "Project is already in this namespace". Now those errors are sent to error tracking, and the transfer is reported as a success. Each step is rescued separately, so a failure in one does not skip the others. Suggested in review: !262788 (comment 4044637633)
References
- !251257 (BBM that fixes existing drifted rows)
- !251255 (merged) (earlier fix:
before_save :ensure_project_namespace_in_sync) - #616993 (closed)
Screenshots or screen recordings
Not applicable, backend only. The new worker spec fails without the fix:
expected: 1
got: 3 # project_namespace.parent_id
expected: [1, 2]
got: [3, 2] # project_namespace.traversal_idsHow to set up and validate locally
- In the Rails console, pick a project you can transfer and a target group, and make the transfer fail after the project is saved:
project = Project.find(<id>); target = Group.find(<id>); user = User.find(<id>) Projects::TransferService.prepend(Module.new { def update_integrations = raise(StandardError, 'boom') }) Projects::TransferWorker.new.perform(project.id, target.id, user.id) rescue nil - Check that the project namespace still matches the project:
project.reload.project_namespace.reload.parent_id == project.namespace_id # => true (false before this MR) - To check a failure after the commit, raise from a post-commit step instead and run the worker again. The transfer completes, and the worker neither raises nor creates a "transfer failed" to-do:
Projects::TransferService.prepend(Module.new { def post_update_hooks(*) = raise(StandardError, 'boom') }) Projects::TransferWorker.new.perform(project.id, target.id, user.id) project.reload.namespace == target # => true
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