Skip to content

Commit d2ad6e8

Browse files
author
Jonathan Harper
committed
Fix parallel writes to UnitySDK.log on Windows
When using the SubprocessUnityEnvironment, parallel writes are made to UnitySDK.log. This causes file access violation issues in Windows/C#. This change modifies the access and sharing mode for our writes to UnitySDK.log to fix the issue.
1 parent 8ee84fe commit d2ad6e8

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

UnitySDK/Assets/ML-Agents/Scripts/Academy.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,13 @@ private void InitializeEnvironment()
346346
Random.InitState(pythonParameters.Seed);
347347
Application.logMessageReceived += HandleLog;
348348
logPath = Path.GetFullPath(".") + "/UnitySDK.log";
349-
logWriter = new StreamWriter(logPath, false);
350-
logWriter.WriteLine(System.DateTime.Now.ToString());
351-
logWriter.WriteLine(" ");
352-
logWriter.Close();
349+
using (var fs = File.Open(logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
350+
{
351+
logWriter = new StreamWriter(fs);
352+
logWriter.WriteLine(System.DateTime.Now.ToString());
353+
logWriter.WriteLine(" ");
354+
logWriter.Close();
355+
}
353356
}
354357

355358
// If a communicator is enabled/provided, then we assume we are in
@@ -386,11 +389,14 @@ private void UpdateResetParameters()
386389

387390
void HandleLog(string logString, string stackTrace, LogType type)
388391
{
389-
logWriter = new StreamWriter(logPath, true);
390-
logWriter.WriteLine(type.ToString());
391-
logWriter.WriteLine(logString);
392-
logWriter.WriteLine(stackTrace);
393-
logWriter.Close();
392+
using (var fs = File.Open(logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
393+
{
394+
logWriter = new StreamWriter(fs);
395+
logWriter.WriteLine(type.ToString());
396+
logWriter.WriteLine(logString);
397+
logWriter.WriteLine(stackTrace);
398+
logWriter.Close();
399+
}
394400
}
395401

396402
/// <summary>

0 commit comments

Comments
 (0)