Skip to content

fix(server): avoid renameat2 SIGSYS on aarch64 Android (Termux) - #353

Merged
dubadub merged 1 commit into
mainfrom
fix/aarch64-rename-sigsys-349
Jun 9, 2026
Merged

dubadub merged 1 commit into
mainfrom
fix/aarch64-rename-sigsys-349

Conversation

@dubadub

@dubadub dubadub commented Jun 9, 2026

Copy link
Copy Markdown
Member

Problem

Fixes #349. cook server crashes with "Bad system call" when saving/editing a recipe on aarch64 Android (reported on Termux on a Samsung S8). Reading recipes works fine — only editing fails.

Root cause

"Bad system call" is SIGSYS, raised by Android's seccomp filter when a process makes a blocked syscall (Samsung devices ship especially strict policies).

The save path (recipe_save) writes a temp file and then calls tokio::fs::rename(). On aarch64 the kernel exposes no legacy rename/renameat syscall, so libc implements rename() via renameat2 — which Android/Samsung seccomp blocks → SIGSYS → the server process is killed.

  • Reading never renames, so it worked.
  • ARM 32-bit isn't affected (it has the permitted legacy rename).
  • SIGSYS is a fatal signal, not an Err, so .map_err() never runs — the syscall must be avoided, not handled.

Fix

New server::fs_atomic module with rename_replace / rename_replace_async:

  • Non-Android: atomic rename as before (keeps crash-safety on servers/desktops).
  • Android: copy + remove_file, which use only permitted syscalls (openat/read/write/unlinkat). Non-atomic, but the only option when renameat2 is forbidden.

All three fs::rename sites are routed through it — recipe save, shopping-list compaction, and legacy shopping-list migration — since they share the same root cause and would all crash on the affected device.

Verification

  • cargo fmt --check clean
  • cargo clippy clean on both host and aarch64-linux-android targets
  • cargo check --target aarch64-linux-android compiles the Android branch
  • cargo test — all pass, incl. 3 new unit tests for the helper

Limitation

The unit tests cover the helper's contract on the host but cannot reproduce the actual SIGSYS (that needs aarch64 Android seccomp, unavailable in CI). The fix rests on the root-cause analysis plus confirming the Android branch compiles and avoids renameat2 by construction. Confirmation from a user on an affected device before release would be ideal.

`cook server` crashed with "Bad system call" when saving recipes on
aarch64 Android (e.g. Termux on a Samsung S8). Reading worked; only
editing failed.

Root cause: the save path writes a temp file then calls
`tokio::fs::rename()`. On aarch64 the kernel exposes no legacy
`rename`/`renameat` syscall, so libc implements `rename()` via
`renameat2`, which Android's (and especially Samsung's) seccomp filter
blocks with SIGSYS. SIGSYS is a fatal signal, not an `Err`, so the
failure can't be caught — the syscall must be avoided entirely.

Add `server::fs_atomic::rename_replace{,_async}`: an atomic rename on
capable platforms, falling back to copy + remove on Android (uses only
permitted syscalls; non-atomic but the only option when renameat2 is
forbidden). Route all three rename sites through it — recipe save,
shopping-list compaction, and legacy migration share the root cause.

Fixes #349
@claude

claude Bot commented Jun 9, 2026

Copy link
Copy Markdown

Code Review: fix(server) — avoid renameat2 SIGSYS on aarch64 Android

Overview

This PR fixes a crash on aarch64 Android (Termux) where cook server receives SIGSYS when editing recipes. The root cause analysis is accurate: aarch64 Linux has no legacy rename/renameat syscalls, so libc routes rename() through renameat2, which Android seccomp (especially Samsung vendor policies) blocks — killing the process with a fatal signal before any Rust error handler can run. The fix introduces a new server::fs_atomic module and routes all three rename call sites through it.


Strengths

  • Root cause analysis is thorough and well-documented in both the PR description and the module-level doc comment.
  • Platform isolation via cfg(target_os = android) is the correct mechanism and leaves non-Android codepaths unchanged.
  • spawn_blocking correctly offloads the blocking I/O from the async runtime.
  • All three affected rename sites are covered, not just the reported one.
  • Test coverage is appropriate: three unit tests exercise the contract on the host platform.
  • Non-atomicity on Android is explicitly acknowledged in both the PR description and the updated comment in shopping_list_store.rs.

Issues and Suggestions

Minor: Use super::fs_atomic inside the server module

Both handlers/recipes.rs and shopping_list_store.rs are already inside crate::server, so crate::server::fs_atomic::rename_replace(...) can be shortened to super::fs_atomic::rename_replace(...), or simply add a use import at the top of each file. The full crate:: path works but is unusual for in-module references.

Minor: Inconsistent parameter style between sync and async variants

rename_replace takes &Path (ergonomic), while rename_replace_async takes Utf8PathBuf by value (needed for spawn_blocking's 'static bound). This forces call sites that already own a Utf8PathBuf — like recipes.rs — to .clone() unnecessarily. The async variant could instead take &Utf8Path and clone internally, pushing the allocation inside the function rather than at every call site. This is a nit — the current approach is entirely correct.

Worth noting: Partial-success edge case in the Android copy+remove path

In rename_replace on Android, if copy succeeds but remove_file fails, the function returns Err even though the destination was already written correctly. The outer error handlers call remove_file(&tmp_path) on failure — which is still correct since the source file is still present — but an Err is propagated to the caller despite the write having fully succeeded from a data-safety perspective. This is extremely unlikely in practice and inherent to the non-atomic workaround, but a brief comment would help future maintainers understand the subtle behavior.

MSRV: io::Error::other requires Rust 1.74

io::Error::other() was stabilized in November 2023. If the project pins a lower MSRV via rust-version in Cargo.toml, this will fail on older toolchains. Worth a quick check — if no MSRV is pinned, this is a non-issue.

Test gap (acknowledged, unavoidable)

The tests cover the contract on the host but cannot reproduce SIGSYS. One cheap addition: a test that verifies rename_replace propagates Err when from does not exist, guarding the error-path code.


Summary

This is a well-researched, correctly scoped fix with clear documentation and appropriate tests given the platform constraints. All items above are minor nits or edge-case observations — none are blockers. The PR is ready to merge, ideally with confirmation from a user on an affected aarch64 Android device as noted in the description.

Verdict: Approve with minor suggestions.

@claude

claude Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review — fix(server): avoid renameat2 SIGSYS on aarch64 Android (Termux)

Root-cause analysis and fix logic are sound. The module-level docs clearly explain the SIGSYS mechanism, and the three call-site changes are consistent. Tests cover the happy-path contract well for host builds.

Critical concern: cfg(target_os = "android") does not activate for the release binaries

The release pipeline (.github/workflows/release.yaml) builds the aarch64 Linux binary as aarch64-unknown-linux-musl — not aarch64-linux-android. #[cfg(target_os = "android")] is only true when the target triple contains -android, so the workaround compiles out of every prebuilt binary that Termux users would actually download. The affected users will still hit SIGSYS after this ships.

Possible fixes:

  • Add aarch64-linux-android as a release target (most correct; requires cross-compilation toolchain changes).
  • Widen the cfg guard to also cover Linux/aarch64, e.g. #[cfg(any(target_os = "android", all(target_os = "linux", target_arch = "aarch64")))]. This is conservative — the fallback is just copy+remove instead of atomic rename, which is safe on Linux too.
  • Runtime detection via prctl(PR_GET_SECCOMP) or a test rename — more complex, probably overkill.

The second option (widen the cfg) is the lowest-friction path that actually fixes Termux users on the current release binary.

Other notes

  • API shape of rename_replace_async — The sync variant takes &Path (ergonomic, generic), but the async variant takes Utf8PathBuf by value, forcing callers to .clone() paths they still need afterwards (see recipes.rs). Taking Utf8PathBuf (or impl Into<Utf8PathBuf>) by value in the async variant is fine for spawn_blocking, but accepting references and cloning internally would avoid the caller-side .clone() noise.

  • Comment in recipes.rs — The inline comment explaining the Android issue is a copy of what the module doc already says; one place is enough. The call-site comment can be trimmed to just the issue link.

  • Atomicity loss on Android — The existing comment in shopping_list_store.rs already notes "On Android rename_replace always copies + removes to dodge the seccomp-blocked renameat2 syscall." That's good. Worth also noting in the module-level doc that the Android path is not crash-safe (power loss between copy and remove could leave the old file in place).

  • spawn_blocking JoinError mapping — io::Error::other is the right call for mapping a JoinError; no issue there.

  • Tests — Three tests are a solid baseline. The async test uses #[tokio::test] correctly.

The fix is logically correct and will help users who build from source with an Android target triple. The main ask before merging is resolving the cfg scope so prebuilt release binaries actually benefit.

@dubadub
dubadub merged commit 46950ab into main Jun 9, 2026
6 checks passed
@dubadub
dubadub deleted the fix/aarch64-rename-sigsys-349 branch June 9, 2026 09:21
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
cookcli 0.31.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## [0.31.0](cooklang/cookcli@v0.30.0...v0.31.0) (2026-06-09)


### Features

* **build:** generate sitemap.xml with `cook build web --sitemap` ([#351](cooklang/cookcli#351)) ([6db127a](cooklang/cookcli@6db127a))


### Bug Fixes

* **server:** avoid renameat2 SIGSYS on aarch64 Android (Termux) ([#353](cooklang/cookcli#353)) ([46950ab](cooklang/cookcli@46950ab)), closes [#349](https://github.com/cooklang/cookcli/issues/349)</pre>
  <p>View the full release notes at <a href="https://github.com/cooklang/cookcli/releases/tag/v0.31.0">https://github.com/cooklang/cookcli/releases/tag/v0.31.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!11090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cook server aarch64 bad system call

1 participant