-
Notifications
You must be signed in to change notification settings - Fork 0
fix(anvil): survive OOM-truncated state + bound memory growth #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #!/usr/bin/env bash | ||
| # MANAGED BY the anvil role — do not edit by hand. | ||
| # | ||
| # anvil's --state flag refuses to start when the snapshot file exists but is empty | ||
| # or truncated (it aborts with "EOF while parsing a value at line 1 column 0", | ||
| # exit 2). An OOM-kill mid-snapshot (--state-interval writes) leaves exactly such a | ||
| # file, which then wedges the service into a permanent restart loop — the incident | ||
| # this guard exists to prevent. | ||
| # | ||
| # Run as ExecStartPre: if the state file is empty or doesn't end in a JSON close | ||
| # token, move it aside so anvil starts from a fresh chain instead of crash-looping. | ||
| # A devnet can afford to lose state; it cannot afford to be down. | ||
| # | ||
| # The checks are deliberately O(1) in memory. This runs INSIDE the unit's cgroup, | ||
| # so it is subject to the same MemoryMax as anvil — parsing a potentially large | ||
| # snapshot with jq could itself be OOM-killed and re-wedge startup, the very thing | ||
| # we are guarding against. A size test plus a trailing-token test catches the empty- | ||
| # and truncated-write failure modes without loading the file into memory. | ||
| set -euo pipefail | ||
|
|
||
| state="{{ anvil_state_dir }}/state.json" | ||
|
|
||
| # No snapshot yet (first boot): anvil will create one. Nothing to guard. | ||
| [ -e "$state" ] || exit 0 | ||
|
|
||
| bad=0 | ||
| if [ ! -s "$state" ]; then | ||
| bad=1 # zero bytes — the OOM-truncation failure mode | ||
| else | ||
| # A complete anvil snapshot is a JSON document, so its last non-whitespace byte | ||
| # is a close token. A write truncated mid-content won't end this way. | ||
| last="$(tail -c 256 "$state" | tr -d '[:space:]' | tail -c 1)" | ||
| if [ "$last" != "}" ] && [ "$last" != "]" ]; then | ||
| bad=1 # non-empty but not a complete JSON document (partial write) | ||
| fi | ||
| fi | ||
|
|
||
| if [ "$bad" -eq 1 ]; then | ||
| # Static name (not timestamped): a repeated crash/OOM loop overwrites one file | ||
| # instead of filling the disk with snapshots. The latest sample suffices for | ||
| # forensics, and the disk headroom matters more on this constrained host. | ||
| corrupt="${state}.corrupt" | ||
| echo "anvil-state-guard: '${state}' is empty or truncated; moving to '${corrupt}' so anvil can start from a fresh chain" >&2 | ||
| mv -f "$state" "$corrupt" | ||
| fi | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.