Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:56:43+00:00 2026-06-11T21:56:43+00:00

I think this is an issue in code somewhere, but the code’s so simple

  • 0

I think this is an issue in code somewhere, but the code’s so simple I’m not sure what it could be.

I’ve verified wait_timeout is high enough and have gone through everything here: http://dev.mysql.com/doc/refman/5.1/en/gone-away.html without any success.

This happens reproducibly on the second query executed in one script run so I’m sure it’s a coding error.

I created a really simple wrapper around the PDO class to have a singleton database handle:

<?php

class PDOWrapper
{
    protected static $instance;
    protected $dbh;

    function __construct()
    {
        if ( is_null(static::$instance) )
        {
            static::$instance = $this;
            $this->connect_to_db();
        }
    }

    static function instance()
    {
        if ( is_null(static::$instance) )
        {
            new static;
        }

        return static::$instance;
    }

    private function connect_to_db()
    {
        $db_info = array(
            0 => array(
                'hostname' => "Host",
                'username' => "User",
                'password' => "Pass",
                'db' => "DB",
            )
        );

        //Try to connect to the database
        try 
        {
            $dbh = new PDO('mysql:host=' . $db_info[0]['hostname'] . ';dbname=' . $db_info[0]['db'], $db_info[0]['username'], $db_info[0]['password'], array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => true ));
        }
        catch (PDOException $e)
        {
            log_message("Error connecting to DB!: " . $e->getMessage(), LOG_LEVEL_CRITICAL );
            return false;
        }

        $this->dbh = $dbh;
    }

    public static function get_dbh()
    {
        if ( is_null(static::$instance) )
        {
            new static;
        }

        return static::$instance->dbh;
    }
}

I then use the wrapper like so:

function somefunc(){
    $dbh = PDOWrapper::get_dbh();
    $future_sth = $dbh->prepare("SELECT * FROM some_table");
    $future_sth->execute();
    $ret = $future_sth->fetchAll(PDO::FETCH_ASSOC);
    print_r($ret);
    $future_sth->closeCursor();
    return $ret;
}

I call this function repeatedly as part of an event loop. The first time it calls it, the print_r runs fine and it prints out the rows I expect to see.

After the function has been executed once, however, I get the following:

Warning: Error while sending QUERY packet. PID=92871
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'

I don’t know why it’d have “gone away”. My my.cnf looks ok. Wait timeout is massive and this happens immediately as soon as I run the second query anyway. Any ideas?

It doesn’t look like anything is obviously wrong in the MySQL error log:

120925 12:48:46 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
120925 12:48:46 [Warning] The syntax '--log' is deprecated and will be removed in a future release. Please use '--general-log'/'--general-log-file' instead.
120925 12:48:46 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
120925 12:48:46 InnoDB: The InnoDB memory heap is disabled
120925 12:48:46 InnoDB: Mutexes and rw_locks use GCC atomic builtins
120925 12:48:46 InnoDB: Compressed tables use zlib 1.2.5
120925 12:48:46 InnoDB: Initializing buffer pool, size = 128.0M
120925 12:48:46 InnoDB: Completed initialization of buffer pool
120925 12:48:46 InnoDB: highest supported file format is Barracuda.
120925 12:48:46  InnoDB: Waiting for the background threads to start
120925 12:48:47 InnoDB: 1.1.8 started; log sequence number 2273680401
120925 12:48:47 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
120925 12:48:47 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
120925 12:48:47 [Note] Server socket created on IP: '0.0.0.0'.
120925 12:48:47 [Note] Event Scheduler: Loaded 0 events
120925 12:48:47 [Note] /usr/local/Cellar/mysql/5.5.25a/bin/mysqld: ready for connections.
Version: '5.5.25a-log'  socket: '/tmp/mysql.sock'  port: 3306  Source distribution
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:56:45+00:00Added an answer on June 11, 2026 at 9:56 pm

    I figured it out.

    I was working on a multi-process daemon using pcntl_fork. The parent process was responsible for running a loop to query the database and fork children to perform additional work depending on what data it saw.

    The children didn’t need a DB connection, however they were still given one because pcntl_fork was used. I was just using exit() to kill the child processes when they were done with their work, which caused the ‘friendly’ PHP cleanup to close the active MySQL connection it perceived the child to have.

    Control would go back to the parent, who would find their DB connection was suddenly no longer valid when they tried to find more data in the database to send off to children.

    The fix, for me, was to use posix_kill(getmypid(), 9); to kill the children rather than exit();.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could either be very simple or quite complex I'm not sure at this
I think this could be a very easy question for you. But I have
I think this question is probably fairly simple, but I've been searching around and
I think this is an escaping issue or something. When I execute the query
I think I am overthinking this issue a little and I'm hoping someone can
(I think this is a pretty basic question on OOP, but unfortunately I wasn't
I think this is easily explained by looking at code, so I'm posting a
I think this is a simple and a silly question. I have included a
I think this is a pretty straightforward problem but... var outerHeight = $('.profile').outerHeight(); $(#total-height).text(outerHeight
I think this code For Each file in filecoll Ext = UCase(Right(File.Path, 3)) If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.