Skip to content

Commit aec321a

Browse files
dlamannaclaude
andcommitted
fix: thread-safe logging to prevent IOException on concurrent writes
Log() opened the file with FileShare.Read, so concurrent calls from different threads (TCP handler, UI, retry timer) would throw IOException. Added a lock to serialize writes and changed to FileShare.ReadWrite. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1272fca commit aec321a

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

DesktopShell/GlobalVar.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,20 +1073,23 @@ public static bool KillProcess(string processName)
10731073
}
10741074
}
10751075

1076+
private static readonly object _logLock = new();
1077+
10761078
public static void Log(string logOutput)
10771079
{
10781080
string logPath = "DesktopShell.log";
1079-
try
1081+
lock (_logLock)
10801082
{
1081-
using FileStream fs = new(logPath, FileMode.Append, FileAccess.Write, FileShare.Read);
1082-
using StreamWriter w = new(fs);
1083+
try
10831084
{
1085+
using FileStream fs = new(logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
1086+
using StreamWriter w = new(fs);
10841087
w.WriteLine($"{DateTime.Now:HH:mm:ss.fff}:\t{logOutput}");
10851088
}
1086-
}
1087-
catch (UnauthorizedAccessException e)
1088-
{
1089-
Console.WriteLine(e.Message);
1089+
catch (Exception e) when (e is UnauthorizedAccessException or IOException)
1090+
{
1091+
Console.WriteLine(e.Message);
1092+
}
10901093
}
10911094
}
10921095

0 commit comments

Comments
 (0)