diff --git a/phpBB/phpbb/config/config.php b/phpBB/phpbb/config/config.php
index aaad333006..036ae32cef 100644
--- a/phpBB/phpbb/config/config.php
+++ b/phpBB/phpbb/config/config.php
@@ -147,6 +147,27 @@ class config implements \ArrayAccess, \IteratorAggregate, \Countable
 		return false;
 	}
 
+	/**
+	* Checks configuration option's value only if the new_value matches the
+	* current configuration value and the configuration value does exist.Called
+	*only after set_atomic has been called.
+	*
+	* @param  string $key       The configuration option's name
+	* @param  string $old_value Current configuration value
+	* @param  string $new_value New configuration value
+	* @throws \phpbb\exception\http_exception when configuration value is set and not equal to *new_value.
+	* @return bool              True if the value was changed, false otherwise.
+	*/
+	public function ensure_lock($key, $new_value)
+	{
+		if(isset($this->config[$key]) && $this->config[$key] == $new_value)
+		{
+			return true;
+		} else {
+			throw new \phpbb\exception\http_exception(500, 'Failure while aqcuiring locks.');
+		}
+	}
+
 	/**
 	* Increments an integer configuration value.
 	*
diff --git a/phpBB/phpbb/lock/db.php b/phpBB/phpbb/lock/db.php
index 85ba9a7aa3..7765619422 100644
--- a/phpBB/phpbb/lock/db.php
+++ b/phpBB/phpbb/lock/db.php
@@ -110,7 +110,16 @@ class db
 		// process we failed to acquire the lock.
 		$this->locked = $this->config->set_atomic($this->config_name, $lock_value, $this->unique_id, false);
 
-		return $this->locked;
+		if ($this->locked == true)
+		{
+			if ($this->config->ensure_lock($this->config_name, $this->unique_id))
+			{
+				return true;
+			}
+		} else {
+
+			return $this->locked;
+		}
 	}
 
 	/**
diff --git a/phpBB/phpbb/lock/flock.php b/phpBB/phpbb/lock/flock.php
index df88e1490a..89ff7cbe48 100644
--- a/phpBB/phpbb/lock/flock.php
+++ b/phpBB/phpbb/lock/flock.php
@@ -101,7 +101,12 @@ class flock
 
 		if ($this->lock_fp)
 		{
-			@flock($this->lock_fp, LOCK_EX);
+			if (@flock($this->lock_fp, LOCK_EX))
+			{
+				return (bool) $this->lock_fp;
+			} else {
+				throw new \phpbb\exception\http_exception(500, 'Failure while aqcuiring locks.');
+			}
 		}
 
 		return (bool) $this->lock_fp;