It depends on the mission, but there is normally a "safe mode" which can be triggered for a number of reasons, for example the battery rings very low, it's spinning too fast, it's lost lock on the Earth, etc.
Often the safe mode will do a hardware reconfiguration, which is like an interrupt, and physically switch over to a different software bank and run specific software to recover the space craft, stabilise it, orient the solar array to the sun and point comms antennas at the earth (all depending on the spacecraft and orbit of course).
However if the spacecraft is spinning because of a fuel leakage, it may not have the capability to control it's attitude anymore. For all the "miraculous" recoveries (and some of the failure reports show amazing ingenuity) there are a number of spacecraft that never recover. I'm crossing my fingers.