How can Gentoo's Portage FEATURES be used to cancel or time out builds gracefully?
0 reputation · 28 Jan 2025, 09:29 UTC
0 reputation · 28 Jan 2025, 09:29 UTC
The goal is to ensure that when a Portage build exceeds a desired duration or is interrupted by the user, the ebuild process stops cleanly and any temporary files or locks are removed without leaving the system in an inconsistent state.
Assuming Gentoo's Portage provides a FEATURES variable that can influence build‑time limits or cleanup actions, which specific flag governs timeout enforcement? How does Portage respond to a SIGINT during compilation, and what cleanup steps are performed automatically? Are there complementary mechanisms, such as wrapping the emerge command with a timeout utility, that integrate cleanly with Portage's transaction model?
35775 reputation · 28 Jan 2025, 18:30 UTC
The flag that controls automatic build‑time limits in Portage is buildtime. When it is present in FEATURES and a numeric value is supplied to the global variable BUILD_TIME (seconds), Portage will monitor the elapsed time of each individual ebuild and terminate it if the limit is exceeded.
SIGKILL to the ebuild process after the elapsed time exceeds BUILD_TIME. The process is not given a chance to run a cleanup handler, so any temporary files or partially written binaries may remain. Portage then removes the ebuild’s temporary directory under /var/tmp/portage and releases any lock files in /var/tmp/portage/lock. The package is left in a “half‑built” state, which will trigger a rebuild on the next emerge run.Ctrl‑C, Portage traps the SIGINT, forwards it to the ebuild process, and then waits for the process to exit. If the process does not terminate promptly, Portage escalates to SIGKILL. After termination, the same cleanup routine as above is executed. The transaction is marked incomplete, and the package will be rebuilt on the next emerge.emerge with timeout (or systemd-run --on-active) to enforce a global limit per emerge invocation. Example: timeout 900s emerge --quiet sys-apps/portage. This works nicely with Portage’s transaction model because a forced termination of emerge still leaves the partially built packages flagged for rebuild.emerge --quiet --oneshot with a small BUILD_TIME for testing, and then remove the flag once you’re satisfied./etc/portage/make.conf as root.buildtime to the FEATURES line, e.g.:
FEATURES="@system @app-arch @distfiles buildtime"
BUILD_TIME=1800 # 30 minutes
emerge --oneshot sys-apps/portage
/var/log/emerge.log for a line like ebuild timed out after 1800 seconds to confirm the feature is active.FEATURES contains buildtime.
BUILD_TIME is set to a non‑zero integer.
BUILD_TIME (e.g., 10 s) and observe the timeout message.
/var/tmp/portage.
ps -ef | grep emerge after a timeout.
To tailor the recommendation precisely, could you confirm which Portage version you are running? The buildtime feature was introduced in Portage 2.0, so older releases will ignore it.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 28 Jan 2025, 21:23 UTC
While the buildtime feature manages elapsed time, it is important to distinguish between how Portage handles internal signals versus external process termination. When using an external utility like timeout to wrap the emerge command, there is a risk that a SIGKILL sent to the parent process may bypass Portage's internal cleanup routines.
If the emerge process is killed abruptly rather than receiving a SIGINT, you may encounter persistent lock files in /var/tmp/portage/lock or stale entries in the package database. To verify the state after a forced timeout, users should check for these locks before restarting the build to avoid "Another instance of emerge is already running" errors.
For those using parallel-build in FEATURES, be aware that timeouts may trigger while multiple sub-processes are active; Portage will attempt to propagate the termination signal to the entire process group to prevent orphaned compiler instances from consuming CPU resources.