Skip to content
/ atexit Public

Support for running deferred functions in case of abnormal exit

License

Notifications You must be signed in to change notification settings

perillo/atexit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

atexit GoDoc

Package atexit implements support for running deferred functions in case of abnormal exit (in contrast to a normal exit when the program returns from the main function).

Since calling os.Exit, e.g. during a signal handler, does not call deferred functions, a complementary mechanism is required when the program acquires resources that are not automatically released by the operating system at program termination, e.g. SYSV shared memory.

atexit is designed to work with, and complement, Go standard deferred mechanism. The Exit function provided by this package must be used, in order to run registered deferred functions.

The Exit function SHOULD only be called in case of abnormal program termination.

The idiomatic use of atexit is:

    AcquireResource(...)
    // ...
    defer atexit.Do(func() {
        ReleaseResource(...)
    })()

ReleaseResource will be called by the runtime deferred mechanism in case of normal program termination, but will be called by atexit when atexit.Exit is called during an abnormal program termination (e.g. after having received a signal), but only if they have not been already be called by the Go runtime.

About

Support for running deferred functions in case of abnormal exit

Resources

License

Stars

Watchers

Forks

Packages

No packages published