ObiHill
ObiHill

Reputation: 11876

Segmentation Fault running Gearman PHP from the Command Line

I'm using Ubuntu Natty with PHP 5.3.5 and PECL Gearman 0.8.0. Here's the version info:

PHP 5.3.5-1ubuntu7.3 with Suhosin-Patch (cli) (built: Oct 13 2011 22:20:48)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with the ionCube PHP Loader v4.0.10, Copyright (c) 2002-2011, by ionCube Ltd., and
    with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH

I'm getting a segmentation fault when trying to run a Gearman Client via the command line (I already have my worker running).

Here's what I get on the command line:

root@Local:~/sandbox# php php_gearman_client.php
Sending job
Segmentation fault

Here is my worker code:

<?php

echo "Starting\n";

# Create our worker object.
$gmworker= new GearmanWorker();

# Add default server (localhost).
$gmworker->addServer();

# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  echo "Received job: " . $job->handle() . "\n";

  $workload = $job->workload();
  $workload_size = $job->workloadSize();

  echo "Workload: $workload ($workload_size)\n";

  # This status loop is not needed, just showing how it works
  for ($x= 0; $x < $workload_size; $x++)
  {
    echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
    $job->sendStatus($x, $workload_size);
    sleep(1);
  }

  $result= strrev($workload);
  echo "Result: $result\n";

  # Return what we want to send back to the client.
  return $result;
}

# A much simpler and less verbose version of the above function would be:
function reverse_fn_fast($job)
{
  return strrev($job->workload());
}

?>

And here is my client code:

<?php

# Create our client object.
$gmclient= new GearmanClient();

# Add default server (localhost).
$gmclient->addServer();

echo "Sending job\n";

# Send reverse job
do
{
  $result = $gmclient->do("reverse", "Hello!");

  # Check for various return packets and errors.
  switch($gmclient->returnCode())
  {
    case GEARMAN_WORK_DATA:
      echo "Data: $result\n";
      break;
    case GEARMAN_WORK_STATUS:
      list($numerator, $denominator)= $gmclient->doStatus();
      echo "Status: $numerator/$denominator complete\n";
      break;
    case GEARMAN_WORK_FAIL:
      echo "Failed\n";
      exit;
    case GEARMAN_SUCCESS:
      break;
    default:
      echo "RET: " . $gmclient->returnCode() . "\n";
      exit;
  }
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);

?>

EDIT

It appears the segmentation fault was being caused by Imagick. So I did the following to deal with the issue.

  1. Remove imagick dpkg --purge --force-all php5-imagick. I had installed this when I was setting up PHP
  2. Restart PHP (This could vary depending on how you installed php)
  3. Restart Gearman Job Server /etc/init.d/gearman-job-server stop && /etc/init.d/gearman-job-server

Everything seems to be working ok now.

Upvotes: 4

Views: 2062

Answers (2)

Layke
Layke

Reputation: 53156

I just also had this problem. This is the soution I took (to fully resolving it), and steps to observing the error.

Step 1.

Running the Gearman Worker resulted in a:

Segmentation fault

Step 2.

Run dmesg from shell, I observed that this was showing up.

[2423402.716386] php[21232]: segfault at 30 ip b66d0321 sp bfc704c0 error 6 in libuuid.so.1.3.0[b66cf000+3000]

Step 3.

Googled for libuuid.so and it became apparent that Imagick uses uuid_create() for Image UUIDs.

http://usrportage.de/archives/922-PHP-segfaulting-with-pecluuid-and-peclimagick.html

Step 4. (The solution)

I ensured that Imagick extension gets initialized last out of all of my PHP extensions. So previously I had in the first line of my

/etc/php5/conf.d/extensions.ini file imagick.so

What I did instead was created a file called imagick.ini and the contents of that were

imagick.so

Because the extensions are loaded in alphabetical order (and if you are running your Worker through command line), loading imagick last ensures that all the dependant extensions are loaded first. In this case libuuid.

Upvotes: 2

dev-null-dweller
dev-null-dweller

Reputation: 29462

since it is segmentation fault that means something is wrong with your installation. Run dmesg to see more details there may be problem with some php extension that could be disabled.

Upvotes: 2

Related Questions