# Thread cancellation on GNU/Linux

**URL:** <https://internals.rust-lang.org/t/thread-cancellation-on-gnu-linux/6758>\
**Category:** compiler\
**Created:** [February 13, 2018, 3:45pm UTC](https://internals.rust-lang.org/t/thread-cancellation-on-gnu-linux/6758 "2018-02-13T15:45:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fweimer](https://avatars.discourse-cdn.com/v4/letter/f/d2c977/32.png) [@fweimer](https://internals.rust-lang.org/u/fweimer)\
**Post date:** [February 13, 2018, 3:45pm UTC](https://internals.rust-lang.org/t/thread-cancellation-on-gnu-linux/6758/1 "2018-02-13T15:45:56Z")

</div>

The announcement that unwinding through FFI boundaries now aborts (on some platforms?) made me wonder how Rust approaches the GNU/Linux implementation of thread cancellation.

`pthread_cancel` uses unwind tables, based on the same mechanism as C++ exception handling. GNU C also supports destructors which run during stack unwinding (using the [`cleanup` variable attribute](https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute)). Furthermore, the x86-64 ABI mandates that unwind tables are emitted, so a precise stack trace can be obtained at all times. (Other ABIs have this by construction, the usual outlier is Aarch64, where some distributions carry custom GCC patches to enable unwind tables by default.)

This means that on GNU/Linux, unwinding is usually possible through FFI boundaries, but obviously, the foreign code has to be structured in a certain way to support unwinding reliably. A few libraries explicitly do the right thing (supporting it is pretty much like providing `longjmp`-safe callback hooks), and some more will just work, and many will just have minor memory leaks if unwinding proceeds through their code.

`pthread_cancel` already causes issues with C++, particularly due the more pervasive use of the `noexcept` attribute, and cannot be used to cancel threads spawned using C++ library facilities (because the thread routine is generally specified to be `noexcept`).

Note that POSIX thread cancellation is generally synchronous. It’s more like [`Thread.interrupt()`](https://docs.oracle.com/javase/9/docs/api/java/lang/Thread.html#interrupt--), and not [`Thread.stop()`](https://docs.oracle.com/javase/9/docs/api/java/lang/Thread.html#stop--). A pretty close approximation of the GNU/Linux implementation is that certain `libc` functions (cancellation points) throw a C++ exception when the thread has been or is being canceled, and this exception is automatically rethrown if a C++ exception handler attempts to completely swallow the exception.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/thread-cancellation-on-gnu-linux/6758/2 "2019-03-25T08:29:39Z")

</div>

Closed on March 25, 2019, 8:29am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
