Make sure that thunks are restored properly if an exception occurs

Fixes Hydra bug #67.