Attempting to unwind the tangled web of pid file creation.

Previously I wrote about how late the mysql pid file is created in the startup process. At first glance it seemed like a relatively easy thing to fix. In main() there is a call to start_signal_handler(). The first instance of static void start_signal_handler() does only one thing. It checks !opt_bootstrap to make sure mysqld isn’t being called by mysql_install_db. I’m not sure why mysql_install_db can’t have a pid file created but that’s getting outside the scope of my investigation. It seems simple enough to move the call to start_signal_handler() above the call to init_server_components() in main() and have the pidfile created earlier. It turns out pidfile creation happens differently on different arches.

For windows and netware start_signal_handler simply creates the pid file. For __EMX__ (I’m not sure what that is) start signal handler does nothing. By default start_signal_handler starts a signal handler thread. This thread then creates the pid file. I think this can be cleaned up by removing the start_signal_handler functions that either do nothing or only create a pid file and handle the pid file creation for arches that need it directly in main with some good self documenting ifdefs right around it.

I don’t have all the environments to test that this patch really works. I’ve tested it on linux and it does create a pid file and deletes it on shutdown. The pid file is created just after argument parsing and before the heavy weight storage engine initialization.

[Update 2009-12-07: I think the old patch broke embedded. I updated it to ifdef out the call to start_signal_handler]
Here is the patch Create pid file sooner patch.


  1. Davi Arnaut says:

    So, how does this fix the race that you mentioned? It seems that the patch only makes it more unlikely to happen.

  2. Eric Bergen says:

    It doesn’t fix it. It just makes it far less likely. Innodb initialization can take minutes or even hours to finish depending on how much work it has to do. WIth my patch the pid file is created after args are parsed.

    In the specific case I ran into while testing some changes to mysqld_safe the race was between me switching terminals to kill mysqld and the pid file being created. I often “won” that race due to the time it takes to initialize innodb. With this patch I can’t win because the time before creating the pid file has been significantly reduced.

    There is also another side case which is allowing two mysqlds to start on the same directly and perform storage engine initialization. Anyone that has had this happen with innodb has seen the flurry of file locking errors innodb spits out when this happens. This patch doesn’t fix that race condition either but makes it significantly less likely to happen.

  3. Antony Curtis says:

    Just FYI, __EMX__ denotes OS/2 using the EMX runtime, originally developed by Eberhard Mattes to port unix applications to the OS/2 platform.

  4. [...] long as we’re in /var, here’s Eric Bergen attempting to unwind the tangled web of pid file creation, which he started doing having discovered a pid file creation race condition in [...]

Leave a Reply