If you want to automatically "handle" deadlocks, you'll need an universal deadlock detection algorithm, which is undecidable in general. It may be possible to make it decidable through special means, but it normally does not worth it.
It would be easier for every program to give up the resources they're holding if they've waited for a very long time for another resource.