Skip to content

Commit 6eedd36

Browse files
committed
Trace2FileWriter: ignore some exceptions on write
These exceptions were discovered while exploring trace2 settings with a full Git client. Git can take a directory location as a trace2 target and will create new files for every process. GCM currently throws a DirectoryNotFoundException given such a parameter. Git processes somehow can append to the same file across multiple subprocesses. When GCM attempts to append to the file, it gets an UnauthorizedAccessException on Windows due to multiple writers being problematic. This exception could also happen on other platforms if the setting is pointing to a file with restricted permissions. In both of these cases, we chose to do nothing. The traces are lost, but that's better than crashing the process. Future directions could include: 1. Sending a warning over stderr if these exceptions occur, to make it clear why trace2 are not showing up. 2. Directories could be noticed as a different kind of trace target and we create a new file for the process before passing it to the Trace2FileWriter. 3. Perhaps there is a way for Git to pass the handle to the trace file so we can append to the file that Git was using. (Alternatively, Git could close the file and then reopen it after running the GCM subprocess.) Each of these issues are a bit complicated, so this quick fix is chosen as a stop gap to avoid this problem for current users.
1 parent d6035ef commit 6eedd36

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/shared/Core/Trace2FileWriter.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ public Trace2FileWriter(Trace2FormatTarget formatTarget, string path) : base(for
1313

1414
public override void Write(Trace2Message message)
1515
{
16-
File.AppendAllText(_path, Format(message));
16+
try
17+
{
18+
File.AppendAllText(_path, Format(message));
19+
}
20+
catch (DirectoryNotFoundException dnfe)
21+
{
22+
// Do nothing, as this either means we don't have the
23+
// parent directories above the file, or this trace2
24+
// target points to a directory.
25+
}
26+
catch (UnauthorizedAccessException uae)
27+
{
28+
// Do nothing, as this either means the file is not
29+
// accessible with current permissions, or we are on
30+
// Windows and the file is currently open for writing
31+
// by another process (likely Git itself.)
32+
}
1733
}
1834
}

0 commit comments

Comments
 (0)