Abort (SIGABRT) when stderr is a closed pipe: uncaught fmt::system_error in ccache_main's catch
Summary
When ccache's stderr is a pipe whose reader has gone away, a failing compile can make ccache abort with SIGABRT and dump core. The abort comes from the error print in the catch block of ccache_main (src/ccache/ccache.cpp, lines 3306-3309 in v4.14.2). It writes to stderr, the write throws fmt::system_error (EPIPE), and nothing catches that second throw, so std::terminate runs.
Master also still has the unguarded print, checked on the day of filing. I found no existing issue or PR for this (searched "Broken pipe", "Failed to write to fd", "std::terminate", "SIGPIPE"). #931 is related but different: a closed Redis socket, a separate write path.
Environment
- ccache v4.14.2 (the release tarball, built from source; the prebuilt linux-x86_64 glibc binary behaves the same)
- Ubuntu 24.04, g++ 13.3.0, ccache used as a compiler wrapper by a build tool that disconnected mid-build
Reproduction
bad.cpp:
int main() { return undefined_symbol; }
export CCACHE_CONFIGPATH=/dev/null
trap '' PIPE # SIGPIPE ignored; children inherit it
ccache g++ -c bad.cpp -o /dev/null 2>&1 | true # the reader exits; ccache's stderr is a dead pipe
echo "exit=${PIPESTATUS[0]}"
trap '' PIPE sets SIGPIPE to SIG_IGN, the disposition a parent that ignores SIGPIPE hands to its children.
Expected
ccache reports the compile failure and exits with the compiler's status, or stops quietly. It does not abort.
Actual
Unpatched v4.14.2, SIGPIPE ignored: exit=134, SIGABRT, and a core dump. The text ccache: error: Failed to write to fd 2: Broken pipe is the exception that was thrown, not output that reached anyone.
Cause
core::send_to_console (src/ccache/core/common.cpp, lines 204-212 in v4.14.2) throws core::Error ("Failed to write to fd 2: ...") when the write to fd 2 fails. ccache_main's catch then prints it with PRINT(stderr, ...), which writes to the same dead fd, throws fmt::system_error, and the throw escapes the catch block.
Results
| binary | SIGPIPE at start | exit | core dump |
|---|---|---|---|
| v4.14.2 upstream | default | 141 (killed by SIGPIPE) | no |
| v4.14.2 upstream | ignored (trap '' PIPE) |
134 (SIGABRT) | yes |
| v4.14.2 + patch below | default | 141 | no |
| v4.14.2 + patch below | ignored | 1 (compiler status) | no |
| control: patched, live stderr | n/a | 1 (normal gcc diagnostic) | no |
With SIGPIPE at its default disposition, the compiler-output replay in to_cache is killed by SIGPIPE (exit 141). That is a clean death with no abort, and the patch does not change it. Whether ccache should handle that case too is a separate question; I have not changed it.
Patch
The patch guards the print so that a failed print can no longer escape the catch block:
--- a/src/ccache/ccache.cpp
+++ b/src/ccache/ccache.cpp
@@ -3304,7 +3304,12 @@
return cache_compilation(argc, argv);
} catch (const core::ErrorBase& e) {
- PRINT(stderr, "ccache: error: {}\n", e.what());
+ try {
+ PRINT(stderr, "ccache: error: {}\n", e.what());
+ } catch (const std::exception&) {
+ // stderr is gone (the build's client closed the pipe): nothing is left to report to, and an
+ // exception escaping this catch block would std::terminate.
+ }
return EXIT_FAILURE;
}
}
It applies to v4.14.2 with patch -p1, and the patched tree builds on Ubuntu 24.04 (g++ 13.3, cmake Release, ENABLE_TESTING=OFF); I have not run ccache's test suite on it. I have only filed this issue; I have not opened a PR.