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

Prevent Rails load hooks applying patch twice #641

Merged
merged 3 commits into from
Nov 29, 2018

Conversation

delner
Copy link
Contributor

@delner delner commented Nov 27, 2018

After refactoring the Rails integration in #552 and #540, we applied a new patching strategy that used ActiveSupport.on_load(:before_initialize) to run Rails patching code once at the appropriate time.

Turns out in some applications, these load hooks can run twice, which injects the middleware twice and causes bad side effects.

Although Rails 5.0+ offers a run_once option for these load hooks, they aren't available for Rails 3.0 - 4.2, which we currently support. Instead, I added a :for option to our existing do_once function which adds a second dimension to the patch hash, then used the Rails application instance as a key.

@delner delner added bug Involves a bug core Involves Datadog core libraries integrations Involves tracing integrations labels Nov 27, 2018
@delner delner added this to the 0.17.3 milestone Nov 27, 2018
@delner delner self-assigned this Nov 27, 2018
@delner delner requested a review from pawelchcki November 27, 2018 23:33
@delner delner force-pushed the fix/rails_load_hooks_run_twice branch from f7d805b to 084a92a Compare November 28, 2018 05:57
pawelchcki
pawelchcki previously approved these changes Nov 28, 2018
soulcutter
soulcutter previously approved these changes Nov 28, 2018
Copy link
Contributor

@soulcutter soulcutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this out in our app that had the "double middleware" problem, and this does indeed fix the problem! Thanks for digging into this!

My one small suggestion is pretty cosmetic; no worries if you take it or leave it.

lib/ddtrace/patcher.rb Outdated Show resolved Hide resolved
@delner delner dismissed stale reviews from soulcutter and pawelchcki via 9b60f72 November 28, 2018 17:31
@delner delner force-pushed the fix/rails_load_hooks_run_twice branch from 084a92a to 9b60f72 Compare November 28, 2018 17:31
@delner delner force-pushed the fix/rails_load_hooks_run_twice branch from 9b60f72 to 950f2d4 Compare November 28, 2018 19:30
pawelchcki
pawelchcki previously approved these changes Nov 28, 2018
@delner delner merged commit 42be529 into master Nov 29, 2018
@delner delner deleted the fix/rails_load_hooks_run_twice branch November 29, 2018 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Involves a bug core Involves Datadog core libraries integrations Involves tracing integrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants