Skip to content

Thread.current causes issues with fiber based servers like falcon #540

Closed

Description

@pschrammel

Hi,

currently i do these patches (AI generated).

Rails.application.config.after_initialize do
  # Patch 1: Graphiti.context (request-scoped metadata)
  module Graphiti
    class << self
      def context
        (Fiber.current.storage || {}).fetch(:graphiti_context, nil) || {}
      end

      def context=(val)
        verbose, $VERBOSE = $VERBOSE, nil
        Fiber.current.storage = (Fiber.current.storage || {}).merge(graphiti_context: val)
      ensure
        $VERBOSE = verbose
      end
    end
  end

  # Patch 2: TransactionHooksRecorder (before/after commit hooks)
  module Graphiti
    module Util
      class TransactionHooksRecorder
        class << self
          private

          def _hooks
            (Fiber.current.storage || {}).fetch(:_graphiti_hooks, nil)
          end

          def reset_hooks
            verbose, $VERBOSE = $VERBOSE, nil
            Fiber.current.storage = (Fiber.current.storage || {}).merge(
              _graphiti_hooks: {
                after_graph_persist: [],
                before_commit: [],
                after_commit: []
              }
            )
          ensure
            $VERBOSE = verbose
          end
        end
      end
    end
  end

  # Patch 3: RescueRegistry (exception handler context)
  module RescueRegistry
    class << self
      def context
        (Fiber.current.storage || {}).fetch(:rescue_registry_context, nil)
      end

      def context=(value)
        verbose, $VERBOSE = $VERBOSE, nil
        Fiber.current.storage = (Fiber.current.storage || {}).merge(rescue_registry_context: value)
      ensure
        $VERBOSE = verbose
      end
    end
  end
end

Sorry no tests that i can contribute. Would be nice to see graphiti working in modern web servers.

Activity

  1. added a commit that references this issue on Aug 31, 2026
    3644ac8
  2. jkeen commented on Aug 31, 2026

    @jkeen
    Collaborator

    Thanks, I ran into this bug from a different direction while working on Graphiti 2.0's changes (through scoping). Your report covers some cases I hadn't converted yet, and I've just made the relevant fixes in rescue_registry and the beta branch of this gem. They'll be in included in beta.13

  3. added a commit that references this issue on Aug 31, 2026
    6569935
  4. github-actions commented on Sep 1, 2026

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 2.0.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions