What does the POSIX standard say about stack streams in atexit () handlers? What is OS practice?

When our UNIX / C program needs an emergency exit, we use the exit (3) function and install atexit (3) handlers for emergency cleaning. This approach worked fine until our application got the thread, and at that moment the atexit () handlers stopped working with the forecast.

We learned from a test error that threads may already be dead in the atexit () handler, and their stacks are freed.

I was unable to find a quote in the standard disappearance of the link stream using atexit (): the threads cease to exist after returning from main (), but is this before calling atexit () or after? What is the actual practice for Linux, FreeBSD, and Mac?

Is there a good pattern for emergency cleaning in a multi-threaded program?

+6
source share
1 answer

Posix Standard

Posix does not seem to determine if atexit handlers are called before or after threads exit .

There are two (or three) ways to complete the normal process.

  • All threads terminate. When the last thread exits, either by returning or by calling pthread_exit , atexit handlers are executed. In this case, there are no other threads. (It depends on the platform. Some platforms may terminate other threads if the main thread terminates differently than exit , others do not.)

  • One thread calls exit . In this case, atexit handlers will be executed, and all threads will be completed. Posix does not indicate in which order.

  • main returns. This is more or less equivalent to calling exit() as the last line of main , so it can be considered as described above.

OS practice

On Linux, the documentation https://linux.die.net/man/2/exit says that threads terminate with _exit calling exit_group and that _exit is called after the atexit handlers. Therefore, on Linux, when calling exit all atexit handlers are started before the threads are completed. Note that they are executed in the exit stream call, not in the stream called atexit .

On Windows, the behavior will be the same if you don't care.

Templates for emergency cleaning.

Best Model: Never be in a condition requiring emergency cleaning.

  • There is no guarantee that your cleanup will start because you can have kill -9 or a power outage.
  • Therefore, you should be able to recover in this scenario.
  • If you can restore it, you can also restore it from abort , so you can use abort for your emergency exit.

If you cannot do this, or if you have a "cleanup" cleanup that you want to do, the atexit handlers should be fine if you first gracefully stop all threads in the process to prevent the input of inconsistent cleanups.

+3
source

All Articles