Skip to content

Commit

Permalink
Add clarification for expected behavior of yield
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoanjo committed Jul 2, 2024
1 parent 533eeca commit 259e134
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/datadog/profiling/ext/dir_monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Profiling
module Ext
# All Ruby versions as of this writing have bugs in the dir class implementation, causing issues such as
# https://github.com/DataDog/dd-trace-rb/issues/3450 .
# See also https://bugs.ruby-lang.org/issues/20586 for more details.
#
# This monkey patch for the Ruby `Dir` class works around these bugs for affected Ruby versions by temporarily
# blocking the profiler from interrupting system calls.
Expand Down Expand Up @@ -43,6 +44,12 @@ def children(*args, &block)
Datadog::Profiling::Collectors::CpuAndWallTimeWorker._native_resume_signals
end

# NOTE: When wrapping methods that yield, it's OK if the `yield` raises an exception while signals are
# enabled. This is because:
# * We can call `_native_resume_signals` many times in a row, both because it's idempotent, as well as it's
# very low overhead (see benchmarks/profiler_hold_resume_interruptions.rb)
# * When an exception is being raised, the iteration will stop anyway, so there's no longer a concern of a
# signal causing Ruby to return an incorrect value
def each_child(*args, &block)
if block
begin
Expand Down Expand Up @@ -80,6 +87,7 @@ def entries(*args, &block)
Datadog::Profiling::Collectors::CpuAndWallTimeWorker._native_resume_signals
end

# See note on methods that yield above.
def foreach(*args, &block)
if block
begin
Expand All @@ -103,6 +111,7 @@ def foreach(*args, &block)
end
end

# See note on methods that yield above.
def glob(*args, &block)
if block
begin
Expand Down Expand Up @@ -153,6 +162,7 @@ def children(*args, **kwargs, &block)
Datadog::Profiling::Collectors::CpuAndWallTimeWorker._native_resume_signals
end

# See note on methods that yield above.
def each_child(*args, **kwargs, &block)
if block
begin
Expand Down Expand Up @@ -190,6 +200,7 @@ def entries(*args, **kwargs, &block)
Datadog::Profiling::Collectors::CpuAndWallTimeWorker._native_resume_signals
end

# See note on methods that yield above.
def foreach(*args, **kwargs, &block)
if block
begin
Expand All @@ -213,6 +224,7 @@ def foreach(*args, **kwargs, &block)
end
end

# See note on methods that yield above.
def glob(*args, **kwargs, &block)
if block
begin
Expand Down Expand Up @@ -251,6 +263,7 @@ def home(*args, **kwargs, &block)
if RUBY_VERSION.start_with?('2.')
# Monkey patches for Dir (Ruby 2 version). See DirMonkeyPatches above for more details.
module DirInstanceMonkeyPatches
# See note on methods that yield above.
def each(*args, &block)
if block
begin
Expand All @@ -274,6 +287,7 @@ def each(*args, &block)
end

unless RUBY_VERSION.start_with?('2.5.') # This is Ruby 2.6+
# See note on methods that yield above.
def each_child(*args, &block)
if block
begin
Expand Down Expand Up @@ -322,6 +336,7 @@ def pos(*args, &block)
else
# Monkey patches for Dir (Ruby 3 version). See DirMonkeyPatches above for more details.
module DirInstanceMonkeyPatches
# See note on methods that yield above.
def each(*args, **kwargs, &block)
if block
begin
Expand All @@ -344,6 +359,7 @@ def each(*args, **kwargs, &block)
end
end

# See note on methods that yield above.
def each_child(*args, **kwargs, &block)
if block
begin
Expand Down

0 comments on commit 259e134

Please sign in to comment.