Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(images): never load sharp on a CPU it can't run on, or when there is nothing to resize (#1469) (#1481)

Tashfeenbeef0f82ee3b
diff --git a/server/src/__tests__/lib/image-normalize-old-cpu.test.ts b/server/src/__tests__/lib/image-normalize-old-cpu.test.ts
new file mode 100644
index 00000000..d4c3344c
--- /dev/null
+++ b/server/src/__tests__/lib/image-normalize-old-cpu.test.ts
@@ -0,0 +1,97 @@
+import { describe, it, expect, vi, afterAll } from 'vitest';
+import type { ChatMessage } from '@freellmapi/shared/types.js';
+
+// #1469: sharp's prebuilt linux-x64 libvips needs SSE4.2. On an older CPU
+// (Intel Atom D2700 in a Synology DS1512+) loading it is an illegal
+// instruction: the kernel kills the process with SIGILL, so the container
+// restarts on every chat. Two guards: a request with nothing to transcode never
+// loads sharp, and a linux-x64 CPU without sse4_2 never loads it at all.
+//
+// Own file: the loaded (or refused) sharp is cached in module state, and
+// vitest gives each file a fresh module registry.
+
+let sharpLoads = 0;
+vi.mock('sharp', () => {
+ sharpLoads += 1;
+ return { default: () => { throw new Error('sharp must not run in this test'); } };
+});
+
+const OLD_CPU_FLAGS = 'flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm movbe lahf_lm';
+const NEW_CPU_FLAGS = 'flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep sse sse2 ssse3 sse4_1 sse4_2 popcnt avx avx2';
+
+vi.mock('node:fs', async (importOriginal) => {
+ const actual = await importOriginal() as typeof import('node:fs');
+ return {
+ ...actual,
+ readFileSync: ((file: any, ...rest: any[]) => file === '/proc/cpuinfo'
+ ? `processor\t: 0\nmodel name\t: Intel(R) Atom(TM) CPU D2700 @ 2.13GHz\n${OLD_CPU_FLAGS}\n`
+ : (actual.readFileSync as any)(file, ...rest)) as typeof actual.readFileSync,
+ };
+});
+
+const realPlatform = Object.getOwnPropertyDescriptor(process, 'platform')!;
+const realArch = Object.getOwnPropertyDescriptor(process, 'arch')!;
+Object.defineProperty(process, 'platform', { value: 'linux' });
+Object.defineProperty(process, 'arch', { value: 'x64' });
+afterAll(() => {
+ Object.defineProperty(process, 'platform', realPlatform);
+ Object.defineProperty(process, 'arch', realArch);
+});
+
+const { normalizeMessageImages, cpuCanRunSharp } = await import('../../lib/image-normalize.js');
+
+const BIG_PNG = `data:image/png;base64,${'A'.repeat(2_000_000)}`;
+const SMALL_JPEG = `data:image/jpeg;base64,${'A'.repeat(1_000)}`;
+
+function turn(...content: unknown[]): ChatMessage[] {
+ return [{ role: 'user', content } as any];
+}
+
+describe('cpuCanRunSharp', () => {
+ it('rejects a linux-x64 CPU without sse4_2', () => {
+ expect(cpuCanRunSharp('linux', 'x64', () => OLD_CPU_FLAGS)).toBe(false);
+ });
+
+ it('accepts a linux-x64 CPU with sse4_2', () => {
+ expect(cpuCanRunSharp('linux', 'x64', () => NEW_CPU_FLAGS)).toBe(true);
+ });
+
+ it('does not match sse4_1 or a longer flag as sse4_2', () => {
+ expect(cpuCanRunSharp('linux', 'x64', () => 'flags : sse4_1 sse4_2a')).toBe(false);
+ });
+
+ it('assumes capable when it cannot tell, and off linux-x64', () => {
+ expect(cpuCanRunSharp('linux', 'x64', () => { throw new Error('EACCES'); })).toBe(true);
+ expect(cpuCanRunSharp('linux', 'x64', () => 'processor : 0')).toBe(true);
+ expect(cpuCanRunSharp('linux', 'arm64', () => OLD_CPU_FLAGS)).toBe(true);
+ expect(cpuCanRunSharp('darwin', 'x64', () => OLD_CPU_FLAGS)).toBe(true);
+ expect(cpuCanRunSharp('win32', 'x64', () => OLD_CPU_FLAGS)).toBe(true);
+ });
+});
+
+describe('inbound image normalization on a CPU without SSE4.2 (#1469)', () => {
+ it('never loads sharp for a text-only or small-image request', async () => {
+ const text = turn({ type: 'text', text: 'hi' });
+ expect((await normalizeMessageImages(text)).normalized).toBe(0);
+ await normalizeMessageImages([{ role: 'user', content: 'hi' } as any]);
+ await normalizeMessageImages(turn({ type: 'image_url', image_url: { url: SMALL_JPEG } }));
+ expect(sharpLoads).toBe(0);
+ });
+
+ it('passes a big image through untouched without loading sharp, and warns once', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ try {
+ const messages = turn({ type: 'text', text: 'what is this?' }, { type: 'image_url', image_url: { url: BIG_PNG } });
+ const summary = await normalizeMessageImages(messages);
+ await normalizeMessageImages(turn({ type: 'image_url', image_url: { url: BIG_PNG } }));
+
+ expect(sharpLoads).toBe(0);
+ expect(summary.normalized).toBe(0);
+ expect((messages[0].content as any[])[1].image_url.url).toBe(BIG_PNG);
+ expect(warn).toHaveBeenCalledTimes(1);
+ expect(String(warn.mock.calls[0][0])).toContain('SSE4.2');
+ } finally {
+ warn.mockRestore();
+ }
+ });
+});
diff --git a/server/src/lib/image-normalize.ts b/server/src/lib/image-normalize.ts
index 0bdefb38..bacc683a 100644
--- a/server/src/lib/image-normalize.ts
+++ b/server/src/lib/image-normalize.ts
@@ -1,3 +1,4 @@
+import { readFileSync } from 'node:fs';
import type { ChatMessage } from '@freellmapi/shared/types.js';
// Inbound image normalization. Vision payloads ride inline as base64 data
@@ -45,11 +46,48 @@ const DEFAULT_QUALITY = 90;
// prebuild, so load it on first use instead and degrade to pass-through when
// it isn't there: images ride through at original size, exactly as they did
// before this module existed.
+//
+// A mismatched binary is not always a catchable import error, though (#1469).
+// sharp's prebuilt linux-x64 libvips needs SSE4.2 (x86-64-v2); on an older CPU
+// (Intel Atom D2700 in a Synology DS1512+, pre-Nehalem Core 2) loading it hits
+// an illegal instruction and the kernel kills the whole process with SIGILL,
+// which no try/catch sees. So on linux-x64 we check the CPU flags first and
+// treat sharp as unavailable when SSE4.2 is missing.
type Sharp = typeof import('sharp').default;
let sharpModule: Sharp | null | undefined; // undefined = not yet attempted
+/**
+ * False when this machine is known to be unable to run sharp's prebuilt
+ * libvips: linux-x64 whose /proc/cpuinfo flags lack sse4_2. Anything we cannot
+ * read or recognise counts as capable, which keeps today's behaviour.
+ */
+export function cpuCanRunSharp(
+ platform: string = process.platform,
+ arch: string = process.arch,
+ readCpuinfo: () => string = () => readFileSync('/proc/cpuinfo', 'utf8'),
+): boolean {
+ if (platform !== 'linux' || arch !== 'x64') return true;
+ let cpuinfo: string;
+ try {
+ cpuinfo = readCpuinfo();
+ } catch {
+ return true;
+ }
+ const flags = cpuinfo.split('\n').find(line => /^flags\s*:/.test(line));
+ if (!flags) return true;
+ return /\bsse4_2\b/.test(flags);
+}
+
async function loadSharp(): Promise<Sharp | null> {
if (sharpModule !== undefined) return sharpModule;
+ if (!cpuCanRunSharp()) {
+ sharpModule = null;
+ console.warn(
+ '[ImageNormalize] this CPU lacks SSE4.2, which the bundled image library (sharp) needs; ' +
+ 'inbound images will not be resized (set IMAGE_NORMALIZE=off to silence this)',
+ );
+ return sharpModule;
+ }
try {
sharpModule = (await import('sharp')).default;
} catch (err: any) {
@@ -130,9 +168,17 @@ function imageSlots(message: ChatMessage): ImageSlot[] {
return slots;
}
-// Transcode one data URL; returns null when the image should ride along
-// unchanged (under threshold, already compact, http URL, or any failure).
-async function normalizeDataUrl(sharp: Sharp, url: string, opts: ResolvedOptions): Promise<{ url: string; before: number; after: number } | null> {
+interface Candidate {
+ mime: string;
+ payload: string;
+ rawBytes: number;
+ alreadyCompact: boolean;
+}
+
+// The sharp-free half of the decision: is this data URL an image worth
+// decoding at all? Kept apart so a request with nothing to transcode never
+// loads sharp (#1469).
+function candidateOf(url: string, opts: ResolvedOptions): Candidate | null {
const match = DATA_URL_RE.exec(url);
if (!match) return null;
const mime = (match[1] ?? '').toLowerCase();
@@ -148,6 +194,13 @@ async function normalizeDataUrl(sharp: Sharp, url: string, opts: ResolvedOptions
// images under the threshold are rare enough (pixels ≈ bytes) that this
// trade stays worth it.
if (rawBytes < opts.thresholdBytes && alreadyCompact) return null;
+ return { mime, payload, rawBytes, alreadyCompact };
+}
+
+// Transcode one candidate; returns null when the image should ride along
+// unchanged (already compact or any failure).
+async function normalizeDataUrl(sharp: Sharp, candidate: Candidate, opts: ResolvedOptions): Promise<{ url: string; before: number; after: number } | null> {
+ const { payload, rawBytes, alreadyCompact } = candidate;
let buffer: Buffer;
try {
@@ -234,22 +287,30 @@ export async function normalizeMessageImages(
const summary: ImageNormalizeSummary = { messages, normalized: 0, bytesBefore: 0, bytesAfter: 0 };
if (!opts.enabled) return summary;
- // No sharp (missing prebuild, unsupported platform): pass everything through
- // untouched rather than failing the request.
- const sharp = await loadSharp();
- if (!sharp) return summary;
-
+ const work: { slot: ImageSlot; candidate: Candidate }[] = [];
for (const message of messages) {
for (const slot of imageSlots(message)) {
if (!slot.url.startsWith('data:')) continue;
- const result = await normalizeDataUrl(sharp, slot.url, opts);
- if (!result) continue;
- slot.set(result.url);
- summary.normalized += 1;
- summary.bytesBefore += result.before;
- summary.bytesAfter += result.after;
+ const candidate = candidateOf(slot.url, opts);
+ if (candidate) work.push({ slot, candidate });
}
}
+ // Text-only and small-image requests never load the native module (#1469).
+ if (work.length === 0) return summary;
+
+ // No sharp (missing prebuild, unsupported platform or CPU): pass everything
+ // through untouched rather than failing the request.
+ const sharp = await loadSharp();
+ if (!sharp) return summary;
+
+ for (const { slot, candidate } of work) {
+ const result = await normalizeDataUrl(sharp, candidate, opts);
+ if (!result) continue;
+ slot.set(result.url);
+ summary.normalized += 1;
+ summary.bytesBefore += result.before;
+ summary.bytesAfter += result.after;
+ }
if (summary.normalized > 0) {
const mb = (n: number) => (n / (1024 * 1024)).toFixed(1);