Skip to content

fix(cram): give reader threads enough stack for CRAM 3.1 rANS-Nx16 - #220

Merged
ewels merged 1 commit into
mainfrom
fix/cram31-thread-stack
Dec 4, 2026
Merged

ewels merged 1 commit into
mainfrom
fix/cram31-thread-stack

Conversation

@ewels

@ewels ewels commented Dec 4, 2026

Copy link
Copy Markdown
Member

Fixes #151. On main, any CRAM 3.1 file that samtools writes with its default settings crashes RustQC with fatal runtime error: stack overflow when --threads is above 1.

Cause

samtools' default CRAM 3.1 profile compresses most data series with rANS-Nx16 (rNx16 in samtools cram-size). noodles' rANS-Nx16 decoder needs more than the 2 MiB stack Rust gives a new thread, and RustQC reads alignments on rayon worker threads. With --threads 1 it reads on the main thread, which has 8 MiB, so single-threaded runs worked. CRAM 3.0, and CRAM 3.1 at samtools' archive level (which uses the arithmetic coder instead), decode fine.

Fix

Both rayon pools (per-chromosome workers in counting.rs, per-file workers in main.rs) now use an 8 MiB stack, set by rna::bam::READER_THREAD_STACK_SIZE. 4 MiB was already enough for the files I tried; 8 MiB matches the main thread.

Testing

  • New fixture tests/data/cram31/:
    • tests/data/test.bam written as CRAM 3.1 with samtools 1.21 defaults (3 KB). The README there has the commands.
    • The new test runs it at 1 and 4 threads and checks that flagstat matches the BAM's. Without the fix it crashes with the stack overflow.
  • Benchmark BAM (52.8k reads) converted to CRAM 3.0, CRAM 3.1 default and CRAM 3.1 archive:
    • With the fix, all three give the same output files as the BAM. The only differences are embedded file names and timestamps.
  • cargo fmt --check, cargo clippy -- -D warnings and cargo test --release pass.

The error in #151 came from htslib in v0.2.1 (Slice decode failure). With noodles, that file would most likely have hit this crash instead, so the reporter should retry with v0.3.0.

🤖 Generated with Claude Code

noodles' rANS-Nx16 decoder overflows Rust's 2 MiB default thread stack.
samtools writes CRAM 3.1 with that codec by default, so any such file
crashed RustQC with --threads above 1 (the main thread has 8 MiB, so
single-threaded runs worked). Both rayon pools now use 8 MiB stacks.

Adds a 3 KB CRAM 3.1 fixture of tests/data/test.bam and a test that runs
it at 1 and 4 threads and checks flagstat against the BAM.

Fixes #151

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ewels ewels added this to the v0.3 milestone Dec 4, 2026
@ewels
ewels merged commit bcdbce4 into main Dec 4, 2026
7 checks passed
@ewels
ewels deleted the fix/cram31-thread-stack branch December 4, 2026 08:59
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.

CRAM 3.1 seems unsupported

1 participant