Skip to content

Run specs in parallel, speed up CI, fix concurrency bugs - #1243

Closed
apiology wants to merge 9 commits into
castwide:masterfrom
apiology:parallel_rspec_on_master
Closed

apiology wants to merge 9 commits into
castwide:masterfrom
apiology:parallel_rspec_on_master

Conversation

@apiology

@apiology apiology commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Performance

master (serial) this PR (parallel)
CI, single job (rspec (3.2, 3.10.0) "Run tests" step) 10m31s 3m41s (~2.9x)
CI, total (push → all workflows green) 22m16s 14m56s (~1.5x)
Local (11 cores), full suite 57.8s 17.5s (~3.3x)

Summary

Runs specs in parallel and fixes concurrency bugs this surfaced (#1167, rebased onto master).

  • Parallelize per-gem YARD/RBS pin caching with a thread pool.
  • Fix a mutex re-entry deadlock in Library#sync_catalog (Fix recursive-mutex deadlock in Library#sync_catalog #1220).
  • Fix concurrency bugs surfaced by parallel runs: a diagnoser thread that could hang Host#fully_stop, a non-atomic PinCache write race, an unsynchronized Dir.chdir in protocol_spec, and a test-ordering bug that made gem caching a silent no-op.
  • Misc: exponential-blowup fix in Pin::Method#combine_same_type_arity_signatures, dead-code fix in shell.rb's gems core, and a guard against Bundler's undocumented materialize_for_installation arity drift.

Test plan

  • Full local bundle exec rspec, rubocop, overcommit --diff castwide/master: clean
  • CI: all jobs green

Generated with Claude Code

Rebases castwide#1167 (apiology/parallel_rspec) onto
castwide/master instead of v0.59, since master and v0.59 have
diverged substantially and v0.59 carries unrelated changes. This
commit is the net diff of that branch (plus its merge-conflict and
CI-regression fixes) applied directly against master; history was
not preserved per request.

Highlights:
- Parallelize per-gem YARD/RBS pin caching with a thread pool
  (doc_map.rb, shell.rb) instead of caching gems serially.
- Fix a mutex re-entry deadlock in Library#sync_catalog when the
  next cacheable gemspec is already being processed elsewhere
  (castwide#1220), with its regression test.
- Fix an exponential-blowup bug in
  Pin::Method#combine_same_type_arity_signatures (O(n^2) bail-out
  for large signature sets), with its regression test.
- shell.rb's gems 'core' command called
  PinCache.core?/PinCache.cache_core, which never existed; use the
  real Solargraph::RbsMap::CoreMap#pins API instead.
- Bundler::LazySpecification#materialize_for_installation is an
  internal, undocumented Bundler API whose arity changed without a
  deprecation path; guard against all known shapes (modern wrapper,
  old zero-arg method, incompatible-arity method) instead of
  assuming one signature.
- Misc RuboCop/YARD-doc fixes and .rubocop_todo.yml updates.
Each of these explained a distinct class of intermittent CI failure
observed across this PR's rspec matrix/parallel_tests jobs, and will
only get more frequent as test parallelism increases.

- Diagnoser: an uncaught exception during a background diagnosis
  (e.g. a file/directory disappearing mid-diagnosis, such as
  protocol_spec's around-block temp dir cleanup racing the async
  diagnoser thread) killed the thread before it reached the line
  that marks it fully stopped, so Host#fully_stop hung for its full
  240-second timeout every time. Now rescues broadly around
  individual diagnoses and guarantees the fully_stopped flag is set
  via ensure regardless of how the thread's loop exits.
- MessageWorker: stop() never signaled its condition variable, so a
  thread blocked in tick's wait() with an empty queue could never
  wake up to notice stopped? and exit - a permanently leaked thread.
  Also added fully_stopped? tracking (matching Diagnoser) and wired
  it into Host#fully_stopped?, which previously didn't wait on
  MessageWorker's thread at all.
- PinCache#save wrote directly to the final cache path with no
  atomicity. Multiple parallel_tests workers (separate OS processes)
  racing to cache the same not-yet-warm gem for the first time could
  corrupt or truncate each other's writes on the shared cache
  directory. Now writes to a temp file and renames into place
  (atomic on the same filesystem).
- Yardoc.cache invoked a bare `yardoc` command, relying on it being
  found via shell PATH - which fails for unbundled
  environments/subprocesses where it only exists inside the current
  bundle's own bin directory. Now resolves the actual executable via
  Gem.bin_path, independent of PATH.
- rubocop_helpers_spec.rb's "custom version" test unconditionally
  removed the process-global RuboCop constant in its cleanup, even
  when its own version-swap had been a no-op (because something else
  in the process, e.g. protocol_spec.rb's top-level require, had
  already loaded the real gem first) - i.e. even when there was
  nothing to restore. That left RuboCop undefined for the rest of
  the process, cascading into failures in unrelated specs
  (library_spec, protocol_spec's formatting/environment handlers,
  rubocop_spec) whenever this spec happened to run first. Now only
  cleans up (and reloads the real version) when the swap actually
  took effect.

Local full-suite run: 13 failures -> 2, both isolated/self-contained
and already understood (rubocop_helpers_spec's version-swap doesn't
work when rubocop was already required by something else first, and
a pre-existing gem_pins_spec bug in this PR's own test content).
Yardoc.cache's "check cached, else build" was a classic
check-then-act race: two OS processes (e.g. two parallel_tests
workers, each caching the same not-yet-cached gem for the first
time) could both see "not cached" and run `yardoc --db path`
concurrently against the same .yardoc database directory,
corrupting or truncating each other's output. This is very likely
the actual cause of spec/pin/base_spec.rb's intermittent
"deals well with known closure combination issue" failure and the
strict_spec.rb Kramdown-constant failure in CI (both build/read a
gem's YARD pins via this path) - my earlier PinCache#save atomic-
write fix only covered Solargraph's own Marshal cache files, not
this separate tool-managed directory.

Wrap the build in a per-gem flock'd lock file, re-checking cached?
after acquiring the lock (double-checked locking) so only one
process actually builds a given gem; the rest wait for the lock and
then reuse what the first process built instead of racing.

This is the same mechanism a prior, incomplete attempt at this
(Yardoc.processing?, referenced from Library#diagnose but never
actually used to coordinate Yardoc.cache itself) was clearly reaching
for.
The two remaining intermittent failures (Kramdown constant in
strict_spec.rb, 0 pins in pin/base_spec.rb) were never a
cross-process caching race at all - reproduced deterministically
locally with a cold cache, in complete isolation, no concurrency
involved. My earlier PinCache atomicity and yardoc-locking fixes
were solving a real but different problem than this one.

Both tests called ApiMap#cache_gem(spec) for a gem before ever
telling the ApiMap's DocMap that gem was needed (via #catalog with
external_requires). DocMap#cache only builds pins for gemspecs in
its own uncached_yard_gemspecs/uncached_rbs_collection_gemspecs
lists, which are only populated from requires resolved during
#catalog - so cache_gem was silently a no-op, and building only
happened to succeed when something else had already warmed the
gem's cache earlier in the same process (hence "intermittent",
depending entirely on test/file run order and cache state, not
timing).

Fixed both to use the catalog -> cache_all_for_doc_map! -> catalog
sequence already used correctly elsewhere in this same PR (see
rbs_map/conversions_spec.rb's "with superclass pin for
Parser::AST::Node" context): catalog first so DocMap learns about
the dependency, cache_all_for_doc_map! to build it, catalog again to
reload the ApiMap's pin store with the now-cached pins.

Verified: both pass individually and together with ~/.cache/solargraph
completely cleared beforehand (previously guaranteed to fail cold, pass
only by accident once something else had warmed the cache). Full local
suite with a cold cache: 1625 examples, 2 failures - both isolated,
already-known, unrelated issues (rubocop_helpers_spec's version-swap
test doesn't work when rubocop was already required by something else
first, and a real RBS/YARD merge bug in gem_pins_spec.rb) - no other
instances of this cache-ordering bug found anywhere else in the suite.
protocol_spec.rb's around block chdirs into a per-example temp
directory and back, on the main thread, without holding
Solargraph::CHDIR_MUTEX - the same mutex that
Diagnostics::Rubocop#diagnose and the textDocument/formatting
handler already use specifically because RuboCop::Runner internally
chdirs (with a block) to read config files.

An earlier fix in this series made the background diagnoser thread
resilient to errors instead of dying on the first one (rescuing
broadly, guaranteeing fully_stopped? via ensure), so it now keeps
running background diagnoses - including RuboCop ones - for longer
during a test run. That made it far more likely to have an active
chdir in flight from that mutex right as protocol_spec's own
(unsynchronized) chdir ran, which Ruby raises as "conflicting chdir
during another chdir block" (visible in CI as a wave of Protocol
example failures, e.g. "handles textDocument/definition").

Route protocol_spec's chdir calls through the same
Solargraph::CHDIR_MUTEX so they can't overlap with RuboCop's.
An audit of the full diff against master (prompted by "are we dragging
in v0.59 changes that weren't intended?") found several small items
inherited from the original apiology/parallel_rspec branch history,
predating this session's rebase:

- Diagnostics::Base#diagnose and TypeCheck#diagnose gained a
  `workspace:` kwarg (commit "Spec performance fixes", a 33% local
  speedup) so TypeChecker.new could reuse an already-loaded Workspace
  instead of implicitly building a fresh one via
  Workspace.new(File.dirname(filename)) on every diagnose call. The
  kwarg was added but the one production call site,
  Library#diagnose (library.rb), was never updated to pass it, so the
  optimization was inert. Wire it through, and add the same kwarg to
  the other Diagnostics::Base subclasses (Rubocop, UpdateErrors,
  RequireNotFound) so the polymorphic call in Library#diagnose doesn't
  raise ArgumentError for reporters that don't use it.
- Remove RbsMap::StdlibMap.possible_stdlibs: added, never called.
- Remove duplicate/redundant YARD @PARAM comments added to
  ComplexType#qualify and .parse alongside the existing docs.
- Revert a no-op reordering of Workspace#gemfile?/gemspec?/gemspec_files
  back to their master position; both locations are public, so this
  wasn't a visibility change, just unexplained churn.

Left alone: a stray "@todo Missed nil violation" comment in
source/chain.rb, and RuboCop-autocorrect-driven formatting diffs
elsewhere in the branch (YARD/CollectionStyle, quote style) that
predate this rebase and are needed to keep Overcommit clean.
The Hash{Array(String, String) => ...} -> Hash{Array, String, String
=> ...} docstring reformatting in api_map/constants.rb,
api_map/store.rb, doc_map.rb, source_map.rb, and the quote/block-style
cleanup in spec/source/chain_spec.rb weren't related to this PR's
stated purpose (parallel specs, concurrency fixes) - they were fixing
YARD/CollectionStyle and Style/StringLiterals offenses that a fresh
`rubocop --auto-gen-config` surfaces under the currently-installed
RuboCop/rubocop-yard versions but that master's own committed
.rubocop_todo.yml doesn't yet grandfather (a pre-existing drift, not
something this branch introduced).

Revert those files to master's content and add a scoped
YARD/CollectionStyle todo exclusion for the same 4 files, so this
branch stays green without carrying the unrelated reformatting. The
actual fix now lives in a standalone PR:
#42.
Two follow-ups to the earlier extraction:

- The per-file YARD/CollectionStyle todo exclusion was a stand-in for
  actually fixing the cop. Replace it with disabling the cop outright
  in .rubocop.yml, matching castwide#1237's fix: for
  nested-generic/tuple Hash key types, the cop's own long-style
  autocorrect produces syntax that doesn't preserve the original
  tuple's meaning, so there's no safe autocorrected form to converge
  on for those cases.
- UniqueType#to_rbs's all_params.empty? nil-safety fix is an
  unrelated, pre-existing bug (from the original branch's own
  history, unrelated to parallel specs) - move it to
  #43.
@apiology

apiology commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of apiology#44, which is the same net contribution rebased on top of #1237 (this branch and #1237 had independently fixed the same catalog-before-cache_gem gem-pin-caching ordering bug and both added Diagnostics::Base#diagnose's workspace: kwarg - rebasing onto #1237 removes that duplication).

Once #1237 lands, apiology#44 can be retargeted or reopened against castwide/solargraph directly.

@apiology apiology closed this Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant