PHP Segmentation Error 11

Just a few days before public deployment of my new website this error popped up in the terminal. Apache was running fine but my favorite php test server crashed after 5 minutes.

sudo php -S -t /Users/wjst/Server

Unfortunately, error logging did not help, at least not the usual way

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

I could see only the last retrieved page in the terminal but adding

file_put_contents('debug.log', print_r(get_defined_vars(),1));

did not show any error log. Also encapsulating

try {
catch(PDOException $e) {
  #echo $e->getMessage();

was not successful. So I updated PHP which is always a good idea.

curl -s | bash -s 7.2

while the error remained. And no logs even after running

php -d xdebug.auto_trace=ON -d xdebug.trace_output_dir=tracedir/

Unfortunately, there is no strace on OSX so I tried dtrace but have been giving up this strategy after 2 hours as the output was not readable at all. Segmentation errors are a nightmare.

Going the hard way – uncommenting each line step by step – it turned out that the sqlite database query killed the server. Looping over different SQL statements did not replicate the error, which occured only when the script was invoked by AJAX calls. So I removed all variables relying on the $_SERVER environment but again, the segmentation error persisted. Strange.

Finally I found the problem. Whenever a query

$db = new PDO('sqlite:database.sqlite3');
$result = $db->query($sql);

was leading to an empty result set, the segmentation error occurred.

As this occurs during execution of the query, the only workaround was to ensure that there is always at least one result set.

Sounds not very logical but this worked out now for the last 24 hours.