PHP Process – Tracing PHP Process Killed on Mac Does Not Work

memoryPHP

I'm running a simple PHP process that uses a tiny bit of memory and CPU. The max memory limit is 2G and the script is run from console.

The memory used never exceeds 200 MB. CPU use fluctuates between 10 – 80% of a single core.

At random, the process will be killed with message Killed: 9. There is nothing else given in the error message in the console and nothing appears in the logs.

This even happens if I pause the script using XDebug so that no additional memory can be used.

This happens if I disable Xdebug as well, so I don't suspect that is the culprit.

I've tried rebooting, but still happens.

The PHP script simply pulls a list of ads from Facebook using their PHP SDK v3.2 and then adds them to a database. There is only one worker running. Very simple process.

I have 32 GB RAM and 1TB HD. Running PHP 7.2 with nginx, MySQL 5.7 and redis.

My sw_vers output:

ProductName:    Mac OS X
ProductVersion: 10.14.2
BuildVersion:   18C54

System Integrity Protection status: enabled

This is the essence of the script:

$ads = $this->getApiAdAccount()->getAds($fields, $params);

$ads->setUseImplicitFetch(true);
$ads->rewind();

if (!$ads->valid()) {
    return false;
}

while ($ads->valid()) {
    $adData = $ads->current()->getData();

    try {
        if (!Campaign::find($adData['campaign'])) {
            dispatch(new ImportCampaign($adData['campaign']));
        }

        Ad::updateOrCreate(['id' => $adData['id']], $adData);
    } catch (\Exception $e) {
        $this->error($e);
    }

    $ads->next();
}

There is no specific spot it fails. I can run the Laravel job worker (redis queue) to process the code and insert/update the tiny array of data it processes on each loop.

How can I trace the source of this crash?

I've tried dmesg and dmesg | grep -E -i -B100 'killed process' as suggested elsewhere, but no useful information comes out.

Best Answer

The behavior you're seeing is not a problem with the hardware, the operating system, or lack or resources.

Instead it is simply the way that Laravel queueing works. If the worker takes too long to process a job, there's a timeout and the worker is killed with signal 9. Similar if the memory usage exceeds a set amount, the worker is killed.

You can set these limits with for example the --memory option.