What are the potential risks of using flock in PHP scripts to prevent simultaneous script execution?

When running PHP scripts that need to prevent simultaneous execution, using flock can help avoid race conditions and ensure only one instance of the script runs at a time. However, there are potential risks such as deadlocks or performance issues if the locking mechanism is not implemented correctly.

$lockFile = '/tmp/my_script.lock';
$lockHandle = fopen($lockFile, 'w');

if (flock($lockHandle, LOCK_EX | LOCK_NB)) {
    // Run your script code here

    flock($lockHandle, LOCK_UN); // Release the lock
} else {
    echo "Script is already running, please try again later.";
}

fclose($lockHandle);