LM393N output doesn't behave as expected
It appears you are experiencing "latchup" which can occur when the input is higher or lower than the rail.  You have a big capacitor which can hold a charge when the power fluctuates.  You can try putting a schottky diode (reverse biased) between signal cap_th and ground and cap_th and the rail which might solve it.