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

Ensure graceful closure of tracer resources #1334

Merged
merged 12 commits into from
Mar 18, 2021
Merged

Conversation

marcotc
Copy link
Member

@marcotc marcotc commented Jan 27, 2021

This PR adds integration tests to assert that no resources are being leaked after the tracer shuts down.

As we register the tracer to be gracefully shut down at process exit, we should ensure that all resources are correctly being handled and closed, to avoid any possible data loss.

@marcotc marcotc requested a review from a team January 27, 2021 23:00
@marcotc marcotc self-assigned this Jan 27, 2021
@marcotc marcotc added the dev/testing Involves testing processes (e.g. RSpec) label Jan 27, 2021
end

it 'closes tracer file descriptors' do
try_wait_until { thread_count > original_thread_count }
Copy link
Member

Choose a reason for hiding this comment

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

hmm... interesting...

I think I get why you didn't add this in originally

@marcotc marcotc requested review from brettlangdon and a team January 28, 2021 18:36
@marcotc
Copy link
Member Author

marcotc commented Jan 28, 2021

CI is hard, I'm still working on getting it green.

@marcotc marcotc marked this pull request as draft February 18, 2021 17:01
@marcotc marcotc marked this pull request as ready for review March 15, 2021 21:59
@codecov-io
Copy link

Codecov Report

Merging #1334 (26bc39c) into master (d015782) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1334   +/-   ##
=======================================
  Coverage   98.16%   98.16%           
=======================================
  Files         768      769    +1     
  Lines       36667    36698   +31     
=======================================
+ Hits        35993    36024   +31     
  Misses        674      674           
Impacted Files Coverage Δ
spec/ddtrace_integration_spec.rb 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d015782...26bc39c. Read the comment docs.

ivoanjo
ivoanjo previously approved these changes Mar 16, 2021
Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

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

LGTM, thanks 👍

Comment on lines 15 to 19
before do
original_thread_count
end

let(:original_thread_count) { thread_count }
Copy link
Member

Choose a reason for hiding this comment

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

Minor: RSpec provides a let! for exactly this behavior -- a let that always runs before the testcase :) (Docs link: https://relishapp.com/rspec/rspec-core/v/3-10/docs/helper-methods/let-and-let)

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 wanted to make sure that it was called before start_tracer, and it looked like this before my latest commit:

before do 
  start_tracer
  original_thread_count
end

I think I refactored it in the last one and didn't see the lonely original_thread_count there.

Comment on lines 38 to 48
start_tracer
end

let(:original_fd_count) { fd_count }

def fd_count
Dir['/dev/fd/*'].size
end

it 'closes tracer file descriptors' do
start_tracer
Copy link
Member

Choose a reason for hiding this comment

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

Minor: start_tracer is getting called twice (this seems unintended...?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Unlikely to be intentional 😅

Comment on lines 43 to 45
def fd_count
Dir['/dev/fd/*'].size
end
Copy link
Member

Choose a reason for hiding this comment

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

Since this is a bit of unix black magic, I suggest leaving here a comment explaining a bit what this is and how it works :)

@marcotc
Copy link
Member Author

marcotc commented Mar 17, 2021

fd leak check is failing here because there's a real leak :) #1414

Comment on lines 17 to 22
before do
# Ensure tracer is not running before test starts.
# This shouldn't be necessary if all other tests
# always terminate the tracer.
Datadog.shutdown!
end
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite like making the tests being defensive in this way, e.g. if the tracer shouldn't stay around between tests, I'd favor something like the thread leak detector where we blame the test that forgot to clean up, rather than make other tests need to clean up after it :)

Copy link
Member Author

@marcotc marcotc Mar 18, 2021

Choose a reason for hiding this comment

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

We have a Datadog.shutdown! helper that runs after every test: https://github.com/DataDog/dd-trace-rb/blob/master/spec/spec_helper.rb#L157-L169

This before block introduced here can only catch issues that happen if the tracer was initialized outside of the scope of an RSpec test run, which seems to be happening.

I added it because I noticed that one of the tests this PR was flaky without it, and I leaned towards non-flakiness at the expense of masking the root cause. I'll revert this change then, and see if we can spot the root cause at a later time.

Copy link
Member Author

Choose a reason for hiding this comment

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

The missing shutdown was in the same file: 50c9a0b (#1334). Fixed it.

@marcotc marcotc merged commit 078dc13 into master Mar 18, 2021
@marcotc marcotc deleted the graceful-resource-shutdown branch March 18, 2021 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev/testing Involves testing processes (e.g. RSpec)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants