Sha256: c59caec2121e2538fb40474ed3c5c2d48b3d6b24bd8486e9ab7a0bf7e6fd5654

Contents?: true

Size: 1.82 KB

Versions: 18

Compression:

Stored size: 1.82 KB

Contents

# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

# ensure that we're being loaded with an agent
# NOTE: THIS CLASS WILL NOT BE RELOADED B/C WE CANNOT RELOAD THE STANDARD
# thread file

# Request context is still tracked per-thread.  There isn't anything
# particularly order-dependent or stateful about request contexts.  It
# exists mostly for reference.
#
# Scope HAS to exist per-fiber.
# Check to see if Thread#initialize has been prepended. If it hasn't, then we should use the alias approach, so that
# others can continue to alias it. If it has been, then we must use prepend ourselves.
if Thread.instance_method(:initialize).owner == Thread
  # Alias-chaining Thread#initialize.
  class Thread
    alias_method :cs__initialize, :initialize

    def initialize *args, &block
      # Thread.current still references the original(callee) thread that is instantiating this new fiber during
      # initialization.
      Contrast::Components::Scope::MONITOR.synchronize do
        if (current_context = Thread.current[:current_context])
          self[:current_context] = current_context.dup
        end

        Contrast::Components::Scope.sweep_dead_ecs
      end

      cs__initialize(*args, &block)
    end
  end
else
  # Prepending Thread#initialize.
  module ContrastThread
    def initialize *args, &block
      # Thread.current still references the original(callee) thread that is instantiating this new fiber during
      # initialization.
      Contrast::Components::Scope::MONITOR.synchronize do
        if (current_context = Thread.current[:current_context])
          self[:current_context] = current_context.dup
        end
        Contrast::Components::Scope.sweep_dead_ecs
      end
      super
    end
  end
  Thread.prepend(ContrastThread)
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
contrast-agent-7.6.1 lib/contrast/extension/thread.rb
contrast-agent-7.6.0 lib/contrast/extension/thread.rb
contrast-agent-7.5.0 lib/contrast/extension/thread.rb
contrast-agent-7.4.1 lib/contrast/extension/thread.rb
contrast-agent-7.4.0 lib/contrast/extension/thread.rb
contrast-agent-7.3.2 lib/contrast/extension/thread.rb
contrast-agent-7.3.1 lib/contrast/extension/thread.rb
contrast-agent-7.3.0 lib/contrast/extension/thread.rb
contrast-agent-7.2.0 lib/contrast/extension/thread.rb
contrast-agent-7.1.0 lib/contrast/extension/thread.rb
contrast-agent-7.0.0 lib/contrast/extension/thread.rb
contrast-agent-6.15.3 lib/contrast/extension/thread.rb
contrast-agent-6.15.2 lib/contrast/extension/thread.rb
contrast-agent-6.15.1 lib/contrast/extension/thread.rb
contrast-agent-6.15.0 lib/contrast/extension/thread.rb
contrast-agent-6.14.0 lib/contrast/extension/thread.rb
contrast-agent-6.13.0 lib/contrast/extension/thread.rb
contrast-agent-6.12.0 lib/contrast/extension/thread.rb