-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PG Transactional locks not working on Rails 5.2 #47
Comments
@jmanian Were you able to resolve the issue? Is this only happening for transactional locks? |
No, I haven't made any progress on this. I'm only seeing it for transactional locks, but we don't use session-level locks very much, so I can't really say. I also haven't been able to reproduce the issue in a development environment. In all of my testing in development the locks work as expected, both transactional and session-level. |
Hey @jmanian did you get to any workaround/solution? Do happen to know if upgrading Rails helps? I'm having the exact same problem and I'd really appreciate if you have anything to share, thank you so much 🙇 |
@lucasprag Unfortunately I did not find a solution. I'm still on Rails 5.2, so I don't know if newer versions will help. I added a bunch of logs to my code that check |
I decided to try posting an issue in Rails, we'll see if anyone there has any help. |
I ran another test that I hadn't tried before, where I use two separate rails console sessions in my production environment to acquire the same lock. I had tried this before in my development environment but not production. The end result is the locks behave as expected in this test. In the first console I ran this ( Team.transaction { Team.with_advisory_lock('jeff testing', transaction: true) { sleep(5); Team.find(2) } } And then immediately after running that, while it was sleeping inside the lock, I ran the same thing in the other console (except without the
So from this I gather that my production app is able to acquire and respect the locks. I'll try to devise more tests to see if I can reproduce the issue in a more controlled way. |
OK I finally figured out what the issue is in my case: caching. The lock is working just fine, but This can be solved with |
Opened #52 to continue this |
Thank you so much for investigating this further @jmanian It was the same case for me and adding a But in my case I was using a scope with I added some logs to compare queries from ActiveRecord and raw SQL queries from Again I really appreciate the idea on how to solve it, thanks @jmanian 👍 |
I've been using
with_advisory_lock
for transactional locks on Rails 4.2 for a long time with success. I just upgraded my app to Rails 5.2 and suddenly they're not working anymore. I'm using Postgres.My use case is to prevent double-creation of a record (very similar to the use described in #42). It looks something like this:
This code is executed by Sidekiq workers in jobs that are triggered by 3rd party webhooks. Often two jobs will run simultaneously that are trying to run this same method for the same user. On Rails 4.2 the transactional advisory lock would successfully prevent two workers from trying to create the same user record. Now that I'm on Rails 5.2 I'm getting flooded with
ActiveRecord::RecordNotUnique
errors, meaning that the the locks are not working like they used to. (The table has a unique index in postgres that prevents a duplicate record.)Does anyone know what might have changed in Rails 5 that could cause this? Did something change with the way that ActiveRecord connects to the database? @mceachen would your comments on #42 apply here? Those were about session-level locks on MySQL so I'm unsure, but the fact that it used to work and now it doesn't is very confusing to me.
PS I've noticed that the block isn't necessary for transaction locks, since the lock is released when the transaction ends, so I've also tried the following code and I get the same result. Both of these approaches worked on Rails 4.2:
The text was updated successfully, but these errors were encountered: