Repository navigation
Description
I am using a combination of zig c++, golang and bazel to cross-compile a cgo program to Darwin. It compiles go stdlib in parallel and sometimes hangs. A container with a hung ps auxf looks as follows:
root@4ba08cf5c898:/x# ps auxf | cat
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 5093 0.4 0.0 3968 3472 pts/1 Ss 04:03 0:00 bash
root 5190 0.0 0.0 6700 2952 pts/1 R+ 04:03 0:00 \_ ps auxf
root 5191 0.0 0.0 2468 516 pts/1 S+ 04:03 0:00 \_ cat
root 1 0.0 0.0 709728 17392 pts/0 Ssl+ Jun16 0:01 ./bazel build -s --platforms @zig_sdk//:aarch64-macos-gnu //test:gognu
root 2423 0.0 0.0 343176 22712 pts/0 Sl+ Jun16 0:12 /root/.cache/bazelisk/downloads/bazelbuild/bazel-4.1.0-linux-x86_64/bin/bazel build -s --platforms @zig_sdk//:aarch64-macos-gnu //test:gognu
root 2428 0.5 3.1 14181376 1017740 ? Ssl Jun16 2:11 bazel(x) -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9 --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED -Xverify:none -Djava.util.logging.config.file=/root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/javalog.properties -Dcom.google.devtools.build.lib.util.LogHandlerQuerier.class=com.google.devtools.build.lib.util.SimpleLogHandler$HandlerQuerier -XX:-MaxFDLimit -Djava.library.path=/root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220/embedded_tools/jdk/lib/jli:/root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220/embedded_tools/jdk/lib:/root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220/embedded_tools/jdk/lib/server:/root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220/ -Dfile.encoding=ISO-8859-1 -jar /root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220/A-server.jar --max_idle_secs=10800 --noshutdown_on_low_sys_mem --connect_timeout_secs=30 --output_user_root=/root/.cache/bazel/_bazel_root --install_base=/root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220 --install_md5=f95ca91ebc34d56aa0f8ad499de91220 --output_base=/root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9 --workspace_directory=/x --default_system_javabase= --failure_detail_out=/root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/failure_detail.rawproto --expand_configs_in_place --idle_server_tasks --write_command_log --nowatchfs --nofatal_event_bus_exceptions --nowindows_enable_symlinks --client_debug=false --product_name=Bazel --noincompatible_enable_execution_transition --option_sources=
root 2709 0.0 0.0 5796 1632 ? S Jun16 0:00 \_ /root/.cache/bazel/_bazel_root/install/f95ca91ebc34d56aa0f8ad499de91220/process-wrapper --timeout=0 --kill_delay=15 bazel-out/host/bin/external/go_sdk/builder stdlib -sdk external/go_sdk -installsuffix darwin_arm64 -out bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_
root 2710 0.0 0.0 707676 13572 ? Ssl Jun16 0:01 \_ bazel-out/host/bin/external/go_sdk/builder stdlib -sdk external/go_sdk -installsuffix darwin_arm64 -out bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_
root 2715 0.0 0.0 1159684 31992 ? Sl Jun16 0:03 \_ external/go_sdk/bin/go install -toolexec /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/sandbox/processwrapper-sandbox/3/execroot/bazel-zig-cc/bazel-out/host/bin/external/go_sdk/builder filterbuildid -gcflags=all= -ldflags=all=-trimpath /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/sandbox/processwrapper-sandbox/3/execroot/bazel-zig-cc -asmflags=all=-trimpath /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/sandbox/processwrapper-sandbox/3/execroot/bazel-zig-cc/bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_ std runtime/cgo
root 5087 0.0 0.0 152932 30948 ? Sl Jun16 0:00 \_ /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/external/zig_sdk/zig c++ -I /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/sandbox/processwrapper-sandbox/3/execroot/bazel-zig-cc/bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_/src/net -fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2201875783/b087=/tmp/go-build -gno-record-gcc-switches -fno-common -o /tmp/go-build2201875783/b087/_cgo_.o /tmp/go-build2201875783/b087/_cgo_main.o /tmp/go-build2201875783/b087/_x001.o /tmp/go-build2201875783/b087/_x002.o /tmp/go-build2201875783/b087/_x003.o /tmp/go-build2201875783/b087/_x004.o /tmp/go-build2201875783/b087/_x005.o -target aarch64-macos-gnu
root 5088 0.0 0.0 152932 31092 ? Sl Jun16 0:00 \_ /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/external/zig_sdk/zig c++ -I /root/.cache/bazel/_bazel_root/cc8755609ad61864910f145119713de9/sandbox/processwrapper-sandbox/3/execroot/bazel-zig-cc/bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_/src/os/user -fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2201875783/b036=/tmp/go-build -gno-record-gcc-switches -fno-common -o /tmp/go-build2201875783/b036/_cgo_.o /tmp/go-build2201875783/b036/_cgo_main.o /tmp/go-build2201875783/b036/_x001.o /tmp/go-build2201875783/b036/_x002.o /tmp/go-build2201875783/b036/_x003.o /tmp/go-build2201875783/b036/_x004.o -target aarch64-macos-gnu
Both are waiting on some lock (pids are different, since I am stracing outside the container):
motiejus ~/code/bazel-zig-cc $ sudo strace -p 812414
strace: Process 812414 attached
futex(0x7f3743028b70, FUTEX_WAIT_PRIVATE, 4294967295, NULL^Cstrace: Process 812414 detached
<detached ...>
motiejus ~/code/bazel-zig-cc $ sudo strace -p 812415
strace: Process 812415 attached
futex(0x7f12064f8b70, FUTEX_WAIT_PRIVATE, 4294967295, NULL^Cstrace: Process 812415 detached
<detached ...>
kill -USR1 did not produce a stack trace. Is there any more information I can provide? Steps to reproduce on a x86_64-linux machine with a working docker installation:
$ git clone https://git.sr.ht/~motiejus/bazel-zig-cc -b hangzig
$ cd bazel-zig-cc
$ for i in $(seq 1000); do date; echo $i; time ./hangzig; done
It fails more often in builds.sr.ht (therefore the test script has --cpuset-cpus=0-1, because builds.sr.ht allocates 2 CPUs), e.g. https://builds.sr.ht/~motiejus/job/526372. On my laptop it failed on the 15'th iteration, an iteration is ~90 seconds.
zig version: 0.9.0-dev.137+86ebd4b97. I know bazel in the loop is cumbersome, but I wasn't able to find an easy way to reproduce it without it.
Activity
[-]zig c++ hanging when compiling golang for macos[/-][+]zig c++ hanging when invoked in parallel[/+]motiejus commentedon Jun 18, 2021
Like discussed on IRC, I ran this overnight with a debug build of zig; it keeps running successfully, 266 iterations, and counting.
motiejus commentedon Jun 18, 2021
Apparently it needs a
x86_64-linux-muslbuild of zig to manifest. Bingo!Process 1
Process 2
Not sure how significant the warning is: I cannot run strace in the container, because it doesn't have ptrace permissions (I forgot to add
--privileged).Zig version
0.8.0-194-gb9e78593b, compiled from https://github.com/ziglang/zig-bootstrap with these changes:
rm -fr zig; cp -r ../zig .)build, so it emits aDebugbuild instead ofReleasebuild../build -j6 x86_64-linux-musl nativeLemonBoy commentedon Jun 18, 2021
Interesting, are you able to get the output of
lslocksinside the container?It's possible that
Cache.hithits some error and never closes the locked fileself.manifest_file, I'm not that familiar with the cache code to tell if that could be a problem.motiejus commentedon Jun 18, 2021
I have stopped the original and am trying to capture it again with
--privileged(so I can ptrace inside the container). If/when that happens again, I will re-report the backtraces and output oflslocks.mikdusan commentedon Jun 18, 2021
perhaps the conditional is our happy little bug?
zig/src/main.zig
Line 2378 in 1f29b75
motiejus commentedon Jun 18, 2021
A new set of borked processes, with ptrace privileges:
ps auxf
lslocks --output-all | grep zig
gdb process 1
gdb process 2
Not sure why the namespace warning is still there; I am running gdb inside the container.
This time I'm leaving these proceses to hang for further investigation. Let me know if there is something more I can try.
motiejus commentedon Jun 18, 2021
lsof 5598 ("process 1")
lsof 5605 ("process 2")
motiejus commentedon Jun 26, 2021
For the record, I still have the (quite powerful) VM hanging around with the stuck processes, if you need any more info.
Once there is a patch, I am happy to re-run the tests with the patch.
motiejus commentedon Jun 26, 2021
Putting the files as requested over IRC.
18c5d15d382debc4dd4079a12f75e0a1.txt
aeee3b9656aecd6cbe1df612eb49381b.txt
e6952993d9b2971d4f720b163e9e672c.txt
f322b09235a58afcec5f96313ce20bb3.txt
andrewrk commentedon Jun 26, 2021
File being compiled:
lib/libcxx/src/vector.cppFile being compiled:
lib/libcxx/src/variant.cppFile being compiled:
lib/libcxx/src/algorithm.cppFile being compiled:
lib/libcxx/src/any.cppandrewrk commentedon Jun 26, 2021
Process 1 locks vector.o & compiles it. Process 2 locks variant.o & compiles it. Process 1 tries to grab lock on variant.o and waits. Process 2 tries to grab lock on vector.o and waits. Deadlock.
I think the problem is clear, this seems to be a design flaw. I'm looking into making proper use of shared locks to solve the problem.
andrewrk commentedon Jun 26, 2021
I believe #7596 will solve this.
mikdusan commentedon Jun 26, 2021
yup the design flaw is verified; reviewing the
gdbandlsofoutputs we have this:e6952993d9b2971d4f720b163e9e672c.txtand proc-2 has it locked-exclusivef322b09235a58afcec5f96313ce20bb3.txtand proc-2 has it locked-exclusiveaeee3b9656aecd6cbe1df612eb49381b.txtand proc-1 has it locked-exclusive18c5d15d382debc4dd4079a12f75e0a1.txtand proc-1 has it locked-exclusiveedit: I also get confirmation on 2 deadlocks processes on my own system
stage2: Cache system handles shared objects
andrewrk commentedon Jun 28, 2021
I've implemented a fix to this here: #9258
However some work is needed to improve the std lib support for file locking on Windows before it can be merged.
stage2: Cache system handles shared objects