feat(system_baseline): stop a hung apt-daily.service with a start timeout #337

Closed
opened 2026-11-23 20:50:33 +00:00 by Dragonish · 0 comments
Collaborator

Problem

apt-daily.service (the daily package-list refresh and download) is Type=oneshot, and systemd gives a oneshot unit no start timeout by default. If the apt-get update it runs hangs, nothing ever stops it, and it keeps the apt/dpkg lock. Every later apt command on the host then waits for the lock or fails with "Could not get lock", including the package update and the package installation this role does, and unattended-upgrades.

I saw it once: an apt-get update hung on two dead mirror connections (sockets stuck in CLOSE-WAIT) for 2 days and 8 hours, held the lock, and made a weekly update job and a build that needed apt fail until the process was killed by hand. A timeout would have stopped the unit and released the lock after an hour.

Out of scope: apt-daily-upgrade.service, which installs packages. Stopping it halfway through could leave dpkg in a half-configured state, so it is left alone on purpose.

Proposal

New input system_baseline_apt_daily_timeout (string, a systemd time span such as 1h or 90min; default empty, which leaves the host alone, so existing consumers see no change). When set, a drop-in /etc/systemd/system/apt-daily.service.d/10-timeout.conf with [Service] and TimeoutStartSec=<value>, written after creating the drop-in directory (ansible.builtin.copy does not create the directory of its destination, and a dry run does not show that it is missing), and a daemon-reload handler. When it is empty, the role does not manage the file. Where to run it in main.yml: a tagged task file (apt-daily-timeout) next to the other systemd drop-ins.

Why only apt-daily.service: it never runs dpkg, so the timeout can stop it safely; systemd stops the unit and everything in its cgroup, which includes the hung apt-get.

Acceptance criteria

  • With a value set (for example 1h), the drop-in exists with mode 0644 and the content above, and after the reload systemctl show -p TimeoutStartUSec apt-daily.service in verify.yml reports one hour (the scenario's systemd loads drop-ins after the handler's daemon-reload, as the existing tests of drop-ins do).
  • With the default (empty), no drop-in is written and no directory is created; a second converge changes nothing (Molecule idempotence).
  • A value that is not a systemd time span ("soon") is refused with a message that names the variable (validation in tasks/validate.yml, and the spec holds type: str).
  • argument_specs.yml and the README variable table list the input and its default; ansible-test sanity, ansible-lint and yamllint are clean; a minor_changes fragment is added.

Not verified

  • That a drop-in for apt-daily.service is read on every Ubuntu release you support: I run the same drop-in on Ubuntu 24.04 guests and on Debian 12 and 13 hosts; I have not run this role.
  • That apt-daily.service exists in the Molecule image (it may not be installed in a minimal image; a stub unit file would do, as the other tests of this role use).
  • Whether the default should stay empty (no change for anyone) or become a safe value such as 1h; I propose empty because a role default that adds a drop-in on every host is a behaviour change.

Questions for the maintainer

  • Empty by default, or 1h?
  • Is system_baseline the right role, or would you rather have this in a separate small role for apt timers?

Assisted-by: Claude Code

## Problem `apt-daily.service` (the daily package-list refresh and download) is `Type=oneshot`, and systemd gives a `oneshot` unit no start timeout by default. If the `apt-get update` it runs hangs, nothing ever stops it, and it keeps the apt/dpkg lock. Every later `apt` command on the host then waits for the lock or fails with "Could not get lock", including the package update and the package installation this role does, and unattended-upgrades. I saw it once: an `apt-get update` hung on two dead mirror connections (sockets stuck in `CLOSE-WAIT`) for 2 days and 8 hours, held the lock, and made a weekly update job and a build that needed `apt` fail until the process was killed by hand. A timeout would have stopped the unit and released the lock after an hour. Out of scope: `apt-daily-upgrade.service`, which installs packages. Stopping it halfway through could leave dpkg in a half-configured state, so it is left alone on purpose. ## Proposal New input `system_baseline_apt_daily_timeout` (string, a systemd time span such as `1h` or `90min`; default empty, which leaves the host alone, so existing consumers see no change). When set, a drop-in `/etc/systemd/system/apt-daily.service.d/10-timeout.conf` with `[Service]` and `TimeoutStartSec=<value>`, written after creating the drop-in directory (`ansible.builtin.copy` does not create the directory of its destination, and a dry run does not show that it is missing), and a `daemon-reload` handler. When it is empty, the role does not manage the file. Where to run it in `main.yml`: a tagged task file (`apt-daily-timeout`) next to the other systemd drop-ins. Why only `apt-daily.service`: it never runs dpkg, so the timeout can stop it safely; systemd stops the unit and everything in its cgroup, which includes the hung `apt-get`. ## Acceptance criteria - [ ] With a value set (for example `1h`), the drop-in exists with mode `0644` and the content above, and after the reload `systemctl show -p TimeoutStartUSec apt-daily.service` in `verify.yml` reports one hour (the scenario's systemd loads drop-ins after the handler's `daemon-reload`, as the existing tests of drop-ins do). - [ ] With the default (empty), no drop-in is written and no directory is created; a second converge changes nothing (Molecule idempotence). - [ ] A value that is not a systemd time span (`"soon"`) is refused with a message that names the variable (validation in `tasks/validate.yml`, and the spec holds `type: str`). - [ ] `argument_specs.yml` and the README variable table list the input and its default; `ansible-test sanity`, ansible-lint and yamllint are clean; a `minor_changes` fragment is added. ## Not verified - That a drop-in for `apt-daily.service` is read on every Ubuntu release you support: I run the same drop-in on Ubuntu 24.04 guests and on Debian 12 and 13 hosts; I have not run this role. - That `apt-daily.service` exists in the Molecule image (it may not be installed in a minimal image; a stub unit file would do, as the other tests of this role use). - Whether the default should stay empty (no change for anyone) or become a safe value such as `1h`; I propose empty because a role default that adds a drop-in on every host is a behaviour change. ## Questions for the maintainer - Empty by default, or `1h`? - Is `system_baseline` the right role, or would you rather have this in a separate small role for apt timers? Assisted-by: Claude Code
Dragonish added the Kind/Enhancement
Status
Ready for Human
labels 2026-11-23 20:50:33 +00:00
Dragonish added
Status
Ready for Agent
and removed
Status
Ready for Human
labels 2026-11-23 20:53:12 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#337