Skip to content

Commit

Permalink
Try harder to make file locking succeed in case of network errors
Browse files Browse the repository at this point in the history
We noticed that during nightly builds, the LockFileEx() call would
occasionally(!) fail with ERROR_NETNAME_DELETED. The lockfile was
specified via an UNC path to a network drive, but the host was
available all the time. We couldn't find a reason why this would
happen and there was no obvious pattern to this either.

Let's just try to call LockFileEx() again in case this error occurs
(at least a couple of times), it might be that it's just a temporary
failure.
  • Loading branch information
Frerich Raabe committed Feb 25, 2014
1 parent 1d3127c commit 2504768
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions withlockfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,28 @@ int main( int argc, char **argv )
OVERLAPPED ol;
::ZeroMemory( &ol, sizeof( ol ) );

if ( !::LockFileEx( f, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ol ) ) {
throw Win32Error( "LockFileEx", ::GetLastError() );
/* For some unknown reason, LockFileEx fails with ERROR_NETNAME_DELETED
* every now and then. We couldn't determine the reason, let's just
* ignore this error for a while if it occurs - maybe it's some
* network instability?
*/
{
bool lockingSucceeded = false;
for ( int i = 0; i < 3; ++i ) {
if ( ::LockFileEx( f, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ol ) ) {
lockingSucceeded = true;
break;
}

const DWORD errorCode = ::GetLastError();
if ( errorCode != ERROR_NETNAME_DELETED ) {
throw Win32Error( "LockFileEx", errorCode );
}
}

if ( !lockingSucceeded ) {
throw Win32Error( "LockFileEx", ERROR_NETNAME_DELETED );
}
}

std::string executable = enforceExeExtension( argv[2] );
Expand Down

0 comments on commit 2504768

Please sign in to comment.