I discovered a race condition in ActionView::TemplateHandlers::Compilable#compile_template. Wrapping the method in a mutex fixes the problem.
I have added some comments pointing out the race conditions and included a test (that must be run explicitly) to reproduce them. The test that reproduces them does so by "brute force". I also have written tests which deterministically reproduce the race conditions but they require adding conditional sleep statements to the production code, eg:
sleep Thread.current[:sleep_time_for_race_condition] if Thread.current[:sleep_time_for_race_condition]
I favored the brute force test as it does not require production code changes.