Repository navigation
fix: make lazily populated runtime caches thread-safe - #1470
Merged
Merged
Conversation
Collaborator
Author
|
This change is part of the following stack: Change managed by git-spice. |
jwils
approved these changes
Nov 24, 2026
On JRuby, concurrent writes to a plain `Hash` can lose entries. These caches live on long-lived objects and get filled while events are indexed or GraphQL requests run. - Add `Support::ThreadSafeCache`, a lazily populated cache guarded by a reentrant `Monitor`. - Use it for the JSON schema validator caches (`ValidatorFactory`, `EnvelopeValidator`), the JSON schemas by version in `SchemaArtifacts::FromDisk`, the record preparers by JSON schema version, `ExtensionLoader`, `IndexDefinition::Base#list_counts_field_paths_for_source`, and the GraphQL `Schema` type and `Type` enum value caches. - Build `RecordPreparerFactory`'s scalar indexing preparer map eagerly, since the scalar types are known up front. Generated with Claude Code
myronmarston
force-pushed
the
myron/threadsafe-runtime-caches
branch
from
November 24, 2026 19:26
4691e4c to
6075535
Compare
myronmarston
marked this pull request as ready for review
November 24, 2026 22:06
myronmarston
requested review from
BrianSigafoos-SQ,
bsorbo,
ellisandrews-toast,
jwondrusch,
marcdaniels-toast and
rossroberts-toast
as code owners
November 24, 2026 22:06
jwils
added a commit
that referenced
this pull request
Nov 29, 2026
Replace the explicit `@mutex` and plain `@types`/`@enums` hashes with `Support::ThreadSafeCache` now that #1470 is on `main`, keeping reentrant nested type resolution and making `@enums` explicitly thread-safe. Co-authored-by: Neo <f833053fa3445a7878359a1bd17c4f6b4ed1ceb3e7414f306f4929b1f9032d02@buzz> Signed-off-by: Josh Wilson <joshuaw@squareup.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On JRuby, concurrent writes to a plain
Hashcan lose entries. #1469 guarded 2 lazily populated caches with inline mutexes. 7 more caches have the same race: each lives on an object built once per process and gets entries while events are indexed or GraphQL requests run. This PR adds one shared cache type, uses it for those 7, and switches #1469's 2 caches to it.Summary
Monitor, not aMutex.Schema#type_frombuilds aType, and that build callstype_fromfor the wrapped type while the lock is held.nilandfalseare cached:fetchcheckskey?, so a cached falsy value doesn't trigger a rebuild, as it would with||=.JSONSchema::ValidatorFactory#validator_for(#1469)JSONIngestion::EnvelopeValidatorvalidator factories (#1469)JSONIngestion::RecordPreparerFactory#for_json_schema_versionSchemaArtifacts::FromDisk#json_schemas_forRuntimeMetadata::ExtensionLoader#loadIndexDefinition::Base#list_counts_field_paths_for_sourceGraphQL::Schema#type_fromGraphQL::Schema::Type#enum_value_namedRecordPreparerFactoryused to load each scalar type's indexing preparer lazily. Now it builds that map in its constructor, because the scalar types are known then. A misconfigured preparer therefore fails at boot, not on the first event that uses it.Evidence
thread_safe_cache_spec.rbruns a deterministic race. The test holds thread 1 inside the block until thread 2 is asleep, then counts block calls.||=. Two examples failed: "evaluates the block only once when another thread requests the same key..." (expected: 1, got: 2), and "cachesnilandfalsevalues" (expected: 2, got: 4).Per-gem suites (support, schema_artifacts, json_ingestion, datastore_core, graphql, indexer) pass with 100% line and branch coverage. Steep is clean.
Merge Danger
Door: two-way
Blast Radius: small. Every indexed event and every GraphQL request now acquires an uncontended
Monitorfor each cache it reads.🤖 Generated with Claude Code