Skip to content

fix(par2): skip PAR2 creation for files smaller than SIMD alignment#213

Merged
javi11 merged 1 commit intomainfrom
claude/dazzling-hodgkin-c9e954
Apr 25, 2026
Merged

fix(par2): skip PAR2 creation for files smaller than SIMD alignment#213
javi11 merged 1 commit intomainfrom
claude/dazzling-hodgkin-c9e954

Conversation

@javi11
Copy link
Copy Markdown
Owner

@javi11 javi11 commented Apr 25, 2026

Summary

Fixes intermittent CI segfault in internal/par2:

INFO Starting par2 creation process executor=NativeExecutor
signal: segmentation fault (core dumped)
FAIL  github.com/javi11/postie/internal/par2  0.794s

Root cause

The par2go C backend may read up to stride-1 bytes past the input buffer when the slice size isn't a multiple of the SIMD stride (64 B on AVX-512). PR #189 fixed this for files ≥128 B by rounding the slice size down to a 128-byte boundary, but the else branch still passed sub-stride slices (e.g. 8 B for a 10-byte file) to the C library, causing segfaults on Linux x86_64 CI runners.

Crashes appeared "flaky" because the overrun only segfaults when the read crosses into an unmapped page — depends on heap layout, ASLR, and allocator state. The existing defer recover() in TestIntegration_NativeExecutor_HandlesVerySmallFiles_10Bytes cannot catch CGO segfaults (they bypass Go's panic mechanism). Locally on macOS the SIMD instruction path differs and the bug stays latent.

Change

In createPar2ForFile, when parBlockSize > file.Size and file.Size < 128, skip PAR2 creation with a warning instead of computing a sub-stride slice. Files that small yield no meaningful recovery data, and the upload pipeline already tolerates a nil par2 result (file upload runs in a parallel goroutine, poster.Post(ctx, nil, ...) is a no-op).

The existing 10-byte regression test was already written assuming "skip" is a valid outcome ("expect either a nil result (skipped with warning) or a graceful error, but never a panic").

Test plan

  • go test -race -count=1 ./internal/par2/... passes locally
  • TestIntegration_NativeExecutor_HandlesVerySmallFiles_10Bytes now takes the skip path
  • TestIntegration_NativeExecutor_HandlesVerySmallFiles_512Bytes still creates PAR2 (≥128 B path unchanged)
  • CI run on AVX-512 Linux: segfault gone

The par2go C backend may read up to (stride-1) bytes past the input
buffer when the slice size isn't a multiple of the SIMD stride (64 B
on AVX-512). PR #189 fixed this for files >=128 B by rounding the
slice size down to a 128-byte boundary, but the else branch still
fed sub-stride slices (e.g. 8 B for a 10-byte file) into the C
library, causing intermittent segfaults on Linux x86_64 CI runners.

Crashes were probabilistic because the overrun only segfaults when
the read crosses into an unmapped page, which depends on heap layout,
ASLR, and allocator state. Locally on macOS the SIMD path differs
and the bug stays latent.

Fix: skip PAR2 entirely for files <128 B (SIMD-safe alignment),
matching the existing skip-with-warning pattern. Files that small
yield no meaningful recovery data anyway, the upload pipeline
already tolerates a nil par2 result, and the existing 10-byte
regression test was written assuming "skip" is a valid outcome.
@javi11 javi11 merged commit c3e7ddf into main Apr 25, 2026
3 checks passed
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.

1 participant