Skip to content

A hosted gem rollback interrupted during the native rebuild leaves the patched gems installed under the restored Gemfile.lock: no command finishes it, and bundle pristine then fails on the checksum and removes the gem #2500

Backlog

Description

[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).

Summary

A hosted rollback restores Gemfile and Gemfile.lock first and replaces the installed gems with the upstream builds afterwards. For a gem with a native extension that second step is a compile, announced with "this can take several minutes". If the run is stopped during it (Ctrl-C, a cancelled CI job, SIGTERM), the pair is the upstream one and the gem home still holds the patched gem and its patched .gem archive. Nothing records that the refresh is owed, so no socket-patch command finishes it: rollback, repair and list all say "No patches in this project".

Not a regression: v5.1.0 behaves the same (it only lacks the "Rebuilding" line).

Impact

  • The user rolled the patch back, and the app still loads the patched code. bundle install (frozen too) prints "Bundle complete!" over it.
  • On a lock with CHECKSUMS (the Bundler 4 default) bundle pristine removes the gem, then fails on the checksum of the patched archive (exit 37). After that the app does not load and bundle install exits 37 too. A project that installed cleanly before socket-patch touched it no longer does.
  • On a lock without CHECKSUMS (Bundler 2.5, 2.6) bundle pristine reinstalls the patched gem from the archive, so the patched code stays for good, with nothing to say so.
  • Gems queued behind the native build are stuck too. In a project with nokogiri, puma, rack and rest-client, an interrupt during puma's build left rack and rest-client (pure Ruby) patched as well.
  • vendor/bundle/ruby/<abi>/.socket-patch-refresh/ stays in the gem home. Nothing removes it until a later hosted scan or rollback has work to do there.

What does recover it: socket-patch scan (wires the patches again) followed by socket-patch rollback left to finish; or rm -f of the gem's specifications/ entry and cache/ archive followed by bundle install. Neither is printed anywhere, and the second is what the docs give for a shared gem home only.

The opposite direction is fine: a hosted scan interrupted during the same step leaves upstream copies under a patched lock, vex omits them (not_applied), and the next scan finishes the refresh.

Repro

Uses the repo's stand-in patch service (tests/nokogiri/patch_service.rb) for puma 6.4.3.

cat > serve.rb <<'RB'
require "./tests/nokogiri/patch_service.rb"
s = PatchService.new("puma", "6.4.3", [], "11111111-1111-4111-8111-111111111111",
                     "GHSA-aaaa-bbbb-cccc", ARGV[0], "\nSOCKET_PATCHED = true\n")
puts s.url; $stdout.flush; sleep
RB
ruby serve.rb /tmp/svc > /tmp/url &   # from the repo root
until [ -s /tmp/url ]; do sleep 1; done; U=$(cat /tmp/url)
A="--api-url $U --patch-server-url $U --api-token fake --org nokogiri-e2e"

mkdir app && cd app
printf 'source "https://rubygems.org"\ngem "puma", "6.4.3"\n' > Gemfile
echo 'require "bundler/setup"; require "puma"; puts defined?(SOCKET_PATCHED) ? "PATCHED code loaded" : "upstream code loaded"' > app.rb
bundle config set --local path vendor/bundle && bundle install
cp Gemfile Gemfile.orig; cp Gemfile.lock Gemfile.lock.orig

socket-patch scan --mode hosted $A
bundle exec ruby app.rb                      # PATCHED code loaded

socket-patch rollback --yes $A               # press Ctrl-C when it prints
                                             # "Rebuilding the native extension of puma-6.4.3 from its upstream gem"
cmp Gemfile Gemfile.orig && cmp Gemfile.lock Gemfile.lock.orig   # identical
bundle exec ruby app.rb                      # PATCHED code loaded
socket-patch rollback --yes $A               # exit 1: No patches in this project; nothing to roll back.
socket-patch repair $A                       # No patches in this project; nothing to repair.
BUNDLE_FROZEN=true bundle install            # Bundle complete!
bundle exec ruby app.rb                      # PATCHED code loaded
bundle pristine                              # Bundler 4.1.1: exit 37, mismatched checksums
bundle exec ruby app.rb                      # cannot load such file -- puma
bundle install                               # exit 37

In a script, start the rollback in its own process group and send the signal to the group once the "Rebuilding" line appears. It reproduced on every run: 9 of 9 on a four-gem project (SIGINT and SIGTERM, 1.0 s to 4.5 s into the run) and 5 of 5 with the script above.

Expected

docs/ecosystems.md, "Ruby gems: gems already installed": after a hosted scan "bundle exec runs the patched code, and every kind of reinstall keeps it. rollback and remove do the same with the upstream gem." The same section says, for a copy a rollback leaves behind, that the run "warns (upstream_gem_stale_install), naming the files to remove before bundle install". "Ruby gems: native extensions" promises for agent mode that an interrupted rebuild is finished or undone by the next apply or rollback.

So an interrupted rollback should either be finished by the next rollback (or repair), or leave a state a later command reports with the remedy. The stage directory should be removed by the next run.

Actual

$ socket-patch rollback --yes      # interrupted
Restored pkg:gem/puma@6.4.3 to its upstream registry entry
Rebuilding the native extension of puma-6.4.3 from its upstream gem; this can take several minutes.
^C                                 # exit 130
$ bundle exec ruby app.rb
PATCHED code loaded
$ socket-patch rollback --yes
Error: No patches in this project; nothing to roll back.
$ bundle pristine
Bundler found mismatched checksums. This is a potential security risk.
  puma (6.4.3) sha256=...
    from the lockfile CHECKSUMS at Gemfile.lock:18:16
$ ls -a vendor/bundle/ruby/3.3.0 | grep socket
.socket-patch-refresh

Versions

socket-patch Ruby Bundler Signal Result
main d987721 (5.2.0) 3.3.6 4.1.1 SIGINT, SIGTERM reproduces; bundle pristine exits 37 and removes the gem
main d987721 (5.2.0) 3.2.6 2.6.9 SIGINT reproduces; bundle pristine reinstalls the patched gem
main d987721 (5.2.0) 3.2.6 2.5.23 SIGTERM reproduces; bundle pristine reinstalls the patched gem
5.1.0 3.3.6 4.1.1 SIGINT at 1.5 s and 2.5 s reproduces

Linux only. Nothing here depends on the platform, so macOS should behave the same. remove and the vendored unwind (vendor --revert of a gem that was hosted first) run the same refresh and were not tested. A gem whose build takes minutes (nokogiri from source, grpc) has a much wider window than puma's 4 seconds.

Suspect code

  • crates/socket-patch-cli/src/commands/gem_refresh.rs:1269 (after_restore): runs after the pair is restored, and nothing on disk says a refresh is pending.
  • crates/socket-patch-cli/src/commands/hosted_unwind.rs:101: the caller. A later rollback stops at "No patches in this project" before it looks at the gem home.
  • crates/socket-patch-core/src/patch/gem_refresh.rs:364 (refresh_installed): the stage directory under .socket-patch-refresh/.

Related: #2484 (a hosted scan interrupted between its two writes), #2337 (stage files left by an interrupted run).


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    on Dec 4, 2026
  2. mikolalysenko commented on Dec 4, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Triage: related to #2484 (a hosted gem scan interrupted between its two writes). Not a duplicate: this one is a rollback interrupted after both project files are restored, during the gem-home refresh, so the trigger and the leftover state differ. Both come from a hosted gem run that records nothing about the step it still owes.


    Generated by Claude Code

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

    agent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions