Skip to content

Our pthread_kill implementation is incorrect, this API is not for killing threads, but sending signals to them #14872

Open

Description

@sbc100

pthread_kill, just like kill, is for sending signal to processes. What is more it seems to specifically not be capable of killing an individual pthread, only directing a signal to be received on a specific thread.

The only way a thread can be killed is if vulunarily exits, and sending a signal to a thread is just one way to ask it to exit. If one actually send SIGKILL to a thread it will bring down the entire process, not that just thread.

See https://stackoverflow.com/questions/34258271/pthread-kill-kills-not-just-a-thread-but-the-whole-program for some discussion of this.

We could make a fake implementation of pthread_kill but we would only be able to deliver signals to threads that yield in some way and not busy-looping. It seems that we can really only support cooperative receiving of signal which means we probably can't do better than pthread_cancel if we want to try to bring down a running thread.

What is more the current test for pthread_kill is currently disabled on chrome because it causes the tab to hang (i.e. worker.terminate() simply doesn't work for busy-looping threads). See:

@no_chrome('pthread_kill hangs chrome renderer, and keep subsequent tests from passing')

Activity

  1. added 2 commits that reference this issue on Aug 15, 2021
    90313d8
    78fab5d
  2. kleisauke commented on Aug 16, 2021

    @kleisauke
    Collaborator

    Perhaps we should only support the SIGCANCEL signal? Although deprecated by the POSIX standard, it's still used to implement pthread_cancel internally in musl, which could help move pthread_cancel to native code (see e.g. commit 16970d2).

    return pthread_kill(t, SIGCANCEL);

    if (signal === 33/*SIGCANCEL*/) { // Used by pthread_cancel in musl
    if (!ENVIRONMENT_IS_PTHREAD) cancelThread(thread);
    else postMessage({ 'cmd': 'cancelThread', 'thread': thread });

  3. sbc100 commented on Aug 16, 2021

    @sbc100
    CollaboratorAuthor

    The problem is that we have no way to deliver SIGCANCEL (or any signal) asynchronously... so we can't interrupt a busy-looping thread. But yes, one possible option is to implement pthread_kill so that it actually does call the signal handler on the respective thread (although it would have to be done in co-operations with the receiving thread). I think you are correct, I will try to do that instead.

  4. added a commit that references this issue on Jan 3, 2025
    6cd6ddc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions