Investigating A PDO Segfault

Posted by Stuart Herbert @ 8:54 AM, Thu 29 Jun 06

Filed under: PHP

3 Comments

I’ve been playing about with using PDO to work with mysql. Something’s not quite right in there, and it’s resulting in a segfault when I run some local unit tests.

I’m not all that hot on PHP’s internals; I only tend to go digging in there when work needs a runkit bug diagnosing and fixing. But I’m pretty sure that what I’m seeing needs some TLC.

PDO objects themselves are created and destroyed when I’d expect them to be, as the objects go out of scope. The problem is that the PDOStatement objects aren’t doing the same. According to gdb, PDOStatement objects are only getting cleaned up when php_request_shutdown() runs; never before.

Surely we should be seeing PDOStatement objects being destroyed before their corresponding PDO object? The segfault is happening because php_mysql_stmt_dtor() is calling mysql_more_results() after the PDO object has been destroyed. We’re passing garbage data into the mysql client library … which chokes and segfaults as a result.

I think there’s two bugs here. The first problem is that PDOStatement objects appear to be persisting even though they’ve gone out of scope. php_pdo_stmt_delref() only gets called during php_request_shutdown(), never before. That doesn’t seem right to me. Why aren’t PDOStatements being destroyed when they go out of scope?

The second problem is that PDO objects are being destroyed before their corresponding PDOStatement objects. I couldn’t find (or, most likely, I didn’t recognise it :) any code that handles reference counting between PDO and PDOStatement objects. Maybe I’ve got it wrong, but I’d expect PDOStatement objects to add/subtract the corresponding PDO object’s reference counter, to ensure that PDO objects don’t get destroyed until all of their PDOStatement objects have been destroyed.

Anyone else got an opinion on this?

3 Comments

  1. Pierre says:
    June 29th, 2006 at 10:28 am

    It is a known problem but hard to fix in the current state. There is a couple of bug reports explaining the same issue (some has been wrongly bogus’ed), one of them is closed from your case:

    http://bugs.php.net/bug.php?id=37445

    A PDO connection only keeps a trace of its statement if an error occured, but not generally. My oppinion is that we need a kind of resource (stmt incl.) manager in pdo, and not trying to play with refcounting.

  2. Wez Furlong says:
    June 30th, 2006 at 1:30 am

    regarding “trying to play with refcounting”, refcounting works fine, so long as everything respects it. In theory, there is no need to do anything more than refcounting. It’s clear now that more gross hackery is required.

  3. tony2001 says:
    June 30th, 2006 at 8:37 am

    >I couldn’t find (or, most likely, I didn’t recognise it any code that handles reference counting between PDO and PDOStatement objects.
    There are none.

Calendar

June 2006
S M T W T F S
« May   Jul »
 123
45678910
11121314151617
18192021222324
252627282930