Improves the robustness of the trace_invariants test by shifting the various tracked state to be per-thread and per-nested-signal, avoiding incorrect results from thread interleavings or nested signals. This presumably explains past flakiness like #4575 (closed).
Fixes #4575 (closed)