[Box Backup-commit] COMMIT r3560 - box/trunk/lib/common

subversion at boxbackup.org subversion at boxbackup.org
Mon May 4 19:56:53 BST 2015


Author: chris
Date: 2015-05-04 18:56:53 +0000 (Mon, 04 May 2015)
New Revision: 3560

Modified:
   box/trunk/lib/common/NamedLock.cpp
Log:
Fix lock race condition by unlinking before closing the file handle.

Modified: box/trunk/lib/common/NamedLock.cpp
===================================================================
--- box/trunk/lib/common/NamedLock.cpp	2015-05-04 18:56:48 UTC (rev 3559)
+++ box/trunk/lib/common/NamedLock.cpp	2015-05-04 18:56:53 UTC (rev 3560)
@@ -207,30 +207,30 @@
 	{
 		THROW_EXCEPTION(CommonException, NamedLockNotHeld)
 	}
-	
-	// Close the file
-	if(::close(mFileDescriptor) != 0)
+
+	// Delete the file. We need to do this before closing the filehandle, otherwise
+	// someone could acquire the lock, release and delete it between us closing (and
+	// hence releasing) and deleting it, and we'd fail when it came to deleting the
+	// file. This happens in tests much more often than you'd expect!
+
+	if(::unlink(mFileName.c_str()) != 0)
 	{
 		THROW_EMU_ERROR(
-			BOX_FILE_MESSAGE(mFileName, "Failed to close lockfile"),
+			BOX_FILE_MESSAGE(mFileName, "Failed to delete lockfile"),
 			CommonException, OSFileError);
 	}
 
-	// Delete the file
-	if(::unlink(mFileName.c_str()) != 0)
+	// Close the file
+	if(::close(mFileDescriptor) != 0)
 	{
 		THROW_EMU_ERROR(
-			BOX_FILE_MESSAGE(mFileName,
-				"Failed to delete lockfile"),
+			BOX_FILE_MESSAGE(mFileName, "Failed to close lockfile"),
 			CommonException, OSFileError);
 	}
 
 	// Mark as unlocked, so we don't try to close it again if the unlink() fails.
 	mFileDescriptor = -1;
-}
 
-
-
 	BOX_TRACE("Released lock and deleted lockfile " << mFileName);
 }
 




More information about the Boxbackup-commit mailing list