devise-async icon indicating copy to clipboard operation
devise-async copied to clipboard

Getting ActiveRecord::RecordNotFound from time to time when using Sidekiq

Open Nowaker opened this issue 9 years ago • 6 comments

I can clearly see after_commit being used in the code:

          after_commit :send_devise_pending_notifications

Yet, ActiveRecord::RecordNotFound still happens from time to time.

Any ideas how to fix it, @mperham?

Nowaker avatar Jun 23 '16 04:06 Nowaker

I have no idea what the problem might be.

mperham avatar Jun 23 '16 15:06 mperham

I've seen this in production in a few apps, my best guess is that Sidekiq is picking up the job before the transaction has completed. Adding a delay of a few seconds has fixed it in every case I've come across.

KazW avatar Jun 27 '16 22:06 KazW

@KazW The real question is how. We can clearly see after_commit block is used. That means Sidekiq is either faster than light, or after_commit doesn't happen after the commit.

Nowaker avatar Jun 27 '16 22:06 Nowaker

Ah, I've only used this with Devise 3.x when it was after_save... Nevermind!

KazW avatar Jun 27 '16 22:06 KazW

No resolution on this then? We're experiencing this with other Sidekiq jobs. Does anybody know of a way to simulate this in a test environment (we're specifically using Postgres)? We have some linear code that basically goes

some_model = SomeModel.create!(...)
SomeAsyncService.new.perform(some_model)

The service kicks off a Sidekiq Job, which picks up the job (impressively) before the model is committed (despite a proper primary key being set etc).

voltechs avatar Feb 28 '18 16:02 voltechs

@voltechs No resolution, unfortunately. Given Sidekiq cannot be faster than light, after_commit must happen technically after a commit but before the result of that commit is available to database clients.

Nowaker avatar Feb 28 '18 17:02 Nowaker