Weird php race issue

Status
Not open for further replies.

AliceWonder

Ars Legatus Legionis
24,239
Code:
<?php

ini_set('default_charset', 'UTF-8');
if(function_exists('mb_internal_encoding')) {
  mb_internal_encoding('UTF-8');
  mb_regex_encoding('UTF-8');
}


date_default_timezone_set('America/New_York');
define('SITE_CACHE_ID', '0');
require_once('dbconnect.inc.php');
require_once('cacheDatabaseFunctions.inc.php');
require_once('generalFunctions.inc.php');
...
?>

dbconnect.inc.php is the first file required

Code:
<?php
try {
  $pdo = new PDO("mysql:dbname=alicenet;host=localhost;charset=utf8", "user", "pass");
  $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}

catch(PDOException $e) {
  print('Database Connection Failure');
}
?>

Vast majority of time no issues.

Every now and then, a page won't load, and when I look in my error logs:

Code:
 PHP Notice:  Undefined variable: pdo in .....

It's not often but seems (not scientific) to be most likely to happen if the server has been under load.

So it seems that sometimes the dbconnect.inc.php script has not finished by the time another script required after it tries to use the $pdo handler.
Is there something I'm missing? I thought php loaded required files in serial. Is that incorrect?

php 5.6.5 (but has happened in every 5.6.x) x86_64 on CentOS 7
 
Use "the razor" and guess ...

pdo isn't declared because the connection failed. Pretend that "$pdo = connection" is actually:

create connection
optional abort
create variable $pdo
set $pdo to connection

I'd just assume that PHP is aborting before creating the variable, because it's PHP. Also, are you explicitly closing the connection using "$pdo = null"?
 

wallinbl

Ars Legatus Legionis
14,336
Subscriptor
[url=http:/civis/viewtopic.php?p=28435885#p28435885:1hfk4srb said:
KrispyK[/url]":1hfk4srb]You are catching the exception and printing an output to the page, but the rest of your scripts will still run, thus later on when you try to use $pdo it will fail.

You should either handle the exception better or remove the try/catch entirely so your script can properly fail.
This, basically. That try/catch isn't stopping execution or doing anything to notify the rest of the program that it failed. So, the rest of the program just assumes it worked.
 

AliceWonder

Ars Legatus Legionis
24,239
[url=http:/civis/viewtopic.php?p=28437427#p28437427:2djofvzx said:
wallinbl[/url]":2djofvzx]
[url=http:/civis/viewtopic.php?p=28435885#p28435885:2djofvzx said:
KrispyK[/url]":2djofvzx]You are catching the exception and printing an output to the page, but the rest of your scripts will still run, thus later on when you try to use $pdo it will fail.

You should either handle the exception better or remove the try/catch entirely so your script can properly fail.
This, basically. That try/catch isn't stopping execution or doing anything to notify the rest of the program that it failed. So, the rest of the program just assumes it worked.

Okay. I should have it sleep for a few and try again until it fails X times, because there is no reason why it should be failing at all.

So there is probably a different problem in the mariadb and/or pdo driver. Or the MariaDB database.
 

AliceWonder

Ars Legatus Legionis
24,239
okay I think I got it fixed now. It tries up to 5 times to connect with 500ms between each try.

First success it breaks out and continues, 5 failures and it sends a 500 internal server error and dies.

I use to use PostgreSQL exclusively, but using MariaDB for this - and also using newer php - I'm going to do some experiments to see if the issue (why it is not always connecting when it should be) is MariaDB vs PostgreSQL.

That's just weird.
 

AliceWonder

Ars Legatus Legionis
24,239
[url=http:/civis/viewtopic.php?p=28442947#p28442947:1ujs7hxr said:
GohanIYIan[/url]":1ujs7hxr]What does the exception actually say? That may shed light on what the issue is.

Not sure, it isn't happening often, just something I was seeing in my logs (the $pdo not defined)

I originally set up the exception catch so that if by accident error reporting was turned on on production, it wouldn't get echoed to browsers as database connection errors can have info you don't want the public to see.

Since it only seems to have been happening under heavy load (and not often even then) it may simply be the database was under too much load at the instant the request was made (it's not a powerful server, it's a VM)

I might spend the few bucks for another VM for the database itself if it ends up being a problem, but I suspect with how rare the error was, giving 5 spaced tries before dying is good enough.

EDIT - I'm also in the process of reducing the database load via APCu.
 

KrispyK

Ars Scholae Palatinae
911
[url=http:/civis/viewtopic.php?p=28443011#p28443011:yv7h6yjb said:
resedit[/url]":yv7h6yjb]
Not sure, it isn't happening often, just something I was seeing in my logs (the $pdo not defined)
... but if you log the exception, you would know exactly what the cause was!

[url=http:/civis/viewtopic.php?p=28443011#p28443011:yv7h6yjb said:
resedit[/url]":yv7h6yjb]I'm also in the process of reducing the database load via APCu.
This won't help your database load at all, it's a compiler cache for PHP. And i very much doubt it will gain you much, unless you have a complex application with lots (1000s) of individual .php files.
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
[url=http:/civis/viewtopic.php?p=28440011#p28440011:yzk4oo3u said:
resedit[/url]":yzk4oo3u]okay I think I got it fixed now. It tries up to 5 times to connect with 500ms between each try.

First success it breaks out and continues, 5 failures and it sends a 500 internal server error and dies.

*note* not a PHP guy, so this is general.

You didn't 'fix' anything. You just implemented a broad, and only somewhat effective workaround. (If it doesn't work, do it a bunch of times!) You still have an intermittent problem, and you still don't know what it is.

As others have said, log the database exception and find out what is actually failing. Until you do that, you're just guessing.
 

technophile

Ars Legatus Legionis
23,768
Yeah, that's one of the most common things I have to discuss with my junior devs. If you find yourself logging a hardcoded message, think harder about whether you could instead log something of the context to make debugging easier. At a minimum you should be logging the actual exception message and stack trace.

Nobody ever learned anything useful from "Something broke!".
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
[url=http:/civis/viewtopic.php?p=28574399#p28574399:22z3at9d said:
resedit[/url]":22z3at9d]It's not happening at all now that I have it retry if it fails up to 5 times.

As an app support person, that screams of something that will bite you in the ass in the future. You don't know what you fixed. You don't know why it's fixed. And the workaround is basically "Mash butan until working'.

But hey, good luck.
 
[url=http:/civis/viewtopic.php?p=28574551#p28574551:1agfoilh said:
C0rinthian[/url]":1agfoilh]
[url=http:/civis/viewtopic.php?p=28574399#p28574399:1agfoilh said:
resedit[/url]":1agfoilh]It's not happening at all now that I have it retry if it fails up to 5 times.

As an app support person, that screams of something that will bite you in the ass in the future. You don't know what you fixed. You don't know why it's fixed. And the workaround is basically "Mash butan until working'.

But hey, good luck.

Well the issue I'm dealing with is that it is very bad for Joe User connecting via his web browser to get a broken page because of a failed backend SQL connection.

That's bad regardless of the cause.
 

GohanIYIan

Ars Legatus Legionis
10,196
[url=http:/civis/viewtopic.php?p=28613955#p28613955:2tivak3a said:
resedit[/url]":2tivak3a]
[url=http:/civis/viewtopic.php?p=28574551#p28574551:2tivak3a said:
C0rinthian[/url]":2tivak3a]
[url=http:/civis/viewtopic.php?p=28574399#p28574399:2tivak3a said:
resedit[/url]":2tivak3a]It's not happening at all now that I have it retry if it fails up to 5 times.

As an app support person, that screams of something that will bite you in the ass in the future. You don't know what you fixed. You don't know why it's fixed. And the workaround is basically "Mash butan until working'.

But hey, good luck.

Well the issue I'm dealing with is that it is very bad for Joe User connecting via his web browser to get a broken page because of a failed backend SQL connection.

That's bad regardless of the cause.

And? That's sort of irrelevant. You could do this:
Code:
while (true) {
    try {
       connectToDatabase();
    } catch (Exception $e) {
    }
}
or you could do this:
Code:
while (true) {
    try {
        connectToDatabase();
    } catch (Exception $e) {
        writeToLog($e);
    }
}

From Joe User's perspective, they both "work". But only one will tell you what the hell is actually happening. In general, knowing what the hell is happening in your program is a good idea.
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
[url=http:/civis/viewtopic.php?p=28613955#p28613955:35koive8 said:
resedit[/url]":35koive8]
[url=http:/civis/viewtopic.php?p=28574551#p28574551:35koive8 said:
C0rinthian[/url]":35koive8]
[url=http:/civis/viewtopic.php?p=28574399#p28574399:35koive8 said:
resedit[/url]":35koive8]It's not happening at all now that I have it retry if it fails up to 5 times.

As an app support person, that screams of something that will bite you in the ass in the future. You don't know what you fixed. You don't know why it's fixed. And the workaround is basically "Mash butan until working'.

But hey, good luck.

Well the issue I'm dealing with is that it is very bad for Joe User connecting via his web browser to get a broken page because of a failed backend SQL connection.

That's bad regardless of the cause.

And now each request takes up to 5x as long as it should which is also bad for Joe User. And the original failure can still potentially cause broken pages.

And since we don't know what's wrong, it's possible requests are getting to the DB, but the response isn't making it back, in which case, blindly resubmitting can result in duplicate data! (Or swallowed DBA exceptions which are expensive)
 
I figured it out - my session management class, the garbage collection function was calling $pdo instead of $this->pdo - hence why the app seemed to work fine yet every now and then I would see that error in the logs. Garbage collection for the session database just wasn't getting run, which only means old data wasn't getting discarded.

And that's probably why it seemed to happen most often under heavy load, statistically that simply is when gc is most likely to be triggered.
 
Status
Not open for further replies.