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 race condition starting CacheCleanupThread #586

Merged
merged 1 commit into from
Jun 22, 2023

Conversation

willkoehler
Copy link
Contributor

This fixes two race conditions

  1. Thread execution may start before CacheCleanupThread.new returns the class instance. In this case t is not yet defined inside the thread block and results in a "undefined method `sleepy_run' for nil:NilClass" error.

  2. Thread execution may start before member variables @store, @interval, etc are initialized in CacheCleanupThread#initialize This results in "undefined method '*' for nil:NilClass" in should_cleanup? (this error only occurs after the first race condition is fixed)

More detail: A subclassed thread is scheduled for execution as soon as super is called. The code block of the thread may start to execute before the class is fully instantiated. See https://www.ruby-forum.com/t/thread-super-should-be-first-line-or-last-line/150617

Fixes #566

This fixes two race conditions

1. Thread execution may start before `CacheCleanupThread.new` returns the class instance. In this case `t` is not yet defined inside the thread block and results in a "undefined method `sleepy_run' for nil:NilClass" error.

2. Thread execution may start before member variables `@store`, `@interval`, etc are initialized in `CacheCleanupThread#initialize` This results in "undefined method '*' for nil:NilClass" in should_cleanup? (this error only occurs after the first race condition is fixed)

More detail: A subclassed thread is scheduled for execution as soon as `super` is called. The code block of the thread may start to execute before the class is fully instantiated. See https://www.ruby-forum.com/t/thread-super-should-be-first-line-or-last-line/150617
@nateberkopec
Copy link
Collaborator

Amazing work. There's not way I would have ever figured out point #2 especially... great job!

@nateberkopec nateberkopec merged commit 0d34c02 into MiniProfiler:master Jun 22, 2023
@willkoehler
Copy link
Contributor Author

Thanks @nateberkopec 🙌 This was a particularly fun one. I had a hunch about points #1 and #2. But I applied them separately at first, which made it appear that neither one made a difference. It took a while to figure out they both needed to be applied.

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

Successfully merging this pull request may close these issues.

Infrequent weird output in tests
2 participants