Description
Problem
Cargo/libgit2 crashes on Apple Silicon M1 when installed through homebrew
This is definitely a weird one - I've been unable to reproduce this outside of homebrew's environment, but it's happening rather consistently when attempting to take actions in homebrew's cargo root.
The most obvious symptom of this is the rather oddly phrased error message seen below
Updating crates.io index
warning: spurious network error (2 tries remaining): failed to lock file '$CARGO_HOME/registry/index/github.com-1ecc6299db9ec823/.git/refs/remotes/origin/master.lock' for writing: ; class=Os (2)
warning: spurious network error (1 tries remaining): failed to lock file '$CARGO_HOME/registry/index/github.com-1ecc6299db9ec823/.git/refs/remotes/origin/master.lock' for writing: ; class=Os (2)
error: failed to fetch `https://github.com/rust-lang/crates.io-index`
Caused by:
failed to lock file '$CARGO_HOME/registry/index/github.com-1ecc6299db9ec823/.git/refs/remotes/origin/master.lock' for writing: ; class=Os (2)
($CARGO_HOME
being homebrew's own cargo root - $HOME/Library/Caches/Homebrew/cargo_cache
)
The error message naturally just means "lock file already exists", though this wasn't incredibly obvious at first glance...
Upon further investigation, on the first run without the lock file in place, cargo actually crashes in the middle of a libgit2 call (crash dump available below).
At this point I realized that I could simply ask cargo to use the git cli instead of libgit2, and after putting more time than I care to admit hitting my head against this, I was more than happy to take that option. Hopefully this information will help someone else to have less frustrations, or hopefully even fully diagnose and fix the issue at play.
Steps
- Edit homebrew formula to use aarch64 compiler for bootstrapping (see notes)
- Install the lightly customized
rust
formula, building from source, using the HEAD release (brew install -s --HEAD rust
) - Attempt to install another brew formula that uses rust's build environment (
brew install -s --HEAD tree-sitter
, for instance, though others also fail) - Observe the failure described above
Possible Solution(s) / Workaround
Change cargo config to use the git cli for interactions.
(printf '[net]\ngit-fetch-with-cli = true' >> ~/Library/Caches/Homebrew/cargo_cache/config
, in this case)
Notes
Output of cargo version
: cargo 1.50.0
Diff to rust formula
diff --git a/Formula/rust.rb b/Formula/rust.rb
index 64d42a594e..68b1926dd1 100644
--- a/Formula/rust.rb
+++ b/Formula/rust.rb
@@ -42,8 +42,13 @@ class Rust < Formula
resource "cargobootstrap" do
on_macos do
# From https://github.com/rust-lang/rust/blob/#{version}/src/stage0.txt
- url "https://static.rust-lang.org/dist/2020-11-19/cargo-1.48.0-x86_64-apple-darwin.tar.gz"
- sha256 "ce00d796cf5a9ac8d88d9df94c408e5d7ccd3541932a829eae833cc8e57efb15"
+ if Hardware::CPU.arch == :arm64
+ url "https://static.rust-lang.org/dist/2020-12-20/cargo-nightly-aarch64-apple-darwin.tar.gz"
+ sha256 "740d16056af3061fd22e9b5bc900483779a4a603f5e63750593d465ee58e019b"
+ else
+ url "https://static.rust-lang.org/dist/2020-11-19/cargo-1.48.0-x86_64-apple-darwin.tar.gz"
+ sha256 "ce00d796cf5a9ac8d88d9df94c408e5d7ccd3541932a829eae833cc8e57efb15"
+ end
end
on_linux do
@@ -78,17 +83,9 @@ class Rust < Formula
else
args << "--release-channel=stable"
end
- # Cross-compile arm64 with x86_64 bootstrap compiler.
- if Hardware::CPU.arch == :arm64
- args << "--build=x86_64-apple-darwin"
- args << "--host=aarch64-apple-darwin"
- args << "--target=aarch64-apple-darwin"
- system "./configure", *args
- system "arch", "-x86_64", "make"
- else
- system "./configure", *args
- system "make"
- end
+
+ system "./configure", *args
+ system "make"
system "make", "install"
resource("cargobootstrap").stage do
macOS Crash Dump
Process: cargo [5638]
Path: /opt/homebrew/*/cargo
Identifier: cargo
Version: 0
Code Type: ARM-64 (Native)
Parent Process: zsh [2671]
Responsible: iTerm2 [2661]
User ID: 501
Date/Time: 2020-12-21 19:52:03.839 -0600
OS Version: macOS 11.1 (20C69)
Report Version: 12
Anonymous UUID: 0CCE72DD-EE13-823A-089C-2C884E109E6E
Time Awake Since Boot: 4600 seconds
System Integrity Protection: enabled
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: EXC_ARM_DA_ALIGN at 0x000000013bf35ebc
Exception Note: EXC_CORPSE_NOTIFY
Termination Signal: Bus error: 10
Termination Reason: Namespace SIGNAL, Code 0xa
Terminating Process: exc handler [5638]
VM Regions Near 0x13bf35ebc:
MALLOC_TINY 13be00000-13bf00000 [ 1024K] rw-/rwx SM=PRV
--> MALLOC_TINY 13bf00000-13c000000 [ 1024K] rw-/rwx SM=PRV
MALLOC_SMALL 13c000000-13c800000 [ 8192K] rw-/rwx SM=PRV
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 cargo 0x0000000101277eb4 git_repository__configmap_lookup + 32
1 cargo 0x00000001012ae014 git_refdb_should_write_reflog + 48
2 cargo 0x00000001012ae014 git_refdb_should_write_reflog + 48
3 cargo 0x00000001012b0a58 refdb_fs_backend__write_tail + 248
4 cargo 0x00000001012aecf0 refdb_fs_backend__write + 176
5 cargo 0x00000001012b2cf8 reference__create + 544
6 cargo 0x00000001012b2a74 git_reference_create_matching + 176
7 cargo 0x00000001012b7c38 update_tips_for_spec + 672
8 cargo 0x00000001012b7288 git_remote_update_tips + 448
9 cargo 0x00000001012b7078 git_remote_fetch + 384
10 cargo 0x000000010102f440 git2::remote::Remote::fetch::h64b7a377f0447347 + 412
Thread 1:
0 libsystem_pthread.dylib 0x000000018d1e7d8c start_wqthread + 0
Thread 2:
0 libsystem_pthread.dylib 0x000000018d1e7d8c start_wqthread + 0
Thread 0 crashed with ARM Thread State (64-bit):
x0: 0x000000016f0e121c x1: 0x000000013bf35d30 x2: 0x000000000000000a x3: 0x0000000000000003
x4: 0x000000010be05b30 x5: 0x0000000000000003 x6: 0x0000000000000000 x7: 0x000000010be05940
x8: 0x000000013bf35d58 x9: 0x0000000000000303 x10: 0x00000000000bcfa0 x11: 0x0000000000003f2a
x12: 0x000000010be00000 x13: 0x00000000203b1f66 x14: 0x00000000203b1e68 x15: 0x000000000000fe26
x16: 0x000000000000fe26 x17: 0x0000000000004828 x18: 0x0000000000000000 x19: 0x000000016f0e1264
x20: 0x000000013bf36b00 x21: 0x000000010be05ad0 x22: 0x000000013bf35ebc x23: 0x000000013bf36b20
x24: 0x000000010be05b0c x25: 0x0000000000000000 x26: 0x0000000000000001 x27: 0x000000010be05ab0
x28: 0x0000000000000002 fp: 0x000000016f0e1200 lr: 0x00000001012ae014
sp: 0x000000016f0e11b0 pc: 0x0000000101277eb4 cpsr: 0x60000000
far: 0x000000013bf35ebc esr: 0x92000021
Binary Images:
[Excluded to keep this an almost-reasonable length, but can be attached if necessary]
External Modification Summary:
Calls made by other processes targeting this process:
task_for_pid: 0
thread_create: 0
thread_set_state: 0
Calls made by this process:
task_for_pid: 0
thread_create: 0
thread_set_state: 0
Calls made by all processes on this machine:
task_for_pid: 3399
thread_create: 0
thread_set_state: 0
VM Region Summary:
ReadOnly portion of Libraries: Total=573.1M resident=0K(0%) swapped_out_or_unallocated=573.1M(100%)
Writable regions: Total=657.8M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=657.8M(100%)
VIRTUAL REGION
REGION TYPE SIZE COUNT (non-coalesced)
=========== ======= =======
Activity Tracing 256K 1
Dispatch continuations 64.0M 1
Kernel Alloc Once 32K 1
MALLOC 104.1M 26
MALLOC guard page 96K 5
MALLOC_MEDIUM (reserved) 480.0M 4 reserved VM address space (unallocated)
STACK GUARD 32K 2
Stack 9248K 3
Stack Guard 56.0M 1
VM_ALLOCATE 160K 3
__AUTH 271K 62
__AUTH_CONST 3497K 151
__DATA 1328K 145
__DATA_CONST 4806K 151
__DATA_DIRTY 302K 62
__LINKEDIT 482.6M 6
__OBJC_CONST 433K 41
__OBJC_RO 64.5M 1
__OBJC_RW 2544K 1
__TEXT 90.5M 163
__UNICODE 588K 1
mapped file 1888K 4
shared memory 80K 5
=========== ======= =======
TOTAL 1.3G 840
TOTAL, minus reserved VM space 886.7M 840