Had the same issue after heavy CAD use of my iMac (same machine, late 2012 3.4 GHz) for some hours, and it continued to happen. After I found this thread, I decided to check the outlet and turns out it was faulty, grounding wire was missing on the back side! Plugged the iMac into a good grounded outlet and it’s been running fine since.