Skip to content

Vendored platform gem is vendored a second time after its lead platform leaves the lock (bundle lock --remove-platform), and then vendor --check, --prune and rollback all fail #2435

Done

Description

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

Summary

A gem that Gemfile.lock pins as precompiled platform gems is vendored through one "lead" patch, whose uuid names its directory under .socket/vendor/gem/. When the run has no patch for the ruby gem (the case the docs describe as "not offered to the organisation"), the lead is the patch of the first platform the lock names. If that platform later leaves the lock (bundle lock --remove-platform aarch64-linux-gnu), the next scan --mode vendored picks the next platform's patch as the lead and vendors the gem a second time, in a second directory. The first copy and its ledger entry are left behind, and from then on the project cannot be cleaned up:

  • vendor --check exits 1 ("wiring missing") for the superseded entry, so a CI gate on it fails;
  • scan --mode vendored --prune keeps both copies ("kept 1 drifted vendored entry");
  • rollback exits 1 for both entries, the live one included, and leaves Gemfile, Gemfile.lock and both directories as they are.

New on main with the per-platform patch model (#2422); v5.1.0 does not vendor platform gems this way, so there is no released version to compare with. When the ruby gem's patch is available to the run the directory is named after it and the same steps pass.

Impact

The user cannot roll back or remove the vendored gem with the tool: the bar for vendored mode is that rollback gives back the original pair. The orphaned copy is also committed (19 MB for ffi here; 89 MB for nokogiri with 8 platforms in the larger project where this was first seen), and every remedy the messages name (vendor --revert, "undo the drift", "re-run socket-patch vendor") starts from a state the user did not create by hand: they only removed a platform with Bundler and scanned again.

Repro

svc.rb serves one fix through this repository's stand-in for the patch service (tests/nokogiri/patch_service.rb), without the ruby gem's patch:

# usage: ruby svc.rb <socket-patch checkout> <gem> <version> <platform,platform,...> [noruby]
# Serves one fix through the repository's stand-in for the patch service and prints its URL.
require File.join(ARGV[0], "tests/nokogiri/patch_service.rb")
class NoRubyPatch < PatchService
  def all_gems; super.reject { |g| g.platform.nil? }; end   # the ruby gem's patch is not offered
end
klass = ARGV[4] == "noruby" ? NoRubyPatch : PatchService
svc = klass.new(ARGV[1], ARGV[2], ARGV[3].split(","), "11111111-1111-4111-8111-111111111111",
                "GHSA-aaaa-bbbb-cccc", File.join(Dir.pwd, "svc-work"), "\nSOCKET_PATCHED = true\n")
puts svc.url
$stdout.flush
sleep

repro1.sh (run beside it with REPO=<checkout of this repository>):

#!/bin/bash
# Needs: Ruby 3.3 + Bundler 4.1.1, a socket-patch build on PATH, REPO=<socket-patch checkout>
set -u
export BUNDLER_VERSION=4.1.1 SOCKET_NO_CONFIG=1 SOCKET_NO_UPDATE_CHECK=1 SOCKET_TELEMETRY_DISABLED=1
P=aarch64-linux-gnu,aarch64-linux-musl,arm-linux-gnu,arm-linux-musl,arm64-darwin,x86_64-darwin,x86_64-linux-gnu,x86_64-linux-musl
ruby svc.rb "$REPO" ffi 1.17.4 $P noruby > svc.url & SVC=$!
until [ -s svc.url ]; do sleep 1; done; M=$(cat svc.url)
SP="--api-url $M --patch-server-url $M --api-token fake --org nokogiri-e2e"
rm -rf app && mkdir app && cd app && git init -q .
printf 'source "https://rubygems.org"\n\ngem "ffi", "1.17.4"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle lock >/dev/null
bundle lock --remove-platform ruby x86-linux-gnu x86-linux-musl >/dev/null   # platform gems only
cp Gemfile Gemfile.orig
bundle install >/dev/null
socket-patch scan --mode vendored $SP 2>&1 | grep -E '^Vendored'; grep ffi Gemfile
bundle lock --remove-platform aarch64-linux-gnu >/dev/null
socket-patch scan --mode vendored $SP 2>&1 | grep -E '^Vendored'; grep ffi Gemfile
du -sh .socket/vendor/gem/*
socket-patch vendor --check $SP 2>&1 | grep ffi; echo "vendor --check: exit ${PIPESTATUS[0]}"
socket-patch scan --mode vendored --prune $SP 2>&1 | grep -E '^(GC|All)'
socket-patch rollback $SP 2>&1 | grep -E '^(Error|Reverted)'; echo "rollback: exit ${PIPESTATUS[0]}"
cmp Gemfile.orig Gemfile; ls .socket/vendor/gem
kill $SVC

Output on main d718b07 (Ruby 3.3.6, Bundler 4.1.1, Linux x86_64):

Vendored 1 package.
gem "ffi", "1.17.4", path: ".socket/vendor/gem/e3e8e451-a3e9-4bd1-801b-84f7a1214a22/ffi-1.17.4"
Vendored 1 package.
gem "ffi", "1.17.4", path: ".socket/vendor/gem/e0d9ea2d-12a1-4df9-8608-c5c868a6e04f/ffi-1.17.4"
17M	.socket/vendor/gem/e0d9ea2d-12a1-4df9-8608-c5c868a6e04f
19M	.socket/vendor/gem/e3e8e451-a3e9-4bd1-801b-84f7a1214a22
pkg:gem/ffi@1.17.4?platform=aarch64-linux-gnu: wiring missing: no lockfile or config references .socket/vendor/gem/e3e8e451-a3e9-4bd1-801b-84f7a1214a22 any more, so a fresh install gets the unpatched package; re-run `socket-patch vendor` to rewire it
pkg:gem/ffi@1.17.4?platform=aarch64-linux-musl: committed artifact and wiring verified
vendor --check: exit 1
GC: kept 1 drifted vendored entry: lock entries were re-resolved since vendoring, so their artifacts and manifest/ledger entries were retained; undo the drift and re-run `vendor --revert` to finish.
Error: Kept vendored state for pkg:gem/ffi@1.17.4?platform=aarch64-linux-gnu: lockfile wiring drifted; vendored state left untouched
Error: Kept vendored state for pkg:gem/ffi@1.17.4?platform=aarch64-linux-musl: lockfile wiring drifted; vendored state left untouched
rollback: exit 1
Gemfile.orig Gemfile differ: char 51, line 3
e0d9ea2d-12a1-4df9-8608-c5c868a6e04f
e3e8e451-a3e9-4bd1-801b-84f7a1214a22

bundle install (frozen too) keeps working after the second scan and the app loads the patched gem; what breaks is the check, the cleanup and the way back.

Expected

docs/ecosystems.md, "Ruby gems: native extensions", the Vendored row: the vendored directory is named after the ruby gem's patch, "where that patch is not available to the run, after the patch of the first locked platform, and a gem already vendored stays in the directory it has". Either the gem stays in the directory it has when its lead's platform leaves the lock (the trees of the platforms still locked are all there), or the move is complete: the old directory and ledger entry go, vendor --check passes, and rollback restores the pre-scan Gemfile and the lock Bundler would write.

Actual

The gem is vendored twice, .socket/vendor/state.json holds two entries for it (pkg:gem/ffi@1.17.4?platform=aarch64-linux-gnu and …?platform=aarch64-linux-musl), and vendor --check, --prune and rollback fail as shown.

Versions

socket-patch Bundler Ruby Ruby gem's patch available Result
main d718b07 4.1.1 3.3.6 no fails (3 runs: ffi alone twice, and a 7-gem app with nokogiri, ffi and sqlite3)
main d718b07 4.1.1 3.3.6 yes passes: the directory stays, the removed platform's tree is dropped, rollback is byte-exact
v5.1.0 not applicable (no per-platform patches)

Linux only. Nothing here depends on the host platform, so macOS and Windows should behave the same.

First bad commit

0a1709b "Use the published patch for each locked Ruby platform (#2422)", which introduced the lead selection.

Suspect code

crates/socket-patch-cli/src/commands/vendor.rs:3660-3688 (GemUnits::with_lock): a ledger entry keeps its lead only while that patch is eligible, that is the ruby gem's patch or the patch of a platform the lock still names. Once the lead's platform leaves the lock, in_use is None and first_build becomes a new lead, but nothing reverts or migrates the entry vendored through the old one.


Generated by Claude Code

Activity

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

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-lead-platform-leaves-lock.


    Generated by Claude Code

  3. added 2 commits that reference this issue on Dec 2, 2026
    603b295
    792cb1b
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