Skip to content

cargo test --lib aborts ~1 run in 5 with a UTF-8 / char unsafe-precondition violation #41

Closed

Description

@GLinnik21

What happens

cargo +nightly test --lib aborts, roughly 1 run in 5, with one of:

panicked at core/src/char/methods.rs:243:18:
unsafe precondition(s) violated: invalid value for `char`

panicked at core/src/str/mod.rs:435:9:
assertion failed: self.as_bytes()[0].is_utf8_char_boundary()

panicked at core/src/str/validations.rs:48:36 (and :55:40)

panicked at core/src/fmt/mod.rs:2949:38

always followed by thread panicked while processing panic. aborting. and
process didn't exit successfully: … (signal: 6, SIGABRT). No backtrace survives — the
abort happens while the panic is being formatted.

Every signature is the same underlying event: a &str being read while its bytes change.
That is a real data race or use-after-free in the suite's process-global state, not a flaky
assertion.

It is not the test that exposes it

Established on the pristine base (1dafd764), with ui/search/empty.rs reverted to its
untouched stub, by varying only what four added test bodies do:

what the four tests do runs aborts
nothing added (bare base) 20 0
assert_eq!(1, 1) 15 0
format! in a 200-iteration loop, calling no project code 12 2
the real unit's tests (which allocate via format!) ~40 ~4

So allocation churn from any new test is the trigger, not the cause. The reading that fits:
a latent use-after-free is normally benign because nothing reuses the freed block; another
thread's String allocations reuse it, and the corruption becomes visible.

This matters for attribution — a red make check may not be the author's change. Re-run
before believing it.

Narrowing so far

It needs tests from both halves of the suite to fire. Each of these was clean in 10 runs:

  • ui:: alone (280 tests)
  • stream:: browse:: auth:: task:: posters:: person:: metadata:: plex::probe ff:: (119 tests)
  • route:: alone (23 tests), 15 runs
  • ui::search::empty plex:: (58 tests), 8 runs

So it is cross-module: a global written by one module's tests and read by another's.

--test-threads=1 never fails (539+ tests, many runs). That is the workaround, and it is
also the proof that this is concurrency and not a bad assertion.

Where to look

The crate has many static mut String / static mut Vec<…> globals that hand out &'static
references (route.rs alone has ten, plus search.rs, ui/alt_sources.rs, ui/library.rs,
ui/detail.rs, ui/stats.rs). plex::servers' FACTS/SLOTS are leak-and-publish so a stale
read there is sound by construction, and reset_for_test only nulls pointers rather than
dropping the boxes — so that pair looks clean and is probably not it.

CLAUDE.md already names the shape of the hazard: the crate's async seams are process-wide, and
some tests take testlock::serial() for exactly this reason. The likely bug is a global that
needs that lock and does not take it, or a test that leaves a worker thread running past its own
end (browse::pump adopting registered slots and spawning a discovery worker per source is
called out in plex/servers.rs's own test comments as something that must not escape).

Repro

cd rust-modules
for i in $(seq 1 20); do cargo +nightly test --lib 2>&1 | grep -E "panicked at|test result"; done

Add four format!-heavy tests anywhere first if it will not reproduce on its own.

Found while building the Search screen's empty states; unrelated to that change.

Activity

  1. GLinnik21 commented on Aug 14, 2026

    @GLinnik21
    CollaboratorAuthor

    Found during #42 (Search empty states); that PR's verification notes point here.

  2. added
    bug
    internal
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buginternal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions