GraphQL::Schema::Visibility is not thread safe

Open
#5,761 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@theluckystrike is already working on this.

Since Nov 26, 2026.

  • #5910 by @theluckystrike — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rails, ruby
Domain
api, backend

Research direction

Run the standalone reproduction with bundle exec ruby repro.rb and read GraphQL::Schema::Visibility#load_all, all_references, get_type, and all_interface_type_memberships. Trace when the reference index becomes visible relative to traversal and post-processing, then add coverage showing concurrent callers cannot observe partial references; done when the reproduction no longer reports an incomplete index.

Written by the indexing model from the issue text.

Description

Describe the bug

Concurrent first access to GraphQL::Schema::Visibility can expose an incomplete type reference index. In our application, Puma requests intermittently fail static validation with errors such as:

No such type FooIcon, so it can't be a fragment condition (Did you mean `UnrelatedType`?)
No such type SomethingElse, so it can't be a fragment condition

The types exist and are reachable through unions or interfaces. The errors stopped when we switched back to GraphQL::Schema::Warden.

In the application, the failure also appeared to persist: after it began, one request-serving Puma thread continued returning bad requests until the process was rebooted. We have not isolated what state remained cached or confirmed that the failure was strictly thread-local. The reproduction below demonstrates an in-flight partial reference-index read, but does not reproduce the persistent failures after traversal completes.

Versions

  • graphql: 2.6.10 in the application; the standalone reproduction demonstrates the race with 2.6.11
  • rails: Rails 8.1.x
  • Ruby: 4.0.7
  • Puma: multi-threaded; preload_app! enabled

GraphQL schema

The production schema has roughly 1,500 types. Its relevant shape is types reachable through unions or interfaces. The application overrides visible? based on query context; this may affect application visibility behavior, but the race below reproduces without a context-dependent override.

The standalone reproduction uses this minimal static schema:

class Icon < GraphQL::Schema::Object
  field :name, String
end

class Image < GraphQL::Schema::Object
  field :url, String
end

class Avatar < GraphQL::Schema::Union
  possible_types Icon, Image
end

class Query < GraphQL::Schema::Object
  field :avatar, Avatar
end

class MySchema < GraphQL::Schema
  query Query
  use GraphQL::Schema::Visibility, preload: true
end

In the application there are no named visibility profiles and dynamic: false. Enabling preload: true did not prevent the failure in our configuration.

GraphQL query

The exact production operation was not captured. This is a representative fragment-condition shape for the minimal schema:

query {
  avatar {
    ... on Icon {
      name
    }
  }
}

The standalone reproduction isolates reference-index access directly rather than executing a query. In the application, the failure occurs during static validation, before a response is produced.

Reproduction repo

https://github.com/lparry/graphql-ruby-visibility-race-repro

bundle install
bundle exec ruby repro.rb

The script pauses the first real schema traversal after it starts, reads the reference index from a second thread, then resumes the traversal. It uses a Queue to make the interleaving deterministic. Expected output with graphql-ruby 2.6.11:

Starting visibility reference-index traversal.
The test pauses that traversal, then reads the index from another thread.
While traversal is paused, Icon reference count is 0 (expected 0: index is incomplete).
After traversal completes, Icon reference count is 1 (expected 1: Avatar references Icon).
RACE REPRODUCED: the concurrent read returned before the reference index was ready.

The complete runnable script is in repro.rb.

Expected behavior

A concurrent caller should not be able to observe a partially built reference index. Visibility lookup and static validation should recognize types that are reachable through unions or interfaces, rather than intermittently reporting them as unknown.

Actual behavior

GraphQL::Schema::Visibility#load_all appears to set @loaded_all before schema traversal and reference-index post-processing finish:

elsif @loaded_all == false
  @loaded_all = true
  @visit.visit_each
end

A second thread calling all_references, get_type, or all_interface_type_memberships during traversal sees @loaded_all == true and returns without waiting. At that point, references—including those propagated from union members and interface implementations—may not have been added yet. A type with no reference recorded at that moment can be treated as unreferenced, hidden from visibility lookup, and reported as unknown during validation.

The standalone reproduction observes Icon with zero references during traversal and one reference after traversal completes. It therefore demonstrates the partial read, but not why the application failures appeared to persist until reboot.

Additional context

  • Visibility#preload ensures field definitions are loaded, but with no named profiles it does not call load_all; the shared reference index is still built lazily. Enabling preload: true did not prevent the application failure.
  • The equivalent application spec fails with expected Set[].present? to be truthy without the workaround below and passes with it.
  • Visibility::Profile#load_all_types appears to have a similar flag-before-traversal pattern. It can affect shared profiles, such as cached named profiles or the top-level profile. Given the apparent persistence of failures, this is a candidate to investigate, but we have not confirmed it is involved.

Our current workaround

To remediate this, we currently build the index at boot, before Puma forks and before request threads start:

# config/initializers/graphql_visibility_preload.rb
Rails.application.config.after_initialize do
  MySchema.visibility&.all_references if Rails.application.config.eager_load
end

Similar but slightly different previous issue

Related but distinct: graphql-ruby#5657 reported similar intermittent missing-type or missing-field symptoms during concurrent visibility checks. That reporter traced their issue to Graphiform dynamically generating types during Rails autoloading and resolved it by adding mutex/monitor protection to type generation. This report's deterministic reproduction instead shows Visibility exposing its reference index before traversal and post-processing finish, without dynamic type generation.

Dominant language
Ruby
Stars
5.4k
Forks
1.4k
Avg merge
1d 6h
Merged PRs (30d)
41

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rmosolgo/graphql-ruby

All issues in rmosolgo/graphql-ruby

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.