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 ServerTiming in Threads, use single subscriber #45964

Merged
merged 3 commits into from
Sep 9, 2022

Conversation

jhawthorn
Copy link
Member

Previously ActionDispatch::ServerTiming would subscribe and unsubscribe on each request. This could cause issues with the internal stacks of ActiveSupport::Notifications, particlularly under the previous AS::N implementation which used thread-local stacks for every subscriber (the new implementation has mostly mitigated this).

Additionally, the previous ServerTiming implementation did not report metrics correctly in a multi-threaded environment.

This commit works around both of these issues by using a single global subscription, which collects events into a per-thread Array.

Thanks to lots of advice from @matthewd

end

def ensure_subscribed
@mutex.synchronize do
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could use double checked locking to avoid synchronizing that mutex on every request.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer readability here, and I think it's a safe bet that anyone enabling this (presumably only in development) isn't too concerned with performance.

I could see revisiting this if we make this more production-appropriate (sampling a small set of useful topics), but that would likely look different from this.

Previously ActionDispatch::ServerTiming would subscribe and unsubscribe
on each request. This could cause issues with the internal stacks of
ActiveSupport::Notifications, particlularly under the previous AS::N
implementation which used thread-local stacks for every subscriber
(the new implementation has mostly mitigated this).

Additionally, the previous ServerTiming implementation did not report
metrics correctly in a multi-threaded environment.

This commit works around both of these issues by using a single global
subscription, which collects events into a per-thread Array.
These are not intended for user consumption
@jhawthorn jhawthorn merged commit 8c07790 into rails:main Sep 9, 2022
jhawthorn added a commit that referenced this pull request Sep 9, 2022
Fix ServerTiming in Threads, use single subscriber
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.

None yet

3 participants