# Bug OP-17121 - Error 500 when bulk delete related work packages

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-17121

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | Medium |
| Module | Modules and project features / Work packages |
| Target versions | 16.0.0 |
| Observed in versions |  |
| Sprint | 16.0.0 |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers | Christophe Bliard |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Steps to reproduce

1.  Create 3 work packages: one parent with 2 children following each other

2.  Select them all in work package list view (there are no checkboxes, you have to select them one by one with the mouse by keeping Ctrl or Command key pressed)

3.  Right click and select "Bulk delete",

4.  In the confirmation dialog, check "I acknowledge that ALL descendants of the listed work packages will be recursively removed." and click "Delete" button.

### What is the buggy behavior?

  - 500 error with error "ActiveRecord::StaleObjectError in WorkPackages::BulkController".

  - Appsignal:
    
      - <https://appsignal.com/openproject-gmbh/sites/681379ae55b0b20ed3735994/exceptions/incidents/586>
    
      - <https://appsignal.com/openproject-gmbh/sites/681379ae55b0b20ed3735994/exceptions/incidents/582>
    
      - <https://appsignal.com/openproject-gmbh/sites/6813715a55b0b20ed373597c/exceptions/incidents/596>
    
      - <https://appsignal.com/openproject-gmbh/sites/674718f1d2a5e4a7cb8b2298/exceptions/incidents/939>
    
      - <https://appsignal.com/openproject-gmbh/sites/63ce63dc83eb6762736f5052/exceptions/incidents/1361>
    
      - <https://appsignal.com/openproject-gmbh/sites/673c529383eb67b55471dda2/exceptions/incidents/503>
    
      - <https://appsignal.com/openproject-gmbh/sites/678fa146338a5d816f8d696c/exceptions/incidents/796>

  - Message: `Attempted to update a stale object: WorkPackage.`

  - Backtrace:
    
      - app/services/work\_packages/delete\_service.rb:98 in block in WorkPackages::DeleteService\#reschedule\_related
    
      - app/services/base\_services/write.rb:36 in BaseServices::Write\#persist

### What is the expected behavior?

  - Work packages should be deleted without any errors

### Notes

This error on bulk delete has also been reported in this ticket <mention class="mention" data-id="60580" data-type="work_package" data-text="##60580">##60580</mention> by Markus. This has been added to the existing issue with bulk edit because the symptoms are the same.

### Stack trace

The error occurs in the [`reschedule_related`](https://github.com/opf/openproject/blob/release/15.5/app/services/work_packages/delete_service.rb#L83-L102) method of the `WorkPackages::DeleteService`.  
My guess is that the error occurs in the method [`update_related`](https://github.com/opf/openproject/blob/release/15.5/app/services/work_packages/update_service.rb#L70-L73) in the `WorkPackages::UpdateService` as well.

In both cases a list of dependent work packages is iterated over and they are saved. This is then when the stale object error occurs because related (e.g. via automatic scheduling) work packages in that list were updated through an update of earlier work packages.

Here is the respective stack trace for the deletion.

``` text
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/locking/optimistic.rb:112:in 'ActiveRecord::Locking::Optimistic#_update_row'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/persistence.rb:1234:in 'ActiveRecord::Persistence#_update_record'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/attribute_methods/dirty.rb:234:in 'ActiveRecord::AttributeMethods::Dirty#_update_record'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/callbacks.rb:449:in 'block (2 levels) in ActiveRecord::Callbacks#_update_record'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/timestamp.rb:140:in 'ActiveRecord::Timestamp#record_update_timestamps'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/callbacks.rb:449:in 'block in ActiveRecord::Callbacks#_update_record'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:110:in 'ActiveSupport::Callbacks#run_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:952:in 'ActiveRecord::Base#_run_update_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/callbacks.rb:449:in 'ActiveRecord::Callbacks#_update_record'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/timestamp.rb:122:in 'ActiveRecord::Timestamp#_update_record'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/persistence.rb:1221:in 'ActiveRecord::Persistence#create_or_update'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/callbacks.rb:441:in 'block in ActiveRecord::Callbacks#create_or_update'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:121:in 'block in ActiveSupport::Callbacks#run_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/autosave_association.rb:375:in 'ActiveRecord::AutosaveAssociation#around_save_collection_association'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:130:in 'block in ActiveSupport::Callbacks#run_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:141:in 'ActiveSupport::Callbacks#run_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:952:in 'ActiveRecord::Base#_run_save_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/callbacks.rb:441:in 'ActiveRecord::Callbacks#create_or_update'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/timestamp.rb:127:in 'ActiveRecord::Timestamp#create_or_update'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/persistence.rb:718:in 'ActiveRecord::Persistence#save'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/validations.rb:49:in 'ActiveRecord::Validations#save'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/transactions.rb:309:in 'block in ActiveRecord::Transactions#save'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/transactions.rb:365:in 'block in ActiveRecord::Transactions#with_transaction_returning_status'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:342:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#transaction'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/transactions.rb:361:in 'ActiveRecord::Transactions#with_transaction_returning_status'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/transactions.rb:309:in 'ActiveRecord::Transactions#save'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/suppressor.rb:52:in 'ActiveRecord::Suppressor#save'
/app/app/services/work_packages/delete_service.rb:98:in 'block in WorkPackages::DeleteService#reschedule_related'
/app/app/services/work_packages/delete_service.rb:97:in 'Array#each'
/app/app/services/work_packages/delete_service.rb:97:in 'WorkPackages::DeleteService#reschedule_related'
/app/app/services/work_packages/delete_service.rb:74:in 'WorkPackages::DeleteService#update_ancestors_and_successors'
/app/app/services/work_packages/delete_service.rb:44:in 'WorkPackages::DeleteService#persist'
/app/app/services/base_services/base_contracted.rb:64:in 'block in BaseServices::BaseContracted#perform'
/app/app/models/journal/notification_configuration.rb:40:in 'Journal::NotificationConfiguration.with'
/app/app/services/shared/service_context.rb:67:in 'block in Shared::ServiceContext#without_context_transaction'
/app/lib/open_project/locale_helper.rb:36:in 'OpenProject::LocaleHelper.with_locale_for'
/app/app/models/user.rb:538:in 'User.execute_as'
/app/app/services/shared/service_context.rb:66:in 'Shared::ServiceContext#without_context_transaction'
/app/app/services/shared/service_context.rb:45:in 'block in Shared::ServiceContext#in_mutex_context'
/app/lib/open_project/mutex.rb:71:in 'block in OpenProject::Mutex.with_advisory_lock'
/app/vendor/bundle/ruby/3.4.0/gems/with_advisory_lock-5.1.0/lib/with_advisory_lock/base.rb:102:in 'WithAdvisoryLock::Base#yield_with_lock'
/app/vendor/bundle/ruby/3.4.0/gems/with_advisory_lock-5.1.0/lib/with_advisory_lock/base.rb:89:in 'WithAdvisoryLock::Base#yield_with_lock_and_timeout'
/app/vendor/bundle/ruby/3.4.0/gems/with_advisory_lock-5.1.0/lib/with_advisory_lock/base.rb:72:in 'WithAdvisoryLock::Base#lock_and_yield'
/app/vendor/bundle/ruby/3.4.0/gems/with_advisory_lock-5.1.0/lib/with_advisory_lock/base.rb:63:in 'WithAdvisoryLock::Base#with_advisory_lock_if_needed'
/app/vendor/bundle/ruby/3.4.0/gems/with_advisory_lock-5.1.0/lib/with_advisory_lock/concern.rb:23:in 'WithAdvisoryLock::Concern::ClassMethods#with_advisory_lock_result'
/app/vendor/bundle/ruby/3.4.0/gems/with_advisory_lock-5.1.0/lib/with_advisory_lock/concern.rb:10:in 'WithAdvisoryLock::Concern::ClassMethods#with_advisory_lock'
/app/lib/open_project/mutex.rb:69:in 'OpenProject::Mutex.with_advisory_lock'
/app/lib/open_project/mutex.rb:63:in 'block in OpenProject::Mutex.with_advisory_lock_transaction'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/connection_adapters/abstract/transaction.rb:535:in 'block in ActiveRecord::ConnectionAdapters::TransactionManager#within_new_transaction'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/concurrency/null_lock.rb:9:in 'ActiveSupport::Concurrency::NullLock#synchronize'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/connection_adapters/abstract/transaction.rb:532:in 'ActiveRecord::ConnectionAdapters::TransactionManager#within_new_transaction'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:344:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#transaction'
/app/vendor/bundle/ruby/3.4.0/gems/activerecord-7.1.5.1/lib/active_record/transactions.rb:212:in 'ActiveRecord::Transactions::ClassMethods#transaction'
/app/lib/open_project/mutex.rb:62:in 'OpenProject::Mutex.with_advisory_lock_transaction'
/app/app/services/shared/service_context.rb:44:in 'Shared::ServiceContext#in_mutex_context'
/app/app/services/shared/service_context.rb:35:in 'Shared::ServiceContext#in_context'
/app/app/services/base_services/base_contracted.rb:54:in 'BaseServices::BaseContracted#service_context'
/app/app/services/base_services/base_contracted.rb:59:in 'BaseServices::BaseContracted#perform'
/app/app/services/base_services/base_callable.rb:40:in 'block in BaseServices::BaseCallable#call'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:121:in 'block in ActiveSupport::Callbacks#run_callbacks'
/app/app/services/concerns/with_reversible_state.rb:53:in 'BaseServices::BaseCallable#assign_state'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:130:in 'block in ActiveSupport::Callbacks#run_callbacks'
/app/vendor/bundle/ruby/3.4.0/gems/activesupport-7.1.5.1/lib/active_support/callbacks.rb:141:in 'ActiveSupport::Callbacks#run_callbacks'
/app/app/services/base_services/base_callable.rb:39:in 'BaseServices::BaseCallable#call'
```
