Skip to content

PHP segfaults if pcntl is built as shared module #9298

Closed

Description

@hasso

Description

The following code:

<?php

declare(strict_types = 1);

function sigalrm()
{
}

while (true) {

    pcntl_signal(SIGALRM, "sigalrm", false);

    $pid = pcntl_fork();
    if ($pid < 0) {
        printf("pcntl_fork() failed\n");
        exit(-1);
    }

    if ($pid == 0) {
        exit(0);
    } else {
        while (true) {
            posix_kill($pid, SIGALRM);
            $wpid = pcntl_wait($st, WNOHANG);
            if ($wpid > 0) {
                break;
            }
        }

        if (pcntl_wifsignaled($st)) {
            $signo = pcntl_wtermsig($st);
            if ($signo != SIGALRM) {
                printf("##### Worker killed by signal %d ####\n", $signo);
                exit(0);
            }
        }

        printf("Worker exited normally\n");
    }

}

Resulted in this output:

##### Worker killed by signal 11 ####

But I expected this output instead:

Worker exited normally
Worker exited normally
Worker exited normally
Worker exited normally
...

The problem only appears if pcntl is built as shared module (--enable-pcntl=shared). There is no problem if pcntl support is built in (--enable-pcntl).

PHP Version

PHP 8.1.9

Operating System

Debian GNU/Linux 11 (bullseye)

Activity

  1. hasso commented on Aug 12, 2022

    @hasso
    Author

    I edited the test code to fix small bug - it could happen before that the code was going to kill a wrong process.

  2. added 2 commits that reference this issue on Nov 13, 2022
    5ecbb1b
    d8fc1af
  3. added a commit that references this issue on Nov 13, 2022
    9575968
  4. added a commit that references this issue on Nov 15, 2022
    6fa26b1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions