Also adds assertions and a randomized multi-thread test to rule out similar issues with a higher confidence.