Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*** Error in `php': malloc(): 0x000055c853bafde0 *** and e.t.c #10

Closed
antonim opened this issue May 29, 2017 · 9 comments
Closed

*** Error in `php': malloc(): 0x000055c853bafde0 *** and e.t.c #10

antonim opened this issue May 29, 2017 · 9 comments

Comments

@antonim
Copy link

antonim commented May 29, 2017

Hello. When I use code:

for ($i = 0; $i < 1000; $i++) {
    Loop::run(function ()  {
        $threads = [];
        for ($i = 0; $i < 100; $i++) {

            $threads[] = Thread::spawn(function () {
                return rand(1,100);
            });
        }

        foreach ($threads as $thread) {
            $thread->join();
        }
    });
    echo $i . "\n";
}

Everytime I'v get system error ex:

*** Error in `php': malloc(): memory corruption (fast): 0x0000000002209ee2 ***
Aborted (core dumped)

or

Segmentation fault (core dumped)

or

*** Error in `php': free(): invalid pointer: 0x00005579a7f2157d ***

or

*** Error in `php': corrupted double-linked list: 0x000055ca863cc7e0 ***

This can happen after the third iteration and can be after 1000 iterations. I can not find the relationship between the number of iterations and the receipt of this error.

Could you tell me how can I avoid that problem?

@trowski
Copy link
Member

trowski commented May 30, 2017

You are attempting to spawn 100 * 1000 = 100,000 threads as fast as possible. I would never expect something like this to work. Honestly I'm impressed you get any sort of output. To see something actually work, simply add yield in front of $thread->join(), which will wait for the 100 threads to exit before attempting to spawn another 100 threads.

@bwoebi
Copy link
Member

bwoebi commented May 31, 2017

@trowski It actually should wait anyway on the threads to exit first as we have open I/O watchers as long as they exist.
I don't think that's the issue.

@trowski
Copy link
Member

trowski commented May 31, 2017

@antonim It was pointed out to me by @bwoebi that I missed the obvious: Loop::run() should not exit until the first 100 threads have exited since there will be active watchers in the loop, so only 100 threads should be spawned at a time (which definitely should work fine). I'll look deeper into what might be happening here.

@kelunik
Copy link
Member

kelunik commented Jun 17, 2017

@antonim I guess you have some extension enabled that isn't thread-safe?

@krakjoe
Copy link

krakjoe commented Jul 18, 2017

So I reproduced the error, it would appear that this originates in PHP itself, at the point where INI entries are copied for new threads - not by pthreads but by ZE - I vaguely remember someone mentioning that this was a problem since we started refcounting strings, but I don't remember the detail.

Let me poke a few people and see what I can find out ...

@krakjoe
Copy link

krakjoe commented Jul 19, 2017

The best advice right now seems to be use pre-release builds of 7.2 with pthreads ... ZTS is broken in 7.0 and 7.1 ... I'm not sure why we didn't fix it when we first became aware of it, but we didn't, and I'm not sure how reasonable it is to back port the changes (zts supports interned strings in 7.2, which are not refcounted), and I don't know who has the appetite or time (I probably don't).

@krakjoe
Copy link

krakjoe commented Jul 19, 2017

This is the commit that fixed it: php/php-src@c698299

It contains ABI breaks, so 7.2 requirement is the only reasonable thing to do ... sorry about that ...

@kelunik
Copy link
Member

kelunik commented Jul 19, 2017

@krakjoe Thanks for looking into this. It's now segfaulting instead of having that memory corruption error on 7.2.0beta1.

krakjoe added a commit to krakjoe/pthreads that referenced this issue Jul 19, 2017
@kelunik
Copy link
Member

kelunik commented Jul 19, 2017

This runs fine now with the latest pthreads and PHP 7.2.0beta1.

@kelunik kelunik closed this as completed Jul 19, 2017
@kelunik kelunik removed the feedback label Jul 19, 2017
Shimmi added a commit to Shimmi/docker-php that referenced this issue Aug 28, 2017
The pthreads started requiring PHP 7.2. ("ZTS in versions 7.0 and 7.1 is broken"). This locks the library to the last commit supporting PHP 7.1. See amphp/parallel#10.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants