fix: handle dpkg lock contention during apt installs#110
Merged
Conversation
Greptile SummaryAdds dpkg lock contention handling across all three shell scripts that run
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[apt_get_with_retry called] --> B{attempt ≤ MAX_RETRIES?}
B -->|Yes| C[Run apt-get with DPkg::Lock::Timeout]
C --> D{Command succeeded?}
D -->|Yes| E[Return 0 - success]
D -->|No| F{More attempts left?}
F -->|Yes| G[Log retry message]
G --> H[Sleep RETRY_DELAY seconds]
H --> I[Increment attempt]
I --> B
F -->|No| J[Return 1 - failure]
B -->|No| J
Prompt To Fix All With AIThis is a comment left during a code review.
Path: setup_repo.sh
Line: 505-523
Comment:
**Inconsistent retry wrapper uses `eval`**
The `apt_get_with_retry` in this file takes a single string argument and uses `eval` to execute it (line 510), whereas `install.sh` uses the safer `"$@"` pattern for argument passing. This `eval`-based approach is fragile because shell metacharacters in `$apt_args` (like the `> /dev/null` redirects baked into the call sites at lines 533-534) are interpreted by `eval`, making the behavior harder to reason about and maintain.
I understand this is constrained by the existing `log_and_run` function which already uses `eval`. However, if this script is ever refactored, consider aligning all three implementations to use `"$@"` for safer argument handling.
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: 9578688 |
…tter version control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: handle dpkg lock contention during apt installs
Problem
During agent installation on freshly provisioned VMs,
apt-getoperations were failing immediately whenunattended-upgradeswas running in the background holding the dpkg frontend lock:This was a consistent pattern on Ubuntu 24.04 (noble) VMs — During VM provisioning triggering our agent installation, but
unattended-upgradesis still running in parallel, holding the dpkg lock. Since ourapt-getcalls had no lock-wait configured, they failed instantly and fell into a tight retry loop, exhausting all attempts beforeunattended-upgradeshad even finished.Root Cause
Three script paths were affected:
packaging/scripts/install.sh— the direct package installer (apt-get updatehad no lock timeout at all; onlyapt-get installhad a partialDPkg::Lock::Timeout=300)packaging/scripts/sc-console-agent-updater.sh— the auto-updater (clean,update, andinstall -fhad no lock timeout)setup_repo.sh— the bootstrap installer called during VM provisioning (allapt-getcalls had no lock timeout, including the "Installing prerequisites" step visible in the error logs above)Fix
Introduced a shared
apt_get_with_retry()wrapper in each script that:DPkg::Lock::Timeout=300to everyapt-getinvocation, making apt wait for the lock to be released rather than failing immediatelyAll three scripts now route every
apt-getoperation (update,install,clean,install -f,install --reinstall) through this wrapper.Why 300 seconds?
unattended-upgradeson Ubuntu typically holds the dpkg frontend lock for at most ~75 seconds during a normal run (package download + configure phase on a freshly booted VM). Setting the timeout to 300 seconds gives a comfortable 4× safety margin over that maximum, ensuring apt will always wait out the lock rather than fail. Combined with up to 5 retries (25 minutes total max window), this makes the installation resilient even in worst-case scenarios such as slow mirrors or large upgrade batches.All timeout/retry values are tunable via environment variables if needed:
SC_APT_LOCK_TIMEOUT300SC_APT_MAX_RETRIES5SC_APT_RETRY_DELAY15Files Changed
packaging/scripts/install.shpackaging/scripts/sc-console-agent-updater.shsetup_repo.shFixes #1264