trap exits in order to unregister all hooks in terminate function