Skip to content

Commit

Permalink
fix start up race condition for server
Browse files Browse the repository at this point in the history
  • Loading branch information
inorton committed Jun 1, 2014
1 parent 9c2bd0c commit b88b2f8
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 53 deletions.
47 changes: 30 additions & 17 deletions CClash/CClashServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ public sealed class CClashServer : IDisposable
public CClashServer()
{
Directory.SetCurrentDirectory(mydocs);

}

Mutex serverMutex;

public bool Preflight(string cachedir)
{
Logging.Emit("cclash server preflight check");
var mtx = new Mutex(false, "cclash_serv_" + cachedir.ToLower().GetHashCode());
serverMutex = mtx;
try
{
if (!mtx.WaitOne(1000))
{
quitnow = true;
Logging.Error("another server is already running");
return false; // some other process is holding it!
}
else
{
Logging.Emit("cclash server preflight ok");
}
}
catch (AbandonedMutexException)
{
Logging.Warning("previous instance did not exit cleanly!");
}
return true;
}

int busyThreads = 0;
Expand Down Expand Up @@ -158,21 +186,7 @@ void NewServerThread(string cachedir)
public void Listen(string cachedir)
{
Environment.CurrentDirectory = mydocs;
var mtx = new Mutex(false, "cclash_serv_" + cachedir.ToLower().GetHashCode());
try
{

if (!mtx.WaitOne(1000))
{
quitnow = true;
Logging.Error("another server is already running");
return; // some other process is holding it!
}
}
catch (AbandonedMutexException)
{
Logging.Warning("previous instance did not exit cleanly!");
}
Logging.Emit("creating direct cache server..");
cache = new DirectCompilerCacheServer(cachedir);
Logging.Emit("starting server threads..");

Expand Down Expand Up @@ -204,8 +218,7 @@ public void Listen(string cachedir)

cache.SetupStats();
Logging.Emit("server quitting");
mtx.ReleaseMutex();

serverMutex.ReleaseMutex();
}

public static string MakePipeName(string cachedir)
Expand Down
2 changes: 1 addition & 1 deletion CClash/CClashServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void Connect()
System.Threading.Thread.Sleep(1000);
ConnectClient();
} catch (Exception e) {
Logging.Emit("error starting cclash server process", e.Message);
Logging.Emit("error starting cclash server process {0}", e.ToString());
throw new CClashErrorException("could not start/connect to server");
}
}
Expand Down
2 changes: 2 additions & 0 deletions CClash/CacheInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public void Commit()
public CacheInfo(FileCacheStore statCache)
{
cache = statCache;
Logging.Emit("creating cache info mutex");
statMtx = new Mutex(false, "cclash_stat_" + cache.FolderPath.ToLower().GetHashCode());
Logging.Emit("created cache info mutex");
}


Expand Down
30 changes: 7 additions & 23 deletions CClash/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ public static string Find()
public Compiler()
{
compilerExe = "cl";
StdErrorText = new StringBuilder(1024);
StdOutputText = new StringBuilder(1024);
Created = DateTime.Now;
}

Expand Down Expand Up @@ -249,9 +247,6 @@ public string[] SourceFiles
public bool PdbExistsAlready { get; set; }
public string ResponseFile { get; set; }

public StringBuilder StdErrorText { get; private set; }
public StringBuilder StdOutputText { get; private set; }

public Action<string> StdErrorCallback { get; set; }
public Action<string> StdOutputCallback { get; set; }

Expand Down Expand Up @@ -806,11 +801,10 @@ public int InvokeCompiler(IEnumerable<string> args, Action<string> onStdErr, Act
}
else
{
if (onStdOut != null) onStdOut(a.Data);
lock (StdOutputText)
{
StdOutputText.AppendLine(a.Data);
}
if (StdOutputCallback != null)
StdOutputCallback(a.Data);
if (onStdOut != null)
onStdOut(a.Data);
}
}

Expand All @@ -820,20 +814,14 @@ public int InvokeCompiler(IEnumerable<string> args, Action<string> onStdErr, Act
{
if (a.Data != null)
{
if (StdErrorCallback != null)
StdErrorCallback(a.Data);
if (onStdErr != null)
{

onStdErr(a.Data);
}
lock (StdErrorText)
{
StdErrorText.AppendLine(a.Data);
}
}
};

p.BeginErrorReadLine();

p.BeginOutputReadLine();

p.WaitForExit();
Expand Down Expand Up @@ -861,11 +849,7 @@ public int InvokeCompiler(IEnumerable<string> args, Action<string> onStdErr, Act
{
string logmsg = string.Format("cl exited with zero but failed to create the object file! {0}", ObjectTarget);
// let the retry system have a go with this
Logging.Warning("stderr was {0}", StdErrorText.ToString());
if (onStdErr != null)
{
onStdErr(logmsg);
}

Logging.Warning("{0}, re-running!", logmsg);
retry = true;
}
Expand Down
9 changes: 8 additions & 1 deletion CClash/CompilerCacheBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ public abstract class CompilerCacheBase : IDisposable

ICacheInfo stats = null;

public CompilerCacheBase(string cacheFolder)
protected CompilerCacheBase()
{
}

public CompilerCacheBase(string cacheFolder) : this()
{
Logging.Emit("setting up file stores");
if (string.IsNullOrEmpty(cacheFolder)) throw new ArgumentNullException("cacheFolder");
outputCache = FileCacheStore.Load(Path.Combine(cacheFolder, "outputs"));
includeCache = FileCacheStore.Load(Path.Combine(cacheFolder, "includes"));
Logging.Emit("setup cache info");
stats = new CacheInfo(outputCache);
Logging.Emit("setup hasher");
hasher = new HashUtil(includeCache);
}

Expand Down
1 change: 1 addition & 0 deletions CClash/DirectCompilerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class DirectCompilerCache : CompilerCacheBase, ICompilerCache
public DirectCompilerCache(string cacheFolder)
: base(cacheFolder)
{
Logging.Emit("direct compiler cache");
}

public FileCacheStore OutputCache
Expand Down
1 change: 1 addition & 0 deletions CClash/DirectCompilerCacheServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DirectCompilerCacheServer(string cachedir)
{
SetupStats();
base.includeCache.CacheEntryChecksInMemory = true;
Logging.Emit("server locking cache data");
base.Lock(CacheLockType.ReadWrite); // base is a multi-process lock, keep this forever
}

Expand Down
8 changes: 7 additions & 1 deletion CClash/FileCacheStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void ReleaseMutex()
{
FolderPath = Path.GetFullPath(folderPath);
mtx = new Mutex(false, "cclash_mtx_" + FolderPath.ToLower().GetHashCode());

Logging.Emit("locking file store: {0}", FolderPath);
WaitOne();

var tlist = new List<Thread>();
Expand All @@ -57,12 +57,18 @@ public void ReleaseMutex()

if (bad_cache_format)
{
Logging.Emit("corrupt filestore, deleting: {0}", FolderPath);
// cache is too old, wiping
Directory.Delete(FolderPath, true);
Directory.CreateDirectory(FolderPath);
File.WriteAllText(Path.Combine(FolderPath, CacheInfo.F_CacheVersion), CacheInfo.CacheFormat);
}
}
Logging.Emit("filestore ready: {0}", FolderPath);
}
catch (IOException)
{
throw new CClashErrorException("could not clear cache!");
}
catch (UnauthorizedAccessException uae)
{
Expand Down
3 changes: 0 additions & 3 deletions CClash/ICompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public interface ICompiler
Action<string> StdErrorCallback { get; set; }
Action<string> StdOutputCallback { get; set; }

StringBuilder StdErrorText { get; }
StringBuilder StdOutputText { get; }

}
}

2 changes: 1 addition & 1 deletion CClash/NullCompilerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CClash {
public class NullCompilerCache : CompilerCacheBase , ICompilerCache {

public NullCompilerCache(string cachedir)
: base(cachedir) {
: base() {
}

public override void Setup() {
Expand Down
31 changes: 25 additions & 6 deletions CClash/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static int Main(string[] args)

if (args.Contains("--cclash-server"))
{
var server = new CClashServer();

if (args.Contains("--attempt-pdb"))
{
Environment.SetEnvironmentVariable("CCLASH_ATTEMPT_PDB_CACHE", "yes");
Expand All @@ -52,15 +52,34 @@ public static int Main(string[] args)
Environment.SetEnvironmentVariable("CCLASH_Z7_OBJ", "yes");
}

if (Settings.DebugEnabled)
{
if (Settings.DebugFile != null)
{
Settings.DebugFile += ".serv";
}
}

if (args.Contains("--debug")) {
if (Settings.DebugFile == null) {
Settings.DebugFile = "Console";
Settings.DebugEnabled = true;
}
}
Server = server;
server.Listen(Settings.CacheDirectory);
return 0;

Logging.Emit("starting in server mode");
Server = new CClashServer();
if (Server.Preflight(Settings.CacheDirectory))
{
Logging.Emit("server created");
Server.Listen(Settings.CacheDirectory);
return 0;
}
else
{
Logging.Emit("another server is running.. quitting");
return 1;
}
}

if (args.Contains("--cclash"))
Expand Down Expand Up @@ -144,12 +163,12 @@ public static int Main(string[] args)

static void AppendStderr(string str)
{
MainStdErr.AppendLine(str);
MainStdErr.AppendLine(str.TrimEnd('\n', '\r'));
}

static void AppendStdout(string str)
{
MainStdOut.AppendLine(str);
MainStdOut.AppendLine(str.TrimEnd('\n', '\r'));
}

private static int RunBuild(string[] args, DateTime start, Action<string> stdout, Action<string> stderr)
Expand Down
Binary file modified Installer/Installer/Express/DVD-5/DiskImages/DISK1/CClash.msi
Binary file not shown.
Binary file modified Installer/Installer/Express/DVD-5/DiskImages/DISK1/Setup.ini
Binary file not shown.
Binary file modified cclash.v11.suo
Binary file not shown.

0 comments on commit b88b2f8

Please sign in to comment.