[PR #7179] [MERGED] fix(termux): keep the server up when sharp's WebAssembly runtime is missing #7609

Closed
opened 2026-10-24 04:05:50 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7179
Author: @SpicyMarinara
Created: 10/6/2026
Status: ✅ Merged
Merged: 10/6/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/termux-sharp-crash-7173


📝 Commits (1)

  • 6eb7bd4 fix(termux): keep the server up when sharp's WebAssembly runtime is missing

📊 Changes

7 files changed (+116 additions, -0 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/package.json (+1 -0)
📝 packages/server/src/index.ts (+8 -0)
📝 packages/server/src/services/image/sharp-runtime.ts (+20 -0)
📝 pnpm-lock.yaml (+3 -0)
➕ scripts/regressions/sharp-wasm-runtime-missing.regression.ts (+58 -0)
📝 scripts/regressions/termux-sharp.regression.mjs (+25 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7173

Why this change

On Android/Termux, a server updated in place from 2.4.6 to 2.5.0 shuts down as soon as a browser opens Engine. The chat list asks for background thumbnails (/api/backgrounds/file/Black.jpg?w=320), which loads sharp, and the server exits.

  • Missing package. 2.4.6 had @img/sharp-wasm32 as an optional dependency, so pnpm recorded @emnapi/runtime as "skipped" in node_modules/.modules.yaml. 2.5.0 made sharp-wasm32 a regular dependency. But the frozen in-place install that start-termux.sh runs (without --force) reaches @emnapi/runtime first through a parent that is always skipped: @huggingface/transformers -> sharp -> @img/sharp-freebsd-wasm32. So pnpm never removes the stale skipped entry, and @emnapi/runtime is never installed. Fresh installs work, and the in-app updater's --force repairs it.
  • The crash. sharp's WebAssembly loader finishes starting inside an async run() that nothing awaits. When require("@emnapi/runtime") fails there, getSharp() catches the import failure ("sharp.libvipsVersion is not a function"). A second, detached MODULE_NOT_FOUND rejection then reaches the server's unhandledRejection handler, which exits with code 1. Image processing is optional, so this failure should switch image processing off, not stop the server.

What changed

  • packages/server/package.json / pnpm-lock.yaml: @emnapi/runtime ^1.11.3 is now a direct server dependency, adding 3 importer lines to the lockfile. pnpm now reaches it from a parent that can be installed, so it gets installed on fresh installs, on in-place updates from 2.4.6 and on 2.5.0 trees that are already broken.
  • packages/server/src/services/image/sharp-runtime.ts: new isSharpLoaderRejection(reason). It matches only errors whose origin (requireStack[0], or else the first non-node:/wasm: stack frame) is a file in sharp's own @img/sharp-* loader packages.
  • packages/server/src/index.ts: the unhandledRejection handler first checks isSharpLoaderRejection. If it matches, the handler logs a warning ("image processing stays off until dependencies are reinstalled") and returns. Every other rejection is still fatal, and the rest of the handler is unchanged.
  • scripts/regressions/termux-sharp.regression.mjs: checks that the server importer depends directly on the same @emnapi/runtime version that the @img/sharp-wasm32 snapshot needs. Its probe also now fails on any detached rejection.
  • New scripts/regressions/sharp-wasm-runtime-missing.regression.ts: sets the platform to Android and uses module.registerHooks to make @emnapi/runtime unresolvable. It then checks that resolveThumbPath serves the original image, that exactly one detached MODULE_NOT_FOUND rejection is classified as non-fatal, that an unrelated Error stays fatal, and that index.ts checks the classifier first.
  • CHANGELOG.md: user-facing entry under [Unreleased].

Known limit, marked with a ponytail: comment: only sharp's own loader files are matched. The half-started WebAssembly heap (about 44 MB) stays in memory until restart. If needed, the upgrade path is to probe sharp in a child process before importing it in-process.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Agent-run checks (macOS arm64, Node 24.16.0, pnpm 10.34.5):

  • node scripts/run-regressions.mjs --filter sharp-wasm-runtime-missing: passes with the fix. With staging's index.ts and sharp-runtime.ts it fails (isSharpLoaderRejection is not a function). With the classifier but staging's handler it fails on the handler-order assertion.
  • node scripts/run-regressions.mjs --filter termux-sharp: passes with the fix. With staging's pnpm-lock.yaml and server package.json it fails: "the server must depend on @emnapi/runtime 1.11.3 directly so in-place updates install it".
  • termux-postmortem (still exactly two noteSessionExitKind("crash") calls) and robustness-shutdown-safety regressions pass.
  • pnpm check: result in the PR comments below.
  • Independent verification of the real upgrade path (pnpm 10.34.5, the same frozen --prefer-offline install start-termux.sh runs, with the CLI flags --os linux --cpu arm64 --cpu wasm32 standing in for Termux's supportedArchitectures; install scripts skipped):
    • v2.4.6, then git checkout --detach this branch in the same tree: @emnapi/runtime@1.11.3 leaves skipped and is linked. From packages/server, import("sharp") loads through WebAssembly (versions.emscripten is set), metadata() on a 1x1 PNG works, and there is no unhandled rejection.
    • v2.4.6, then v2.5.0: reproduces the bug. @emnapi/runtime stays under skipped, the import fails, and one detached MODULE_NOT_FOUND rejection comes from @img/sharp-wasm32/lib/sharp-wasm32-0.35.5.node.js. Updating that same tree to this branch repairs it without --force, and check-workspace-install reports no problems for android.
    • A fresh Termux-flag install and a normal macOS install of this branch both work. The frozen lockfile is accepted, and macOS keeps native sharp.
    • Built server with no native binding and @emnapi/runtime made unresolvable, then GET /api/backgrounds/file/Black.jpg?w=320: staging exits with code 1 ([process] Unhandled rejection). This branch returns the original image with 200, logs one [image] warning and keeps answering /api/health.

Manual verification notes

  • Manually verify on Android/Termux: start from a 2.4.6 install, update in place with start-termux.sh, and confirm .pnpm/@emnapi+runtime@1.11.3 exists in the repository root (.npmrc puts the virtual store in .pnpm/, not node_modules/.pnpm/) and @emnapi/runtime@1.11.3 is no longer under skipped in node_modules/.modules.yaml.
  • Manually verify on Android/Termux: open Engine in a browser on the updated install. Confirm the chat list loads its background thumbnails and the server keeps running.
  • Manually verify on an already-broken 2.5.0 Termux tree: run start-termux.sh on this branch and confirm the frozen install adds @emnapi/runtime without --force.
  • Manually verify that the server stays up if @emnapi/runtime is removed by hand: the log shows one "[image] sharp's image runtime failed to start" warning, and backgrounds are served at full size.
  • Manually verify on desktop (Windows/macOS/Linux) that background thumbnails are still generated by native sharp.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

Only CHANGELOG.md changes; no files under docs/ are touched.

UI evidence (if applicable)

Not applicable (server and dependency change only).

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability on Android and Termux when the image-processing library fails to load, including when a browser opens Engine. The server can now continue running instead of crashing; thumbnails may still be unavailable when image processing fails.
  • Documentation
    • Added an unreleased changelog entry covering the Android/Termux updates and image-processing failures.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/7179 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/6/2026 **Status:** ✅ Merged **Merged:** 10/6/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/termux-sharp-crash-7173` --- ### 📝 Commits (1) - [`6eb7bd4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6eb7bd4f506235ba6d48907f2cbe577a6a674e11) fix(termux): keep the server up when sharp's WebAssembly runtime is missing ### 📊 Changes **7 files changed** (+116 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/package.json` (+1 -0) 📝 `packages/server/src/index.ts` (+8 -0) 📝 `packages/server/src/services/image/sharp-runtime.ts` (+20 -0) 📝 `pnpm-lock.yaml` (+3 -0) ➕ `scripts/regressions/sharp-wasm-runtime-missing.regression.ts` (+58 -0) 📝 `scripts/regressions/termux-sharp.regression.mjs` (+25 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7173 ## Why this change On Android/Termux, a server updated in place from 2.4.6 to 2.5.0 shuts down as soon as a browser opens Engine. The chat list asks for background thumbnails (`/api/backgrounds/file/Black.jpg?w=320`), which loads sharp, and the server exits. - **Missing package.** 2.4.6 had `@img/sharp-wasm32` as an optional dependency, so pnpm recorded `@emnapi/runtime` as "skipped" in `node_modules/.modules.yaml`. 2.5.0 made sharp-wasm32 a regular dependency. But the frozen in-place install that `start-termux.sh` runs (without `--force`) reaches `@emnapi/runtime` first through a parent that is always skipped: `@huggingface/transformers -> sharp -> @img/sharp-freebsd-wasm32`. So pnpm never removes the stale skipped entry, and `@emnapi/runtime` is never installed. Fresh installs work, and the in-app updater's `--force` repairs it. - **The crash.** sharp's WebAssembly loader finishes starting inside an `async run()` that nothing awaits. When `require("@emnapi/runtime")` fails there, `getSharp()` catches the import failure ("sharp.libvipsVersion is not a function"). A second, detached `MODULE_NOT_FOUND` rejection then reaches the server's `unhandledRejection` handler, which exits with code 1. Image processing is optional, so this failure should switch image processing off, not stop the server. ## What changed - `packages/server/package.json` / `pnpm-lock.yaml`: `@emnapi/runtime ^1.11.3` is now a direct server dependency, adding 3 importer lines to the lockfile. pnpm now reaches it from a parent that can be installed, so it gets installed on fresh installs, on in-place updates from 2.4.6 and on 2.5.0 trees that are already broken. - `packages/server/src/services/image/sharp-runtime.ts`: new `isSharpLoaderRejection(reason)`. It matches only errors whose origin (`requireStack[0]`, or else the first non-`node:`/`wasm:` stack frame) is a file in sharp's own `@img/sharp-*` loader packages. - `packages/server/src/index.ts`: the `unhandledRejection` handler first checks `isSharpLoaderRejection`. If it matches, the handler logs a warning ("image processing stays off until dependencies are reinstalled") and returns. Every other rejection is still fatal, and the rest of the handler is unchanged. - `scripts/regressions/termux-sharp.regression.mjs`: checks that the server importer depends directly on the same `@emnapi/runtime` version that the `@img/sharp-wasm32` snapshot needs. Its probe also now fails on any detached rejection. - New `scripts/regressions/sharp-wasm-runtime-missing.regression.ts`: sets the platform to Android and uses `module.registerHooks` to make `@emnapi/runtime` unresolvable. It then checks that `resolveThumbPath` serves the original image, that exactly one detached `MODULE_NOT_FOUND` rejection is classified as non-fatal, that an unrelated `Error` stays fatal, and that `index.ts` checks the classifier first. - `CHANGELOG.md`: user-facing entry under `[Unreleased]`. Known limit, marked with a `ponytail:` comment: only sharp's own loader files are matched. The half-started WebAssembly heap (about 44 MB) stays in memory until restart. If needed, the upgrade path is to probe sharp in a child process before importing it in-process. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Agent-run checks (macOS arm64, Node 24.16.0, pnpm 10.34.5): - `node scripts/run-regressions.mjs --filter sharp-wasm-runtime-missing`: passes with the fix. With staging's `index.ts` and `sharp-runtime.ts` it fails (`isSharpLoaderRejection is not a function`). With the classifier but staging's handler it fails on the handler-order assertion. - `node scripts/run-regressions.mjs --filter termux-sharp`: passes with the fix. With staging's `pnpm-lock.yaml` and server `package.json` it fails: "the server must depend on @emnapi/runtime 1.11.3 directly so in-place updates install it". - `termux-postmortem` (still exactly two `noteSessionExitKind("crash")` calls) and `robustness-shutdown-safety` regressions pass. - `pnpm check`: result in the PR comments below. - Independent verification of the real upgrade path (pnpm 10.34.5, the same frozen `--prefer-offline` install `start-termux.sh` runs, with the CLI flags `--os linux --cpu arm64 --cpu wasm32` standing in for Termux's `supportedArchitectures`; install scripts skipped): - v2.4.6, then `git checkout --detach` this branch in the same tree: `@emnapi/runtime@1.11.3` leaves `skipped` and is linked. From `packages/server`, `import("sharp")` loads through WebAssembly (`versions.emscripten` is set), `metadata()` on a 1x1 PNG works, and there is no unhandled rejection. - v2.4.6, then v2.5.0: reproduces the bug. `@emnapi/runtime` stays under `skipped`, the import fails, and one detached `MODULE_NOT_FOUND` rejection comes from `@img/sharp-wasm32/lib/sharp-wasm32-0.35.5.node.js`. Updating that same tree to this branch repairs it without `--force`, and `check-workspace-install` reports no problems for android. - A fresh Termux-flag install and a normal macOS install of this branch both work. The frozen lockfile is accepted, and macOS keeps native sharp. - Built server with no native binding and `@emnapi/runtime` made unresolvable, then `GET /api/backgrounds/file/Black.jpg?w=320`: staging exits with code 1 (`[process] Unhandled rejection`). This branch returns the original image with 200, logs one `[image]` warning and keeps answering `/api/health`. ### Manual verification notes - Manually verify on Android/Termux: start from a 2.4.6 install, update in place with `start-termux.sh`, and confirm `.pnpm/@emnapi+runtime@1.11.3` exists in the repository root (`.npmrc` puts the virtual store in `.pnpm/`, not `node_modules/.pnpm/`) and `@emnapi/runtime@1.11.3` is no longer under `skipped` in `node_modules/.modules.yaml`. - Manually verify on Android/Termux: open Engine in a browser on the updated install. Confirm the chat list loads its background thumbnails and the server keeps running. - Manually verify on an already-broken 2.5.0 Termux tree: run `start-termux.sh` on this branch and confirm the frozen install adds `@emnapi/runtime` without `--force`. - Manually verify that the server stays up if `@emnapi/runtime` is removed by hand: the log shows one "[image] sharp's image runtime failed to start" warning, and backgrounds are served at full size. - Manually verify on desktop (Windows/macOS/Linux) that background thumbnails are still generated by native sharp. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) Only `CHANGELOG.md` changes; no files under `docs/` are touched. ## UI evidence (if applicable) Not applicable (server and dependency change only). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved stability on Android and Termux when the image-processing library fails to load, including when a browser opens Engine. The server can now continue running instead of crashing; thumbnails may still be unavailable when image processing fails. * **Documentation** * Added an unreleased changelog entry covering the Android/Termux updates and image-processing failures. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:05:50 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#7609
No description provided.