Provide a local buffer capacitor for the cpu.
Or put a bigger buffer cap overt he main supply. Like 1000uf .
I prevent stuff like this from happening by feeding the cpu from its own power branch. You can do the following trick :
Take a shottky diode, or regular diode if you do t have a shottky, go from 5 volt to anode.
Go from carhode to your supply of the cpu.
Put a 220uf cap from cathode to ground
Whenever there is a current spike the cpu will not see it. The diode blocks the capacitor charge from flowing into the solenoid. So, the charge in the capaciotr can only be used by the cpu.
That solves the problem.