There are a number of places where the code makes use of volatile variables, where it should be using variants of atomic ones (the comments even indicate that they should be atomic).
The code would work just about all the time, but when it breaks it will break in a very strange way indeed (undefined behaviour style).
See J F Bastien - Deprecating volatile for the deep-dive on this.