Skip to content

Wrap load_schema in a Mutex. - #28756

Closed
nasa42 wants to merge 2 commits into
rails:masterfrom
watchmanmonitoring:nasa42-model-schema-thread-safety-issue-28589
Closed

nasa42 wants to merge 2 commits into
rails:masterfrom
watchmanmonitoring:nasa42-model-schema-thread-safety-issue-28589

Conversation

@nasa42

@nasa42 nasa42 commented Apr 14, 2017

Copy link
Copy Markdown

This PR wraps ActiveRecord::ModelSchema::ClassMethods#load_schema in a Mutex to make it thread-safe. Closes #28589.

Regarding tests, are there existing tests in Rails that test thread-safety from race-conditions that I can use as examples to write tests for this PR?

@rails-bot

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@matthewd

Copy link
Copy Markdown
Member

You can use Monitor instead of Mutex for re-entrancy.. though I'm surprised that's something we need to worry about.

We can use an instance variable to hold the lock, instead of a class variable: everything we're protecting is class-local, so the lock should be too.

Oh, was the re-entrancy across different classes? Trying to load a class's schema while we're already in the middle of loading the schema, even within a single thread, seems bad.


You can find various uses of Concurrent::Event and Concurrent::CyclicBarrier in existing tests that coordinate between threads.

@nasa42

nasa42 commented Apr 27, 2017

Copy link
Copy Markdown
Author

@matthewd I'm having a bit of difficulty conceptualising a test that would fail without this patch.

Here is what I've been thinking for a test-case that would fail without this patch:

  • Create two separate threads.
  • Pause the first thread on a pre-determined location (this would create a suitable environment to cause a race-condition from another thread).
  • Run the asserts in second thread (or in main thread) that would fail.

However, the problem is that this test-case won't work after the patch in this PR are applied. Since this PR introduces a mutex, the second thread will now hang and wait for the first thread to release the mutex - which would never happen as we have paused the first thread on a pre-determined location.

@nasa42

nasa42 commented May 24, 2017

Copy link
Copy Markdown
Author

@matthewd I've replaced the Mutex with a Monitor, as well as class variable with a class instance variable.

The downside however is that load_schema_monitor is now a public class method available on all models. Should I additionally add following? (or rename to _load_schema_monitor?)

private_class_method :load_schema_monitor
private_class_method :load_schema_monitor=

nasa42 added 2 commits May 24, 2017 19:47
This commit wraps `ActiveRecord::ModelSchema::ClassMethods#load_schema` in a
Mutex to make it thread-safe. See #28589.
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.

5 participants