pwnlib.atexit — Replacement for atexit

Replacement for the Python standard library’s

Whereas the standard atexit module only defines atexit.register(), this replacement module also defines unregister().

This module also fixes a the issue that exceptions raised by an exit handler is printed twice when the standard atexit is used.

pwnlib.atexit.register(func, *args, **kwargs)[source]

Registers a function to be called on program termination. The function will be called with positional arguments args and keyword arguments kwargs, i.e. func(*args, **kwargs). The current context is recorded and will be the one used when the handler is run.

E.g. to suppress logging output from an exit-handler one could write:

with context.local(log_level = 'error'):

An identifier is returned which can be used to unregister the exit-handler.

This function can be used as a decorator:

def handler():

Notice however that this will bind handler to the identifier and not the actual exit-handler. The exit-handler can then be unregistered with:


This function is thread safe.


Remove the exit-handler identified by ident from the list of registered handlers. If ident isn’t registered this is a no-op.