** Description changed: + [SRU] 2.79: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/2175701 [Impact] - * + A snap service's OOM score can change silently after an unrelated snap + operation, making OOM kill priority unpredictable. [Test Plan] - * + Complete the steps first with snapd <2.79 followed by with snapd 2.79 debs + 1. Build and install a minimal service snap: + mkdir -p svc/meta svc/bin + cat > svc/meta/snap.yaml <<EOF + name: test-vitality + version: "1" + base: core24 + apps: + svc: + command: bin/run + daemon: simple + EOF + printf '#!/bin/sh\nexec sleep infinity\n' > svc/bin/run; chmod +x svc/bin/run + snap pack svc && sudo snap install --dangerous test-vitality_1_all.snap + 2. Set the hint, add a duplicate, then re-install the snap to regenerate its service unit: + U=/etc/systemd/system/snap.test-vitality.svc.service + sudo snap set system resilience.vitality-hint=other,test-vitality; grep OOM $U + sudo snap set system resilience.vitality-hint=other,test-vitality,xyz,test-vitality; grep OOM $U + sudo snap install --dangerous test-vitality_1_all.snap; grep OOM $U + 3. Verify that before the fix the grepped values vary whereas after they do not. [Where problems could occur] - * - - [Other Info] - - * + The change only affects lists with duplicate entries. A bug in this + handler could leave wrong vitality scores in units. ---original--- # Duplicate entries in `resilience.vitality-hint` can produce inconsistent vitality ranks I stumbled on this while digging into charm code that sets `resilience.vitality-hint`, like [this](https://github.com/canonical/postgresql- operator/pull/1993/changes#diff-b1d97858c05db2ed9cb5e8c80b71dbc5fdfba89c0aa2a1fbd21cf333c5046a9bR4). The writeup below was generated from a Copilot investigation of the snapd codebase, read and edited by me. ## Summary The `resilience.vitality-hint` system configuration option accepts a comma-separated list of snap instance names. The position of a snap in the list determines its vitality rank. For example: ```text bar,foo,baz ``` assigns: - `bar`: rank 1 - `foo`: rank 2 - `baz`: rank 3 The rank is converted into a systemd `OOMScoreAdjust` value: ```text OOMScoreAdjust = -900 + vitality-rank ``` Therefore, a higher rank produces a less-negative score and makes the snap's services more likely to be selected by the OOM killer. Duplicate entries are currently accepted, but the two code paths that interpret the list use different duplicate semantics: - The configuration-change handler uses **last occurrence wins**. - Service regeneration during snap installation, refresh, rollback, or other lifecycle operations uses **first occurrence wins**. This means the OOM score written to a service unit can change unexpectedly after a later snap operation. ## Example failure scenario Assume snap `foo` is already installed and has a service. 1. `foo` is configured with rank 2: - ```text - resilience.vitality-hint=bar,foo - ``` - - Its service unit receives: - - ```text - OOMScoreAdjust=-898 - ``` + ```text + resilience.vitality-hint=bar,foo + ``` + + Its service unit receives: + + ```text + OOMScoreAdjust=-898 + ``` 2. Someone later appends `foo` again, producing: - ```text - resilience.vitality-hint=bar,foo,baz,foo - ``` + ```text + resilience.vitality-hint=bar,foo,baz,foo + ``` 3. The configuration-change handler builds a map of ranks. Because map entries are overwritten, the second occurrence of `foo` wins and `foo` is assigned rank 4: - ```text - OOMScoreAdjust=-896 - ``` + ```text + OOMScoreAdjust=-896 + ``` 4. At a later point, a snap refresh causes the service unit to be regenerated. The service-option lookup scans the list from the beginning and stops at the first matching entry. It therefore assigns `foo` rank 2 again: - ```text - OOMScoreAdjust=-898 - ``` + ```text + OOMScoreAdjust=-898 + ``` The service unit can consequently revert from the rank written during configuration processing to a different rank during an unrelated snap lifecycle operation. The configuration handler rewrites the unit file, but does not necessarily restart an already-running service. The new score takes effect when the service is started or restarted. ## Root cause The configuration-change handler constructs the new rank map as follows: ```go for i, instanceName := range strings.Split(newVitalityStr, ",") { - newVitalityMap[instanceName] = i + 1 + newVitalityMap[instanceName] = i + 1 } ``` Repeated entries overwrite earlier entries, so this implements last- occurrence-wins behavior. The service-option lookup uses a forward scan and stops at the first match: ```go for i, s := range strings.Split(vitalityStr, ",") { - if s == snapInfo.InstanceName().String() { - opts.VitalityRank = i + 1 - break - } + if s == snapInfo.InstanceName().String() { + opts.VitalityRank = i + 1 + break + } } ``` This implements first-occurrence-wins behavior. ## Suggested fix: consistently use first occurrence The preferred fix is to make the configuration-change handler use first- occurrence-wins behavior, leaving the existing service-option lookup unchanged: ```diff diff --git a/overlord/configstate/configcore/vitality.go b/overlord/configstate/configcore/vitality.go --- a/overlord/configstate/configcore/vitality.go +++ b/overlord/configstate/configcore/vitality.go @@ - // build rank of the new entries - for i, instanceName := range strings.Split(newVitalityStr, ",") { + // build rank of the new entries + for i, instanceName := range strings.Split(newVitalityStr, ",") { - newVitalityMap[instanceName] = i + 1 + if _, alreadyRanked := newVitalityMap[instanceName]; !alreadyRanked { + newVitalityMap[instanceName] = i + 1 + } - } + } ``` With this change, both paths interpret: ```text bar,foo,baz,foo ``` as: - `bar`: rank 1 - `foo`: rank 2 - `baz`: rank 3 The existing `SnapServiceOptions` implementation already has first- occurrence-wins behavior because it stops at the first match. ### Why prefer first occurrence? First-wins is preferable for compatibility because the existing service- regeneration path already uses it. That path is used during normal snap lifecycle operations, including: - snap installation, - snap refresh, - snap rollback, - snap relinking and recovery, - service regeneration after seeding, - some quota-related service updates. Changing that path to last-wins would change the behavior of existing duplicate-containing configurations whenever service units are regenerated. In contrast, changing the configuration-change handler to first-wins makes it converge with the behavior users already receive during those lifecycle operations. Existing duplicate configurations may still have service units written using the old last-occurrence rank. A subsequent configuration transaction involving the vitality hint may rewrite those units using the now-consistent first-occurrence rank. This is a one-time convergence effect and is preferable to continuing to produce different results depending on the code path. The choice between first-wins and last-wins is not meaningful for valid, duplicate-free configurations. The important requirement is that both paths agree. First-wins is preferred because it retains the behavior of the already-established lifecycle/regeneration path and changes the smaller surface area. ## Relevant implementation locations - Configuration validation and update handling: - `overlord/configstate/configcore/vitality.go` + `overlord/configstate/configcore/vitality.go` - Service-option lookup: - `overlord/servicestate/servicestate.go` + `overlord/servicestate/servicestate.go` - Service-unit generation and OOM score conversion: - `wrappers/internal/service_unit_gen.go` + `wrappers/internal/service_unit_gen.go` - Service regeneration: - `wrappers/services.go` + `wrappers/services.go`
-- You received this bug notification because you are a member of Ubuntu Bugs, which is subscribed to Ubuntu. https://bugs.launchpad.net/bugs/2169519 Title: Duplicate entries in `resilience.vitality-hint` can produce inconsistent vitality ranks To manage notifications about this bug go to: https://bugs.launchpad.net/snapd/+bug/2169519/+subscriptions -- ubuntu-bugs mailing list [email protected] https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs
