Skip to content
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

Fix thread safety of prevent_writes #36868

Merged

Conversation

eileencodes
Copy link
Member

As demonstrated in the test added and in #36830 the code that prevents
writes wasn't thread safe. If one thread does a read, then another does
a write, and then another does a read the second read will cause the
first write to be unwriteable.

This change removes the instance variable and instead uses a
getter/setter on Thread.current[:prevent_writes] for the connection
handler to set whether writes are allowed.

Fixes #36830


Thanks to @matthewd for helping me with writing this test.

cc/ @matthewd @tenderlove @jhawthorn
Thanks to @kiyot for the original report

As demonstrated in the test added and in rails#36830 the code that prevents
writes wasn't thread safe. If one thread does a read, then another does
a write, and then another does a read the second read will cause the
first write to be unwriteable.

This change removes the instance variable and instead uses a
getter/setter on Thread.current[:prevent_writes] for the connection
handler to set whether writes are allowed.

Fixes rails#36830
@eileencodes eileencodes force-pushed the make-prevent-writes-threadsafe branch from fdc696b to ff70c17 Compare August 6, 2019 17:07
@eileencodes eileencodes added this to the 6.0.0 milestone Aug 6, 2019
@eileencodes eileencodes merged commit b4ee594 into rails:master Aug 6, 2019
@eileencodes eileencodes deleted the make-prevent-writes-threadsafe branch August 6, 2019 19:58
eileencodes added a commit that referenced this pull request Aug 6, 2019
…safe

Fix thread safety of prevent_writes
@eileencodes
Copy link
Member Author

Backported to 6-0-stable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails 6 RC2 multi-db issue under multithreaded environment
3 participants