2

This is more of a question about best practices. My server allows users to convert fonts from one format to another using FontForge. To prevent collisions, only one font conversion is allowed at any given instance.

When one user initiates a conversion, a PID file is created which acts as a lock. If another user tries to initiate a conversion while the first conversion is still running, then the script will pause for a moment and check for the PID file again.

This repeats until the first process removes the PID file, thus unlocking access to FontForge. However, if the first user cancels the script before it finishes (by pressing the stop button on their browser), then the script exits before the PID file is removed. The second user will never be able to proceed.

What is the best way to handle this?

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
David Jones
  • 10,117
  • 28
  • 91
  • 139

2 Answers2

1

Stop watching for the PID file to go away. Start using flock so the operating system will clean up the lock if you exit prematurely.

Example code from http://tuxradar.com/practicalphp/8/11/undefined:

<?php
    $fp = fopen("foo.txt", "w");
    if (flock($fp, LOCK_EX)) {
        print "Got lock!\n";
        sleep(10);
        flock($fp, LOCK_UN);
    }
?>

Also, http://php.net/manual/en/function.flock.php.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

Looking at this PHP Man page:

http://php.net/manual/en/features.connection-handling.php

I quote:

You can decide whether or not you want a client disconnect to cause your script to be aborted. Sometimes it is handy to always have your scripts run to completion even if there is no remote browser receiving the output. The default behaviour is however for your script to be aborted when the remote client disconnects. This behaviour can be set via the ignore_user_abort php.ini directive as well as through the corresponding php_value ignore_user_abort Apache httpd.conf directive or with the ignore_user_abort() function. If you do not tell PHP to ignore a user abort and the user aborts, your script will terminate.

The one exception is if you have registered a shutdown function using register_shutdown_function(). With a shutdown function, when the remote user hits his STOP button, the next time your script tries to output something PHP will detect that the connection has been aborted and the shutdown function is called. This shutdown function will also get called at the end of your script terminating normally, so to do something different in case of a client disconnect you can use the connection_aborted() function. This function will return TRUE if the connection was aborted.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104