From 657350c603a870ec2c10a1df8044d46a747f35e5 Mon Sep 17 00:00:00 2001 From: "admin@arionum.com" Date: Wed, 21 Jul 2021 15:30:10 +0300 Subject: [PATCH] changed sanity lock mechanism to flock --- peer.php | 9 +++++++++ sanity.php | 26 +++++--------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/peer.php b/peer.php index 08e653c..2799531 100755 --- a/peer.php +++ b/peer.php @@ -23,6 +23,7 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +const SANITY_LOCK_PATH = __DIR__.'/tmp/sanity-lock'; require_once __DIR__.'/include/init.inc.php'; header('Content-Type: application/json'); @@ -164,6 +165,14 @@ if ($q == "peer") { _log('['.$ip."] Block rejected due to sanity sync"); api_err("sanity-sync"); } + $sanity_lock=fopen(SANITY_LOCK_PATH,'w+'); + if(!flock($sanity_lock, LOCK_EX | LOCK_NB)){ + _log('['.$ip."] Block rejected due to sanity sync"); + api_err("sanity-sync"); + } + // make sure sanity doesn't start while we add a new block and no other blocks are added at the same time + flock($sanity_lock, LOCK_EX); + $data['id'] = san($data['id']); $current = $block->current(); // block already in the blockchain diff --git a/sanity.php b/sanity.php index 567fa5c..4d63012 100755 --- a/sanity.php +++ b/sanity.php @@ -37,29 +37,13 @@ if (php_sapi_name() !== 'cli') { require_once __DIR__.'/include/init.inc.php'; // make sure there's only a single sanity process running at the same time -if (file_exists(SANITY_LOCK_PATH)) { - $ignore_lock = false; - if ($argv[1] == "force") { - $res = intval(shell_exec("ps aux|grep sanity.php|grep -v grep|wc -l")); - if ($res == 1) { - $ignore_lock = true; - } - } - $pid_time = filemtime(SANITY_LOCK_PATH); - - // If the process died, restart after 60 times the sanity interval - if (time() - $pid_time > ($_config['sanity_interval'] * 60 ?? 900 * 60)) { - @unlink(SANITY_LOCK_PATH); - } - - if (!$ignore_lock) { - die("Sanity lock in place".PHP_EOL); - } +$sanity_lock=fopen(SANITY_LOCK_PATH,'w+'); +if(!flock($sanity_lock, LOCK_EX | LOCK_NB)){ + die("Sanity lock in place".PHP_EOL); } - // set the new sanity lock -$lock = fopen(SANITY_LOCK_PATH, "w"); -fclose($lock); +flock($sanity_lock, LOCK_EX); + $arg = trim($argv[1]); $arg2 = trim($argv[2]); echo "Sleeping for 3 seconds\n";