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 7510043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:06:42+00:00 2026-05-29T23:06:42+00:00

I need to use mutexes or semaphores in PHP, and it scares me. To

  • 0

I need to use mutexes or semaphores in PHP, and it scares me. To clarify, I’m not scared of writing deadlock-free code that synchronizes properly or afraid of the perils of concurrent programming, but of how well PHP handles fringe cases.

Quick background: writing a credit card handler interface that sits between the users and the 3rd party credit card gateway. Need to prevent duplicate requests, and already have a system in place that works, but if the user hits submit (w/out JS enabled so I can’t disable the button for them) milliseconds apart, a race condition ensues where my PHP script does not realize that a duplicate request has been made. Need a semaphore/mutex so I can ensure only one successful request goes through for each unique transaction.

I’m running PHP behind nginx via PHP-FPM with multiple processes on a multi-core Linux machine. I want to be sure that

  1. semaphores are shared between all php-fpm processes and across all cores (i686 kernel).
  2. php-fpm handles a PHP process crash while holding a mutex/semaphore and releases it accordingly.
  3. php-fpm handles a session abort while holding a mutex/semaphore and releases it accordingly.

Yes, I know. Very basic questions, and it would be foolish to think that a proper solution doesn’t exist for any other piece of software. But this is PHP, and it was most certainly not built with concurrency in mind, it crashes often (depending on which extensions you have loaded), and is in a volatile environment (PHP-FPM and on the web).

With regards to (1), I’m assuming if PHP is using the POSIX functions that both these conditions hold true on a SMP i686 machine. As for (2), I see from briefly skimming the docs that there is a parameter that decides this behavior (though why would one ever want PHP to NOT release a mutex is the session is killed I don’t understand). But (3) is my main concern and I don’t know if it’s safe to assume that php-fpm properly handles all fringe cases for me. I (obviously) don’t ever want a deadlock, but I’m not sure I can trust PHP to never leave my code in a state where it cannot obtain a mutex because the session that grabbed it was either gracefully or ungracefully terminated.

I have considered using a MySQL LOCK TABLES approach, but there’s even more doubt there because while I trust the MySQL lock more than the PHP lock, I fear if PHP aborts a request (with*out* crashing) while holding the MySQL session lock, MySQL might keep the table locked (esp. because I can easily envision the code that would cause this to take place).

Honestly, I’d be most comfortable with a very basic C extension where I can see exactly what POSIX calls are being made and with what params to ensure the exact behavior I want.. but I don’t look forward to writing that code.

Anyone have any concurrency-related best practices regarding PHP they’d like to share?

  • 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-05-29T23:06:44+00:00Added an answer on May 29, 2026 at 11:06 pm

    In fact, i think there is no need for a complex mutex / semaphore whatever solution.

    Form keys stored in a PHP $_SESSION are all you need. As a nice side effect, this method also protects your form against CSRF attacks.

    In PHP, sessions are locked by aquiring a POSIX flock() and PHP’s session_start() waits until the user session is released. You just have to unset() the form key on the first valid request. The second request has to wait until the first one releases the session.

    However, when running in a (not session or source ip based) load balancing scenario involving multiple hosts things are getting more complicated. For such a scenario, i’m sure you will find a valuable solution in this great paper: http://thwartedefforts.org/2006/11/11/race-conditions-with-ajax-and-php-sessions/

    I reproduced your use case with the following demonstration. just throw this file onto your webserver and test it:

    <?php
    session_start();
    if (isset($_REQUEST['do_stuff'])) {
      // do stuff
      if ($_REQUEST['uniquehash'] == $_SESSION['uniquehash']) {
        echo "valid, doing stuff now ... "; flush();
        // delete formkey from session
        unset($_SESSION['uniquehash']);
        // release session early - after committing the session data is read-only
        session_write_close();
        sleep(20);  
        echo "stuff done!";
      }
      else {
        echo "nope, {$_REQUEST['uniquehash']} is invalid.";
      }     
    }
    else {
      // show form with formkey
      $_SESSION['uniquehash'] = md5("foo".microtime().rand(1,999999));
    ?>
    <html>
    <head><title>session race condition example</title></head>
    <body>
      <form method="POST">
        <input type="hidden" name="PHPSESSID" value="<?=session_id()?>">
        <input type="text" name="uniquehash" 
          value="<?= $_SESSION['uniquehash'] ?>">
        <input type="submit" name="do_stuff" value="Do stuff!">
      </form>
    </body>
    </html>
    <?php } ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that have the following need: use source ip address
So, I need use this event so I can navigate trought blog posts. I
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I need to use a C library to access and older file format used
I need to use the $.ajax() call within jQuery to post a little bit
I need to use this formula to calculate the upper limit and lower limit
I need to use a forward chainer in certain Prolog problem. I would like
I need to use cv::FindContours() in a program and I have to know the
I need to use recursion to for a method I'm trying to implement. The
I need to use a function to start and stop Tor via web interface.

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.