Sha256: 8aeb0ad9fc2ad7b96495efe69a182fe0d3f257d1398f0bdf6e344dd8df0919c2

Contents?: true

Size: 1.82 KB

Versions: 24

Compression:

Stored size: 1.82 KB

Contents

# Copyright (c) 2022 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

24 entries across 24 versions & 1 rubygems

Version Path
contrast-agent-6.11.0 lib/contrast/extension/thread.rb
contrast-agent-6.10.0 lib/contrast/extension/thread.rb
contrast-agent-6.9.0 lib/contrast/extension/thread.rb
contrast-agent-6.8.0 lib/contrast/extension/thread.rb
contrast-agent-6.7.0 lib/contrast/extension/thread.rb
contrast-agent-6.6.5 lib/contrast/extension/thread.rb
contrast-agent-6.6.4 lib/contrast/extension/thread.rb
contrast-agent-6.6.3 lib/contrast/extension/thread.rb
contrast-agent-6.6.2 lib/contrast/extension/thread.rb
contrast-agent-6.6.1 lib/contrast/extension/thread.rb
contrast-agent-6.6.0 lib/contrast/extension/thread.rb
contrast-agent-6.5.1 lib/contrast/extension/thread.rb
contrast-agent-6.5.0 lib/contrast/extension/thread.rb
contrast-agent-6.4.0 lib/contrast/extension/thread.rb
contrast-agent-6.3.0 lib/contrast/extension/thread.rb
contrast-agent-6.2.0 lib/contrast/extension/thread.rb
contrast-agent-6.1.2 lib/contrast/extension/thread.rb
contrast-agent-6.1.1 lib/contrast/extension/thread.rb
contrast-agent-6.1.0 lib/contrast/extension/thread.rb
contrast-agent-6.0.0 lib/contrast/extension/thread.rb