Skip to content

Commit f7c9fd0

Browse files
authored
trace2: add child start and exit events (#1131)
This change adds the `child_start` and `child_exit` events to GCM's `TRACE2` tracing system. The first patches are either bug fixes or prep for the bigger refactors involved in making child start and exit work. The first patch puts sid-related logic into its own class, which allows us to immediately set up GCM's sid in `Program.cs` to ensure Git processes that are executed before `TRACE2` tracing is set up have the correct parent sid. The next patch adds a `FileWriter` class and uses it (rather than `Trace2StreamWriter`) to write to files, which ensures processes aren't overwriting one another's data. The third patch adds a snake casing string extension to ensure we write multi-word enum values according to the `TRACE2` convention. The fourth patch ensures we _do not_ redirect stderr for Git-related processes, as it was discovered to cause deadlocks when `TRACE2` was enabled. The fifth patch refactors GCM's diagnostics to depend on an instance of `CommandContext` rather than individual dependencies within `CommandContext`. The sixth patch re-configures `TRACE2` to depend on an instance of `CommandContext` rather than individual dependencies within `CommandContext`, to start up earlier, and for operations to no-op if `Trace2` has not yet been initialized (i.e. when child `git config` processes run before we're able to set up tracing). The seventh patch wraps `Process.cs` to give us the needed level of control to write `TRACE2` child process start and exit events. The eighth patch (finally) adds the new child start and exit events.
2 parents 6229602 + 336278c commit f7c9fd0

File tree

67 files changed

+936
-433
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+936
-433
lines changed

src/shared/Atlassian.Bitbucket.UI.Avalonia/Controls/TesterWindow.axaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public TesterWindow()
2424
#if DEBUG
2525
this.AttachDevTools();
2626
#endif
27-
2827
if (PlatformUtils.IsWindows())
2928
{
3029
_environment = new WindowsEnvironment(new WindowsFileSystem());

src/shared/Atlassian.Bitbucket.UI.Avalonia/Program.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ private static void AppMain(object o)
4545
{
4646
string[] args = (string[]) o;
4747

48-
using (var context = new CommandContext(args))
48+
// Set the session id (sid) for the helper process, to be
49+
// used when TRACE2 tracing is enabled.
50+
SidManager.CreateSid();
51+
using (var context = new CommandContext())
4952
using (var app = new HelperApplication(context))
5053
{
54+
// Initialize TRACE2 system
55+
context.Trace2.Initialize(DateTimeOffset.UtcNow);
56+
57+
// Write the start and version events
58+
context.Trace2.Start(context.ApplicationPath, args);
59+
5160
app.RegisterCommand(new CredentialsCommandImpl(context));
5261

5362
int exitCode = app.RunAsync(args)

src/shared/Core.Tests/GitConfigurationTests.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public void GitProcess_GetConfiguration_ReturnsConfiguration()
4848
string gitPath = GetGitPath();
4949
var trace = new NullTrace();
5050
var env = new TestEnvironment();
51-
var git = new GitProcess(trace, env, gitPath);
51+
var processManager = new TestProcessManager();
52+
var git = new GitProcess(trace, processManager, gitPath);
5253
var config = git.GetConfiguration();
5354
Assert.NotNull(config);
5455
}
@@ -71,7 +72,8 @@ public void GitConfiguration_Enumerate_CallbackReturnsTrue_InvokesCallbackForEac
7172
string gitPath = GetGitPath();
7273
var trace = new NullTrace();
7374
var env = new TestEnvironment();
74-
var git = new GitProcess(trace, env, gitPath, repoPath);
75+
var processManager = new TestProcessManager();
76+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
7577
IGitConfiguration config = git.GetConfiguration();
7678

7779
var actualVisitedEntries = new List<(string name, string value)>();
@@ -109,7 +111,8 @@ public void GitConfiguration_Enumerate_CallbackReturnsFalse_InvokesCallbackForEa
109111
string gitPath = GetGitPath();
110112
var trace = new NullTrace();
111113
var env = new TestEnvironment();
112-
var git = new GitProcess(trace, env, gitPath, repoPath);
114+
var processManager = new TestProcessManager();
115+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
113116
IGitConfiguration config = git.GetConfiguration();
114117

115118
var actualVisitedEntries = new List<(string name, string value)>();
@@ -139,7 +142,9 @@ public void GitConfiguration_TryGet_Name_Exists_ReturnsTrueOutString()
139142
string gitPath = GetGitPath();
140143
var trace = new NullTrace();
141144
var env = new TestEnvironment();
142-
var git = new GitProcess(trace, env, gitPath, repoPath);
145+
var processManager = new TestProcessManager();
146+
147+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
143148
IGitConfiguration config = git.GetConfiguration();
144149

145150
bool result = config.TryGet("user.name", false, out string value);
@@ -156,7 +161,8 @@ public void GitConfiguration_TryGet_Name_DoesNotExists_ReturnsFalse()
156161
string gitPath = GetGitPath();
157162
var trace = new NullTrace();
158163
var env = new TestEnvironment();
159-
var git = new GitProcess(trace, env, gitPath, repoPath);
164+
var processManager = new TestProcessManager();
165+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
160166
IGitConfiguration config = git.GetConfiguration();
161167

162168
string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
@@ -175,7 +181,8 @@ public void GitConfiguration_TryGet_IsPath_True_ReturnsCanonicalPath()
175181
string gitPath = GetGitPath();
176182
var trace = new NullTrace();
177183
var env = new TestEnvironment();
178-
var git = new GitProcess(trace, env, gitPath, repoPath);
184+
var processManager = new TestProcessManager();
185+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
179186
IGitConfiguration config = git.GetConfiguration();
180187

181188
bool result = config.TryGet("example.path", true, out string value);
@@ -193,7 +200,8 @@ public void GitConfiguration_TryGet_IsPath_False_ReturnsRawConfig()
193200
string gitPath = GetGitPath();
194201
var trace = new NullTrace();
195202
var env = new TestEnvironment();
196-
var git = new GitProcess(trace, env, gitPath, repoPath);
203+
var processManager = new TestProcessManager();
204+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
197205
IGitConfiguration config = git.GetConfiguration();
198206

199207
bool result = config.TryGet("example.path", false, out string value);
@@ -211,7 +219,8 @@ public void GitConfiguration_TryGet_BoolType_ReturnsCanonicalBool()
211219
string gitPath = GetGitPath();
212220
var trace = new NullTrace();
213221
var env = new TestEnvironment();
214-
var git = new GitProcess(trace, env, gitPath, repoPath);
222+
var processManager = new TestProcessManager();
223+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
215224
IGitConfiguration config = git.GetConfiguration();
216225

217226
bool result = config.TryGet(GitConfigurationLevel.Local, GitConfigurationType.Bool,
@@ -230,7 +239,8 @@ public void GitConfiguration_TryGet_BoolWithoutType_ReturnsRawConfig()
230239
string gitPath = GetGitPath();
231240
var trace = new NullTrace();
232241
var env = new TestEnvironment();
233-
var git = new GitProcess(trace, env, gitPath, repoPath);
242+
var processManager = new TestProcessManager();
243+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
234244
IGitConfiguration config = git.GetConfiguration();
235245

236246
bool result = config.TryGet(GitConfigurationLevel.Local, GitConfigurationType.Raw,
@@ -249,7 +259,8 @@ public void GitConfiguration_Get_Name_Exists_ReturnsString()
249259
string gitPath = GetGitPath();
250260
var trace = new NullTrace();
251261
var env = new TestEnvironment();
252-
var git = new GitProcess(trace, env, gitPath, repoPath);
262+
var processManager = new TestProcessManager();
263+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
253264
IGitConfiguration config = git.GetConfiguration();
254265

255266
string value = config.Get("user.name");
@@ -265,7 +276,8 @@ public void GitConfiguration_Get_Name_DoesNotExists_ThrowsException()
265276
string gitPath = GetGitPath();
266277
var trace = new NullTrace();
267278
var env = new TestEnvironment();
268-
var git = new GitProcess(trace, env, gitPath, repoPath);
279+
var processManager = new TestProcessManager();
280+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
269281
IGitConfiguration config = git.GetConfiguration();
270282

271283
string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
@@ -280,7 +292,8 @@ public void GitConfiguration_Set_Local_SetsLocalConfig()
280292
string gitPath = GetGitPath();
281293
var trace = new NullTrace();
282294
var env = new TestEnvironment();
283-
var git = new GitProcess(trace, env, gitPath, repoPath);
295+
var processManager = new TestProcessManager();
296+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
284297
IGitConfiguration config = git.GetConfiguration();
285298

286299
config.Set(GitConfigurationLevel.Local, "core.foobar", "foo123");
@@ -298,7 +311,8 @@ public void GitConfiguration_Set_All_ThrowsException()
298311
string gitPath = GetGitPath();
299312
var trace = new NullTrace();
300313
var env = new TestEnvironment();
301-
var git = new GitProcess(trace, env, gitPath, repoPath);
314+
var processManager = new TestProcessManager();
315+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
302316
IGitConfiguration config = git.GetConfiguration();
303317

304318
Assert.Throws<InvalidOperationException>(() => config.Set(GitConfigurationLevel.All, "core.foobar", "test123"));
@@ -317,7 +331,8 @@ public void GitConfiguration_Unset_Global_UnsetsGlobalConfig()
317331
string gitPath = GetGitPath();
318332
var trace = new NullTrace();
319333
var env = new TestEnvironment();
320-
var git = new GitProcess(trace, env, gitPath, repoPath);
334+
var processManager = new TestProcessManager();
335+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
321336
IGitConfiguration config = git.GetConfiguration();
322337

323338
config.Unset(GitConfigurationLevel.Global, "core.foobar");
@@ -348,7 +363,8 @@ public void GitConfiguration_Unset_Local_UnsetsLocalConfig()
348363
string gitPath = GetGitPath();
349364
var trace = new NullTrace();
350365
var env = new TestEnvironment();
351-
var git = new GitProcess(trace, env, gitPath, repoPath);
366+
var processManager = new TestProcessManager();
367+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
352368
IGitConfiguration config = git.GetConfiguration();
353369

354370
config.Unset(GitConfigurationLevel.Local, "core.foobar");
@@ -374,7 +390,8 @@ public void GitConfiguration_Unset_All_ThrowsException()
374390
string gitPath = GetGitPath();
375391
var trace = new NullTrace();
376392
var env = new TestEnvironment();
377-
var git = new GitProcess(trace, env, gitPath, repoPath);
393+
var processManager = new TestProcessManager();
394+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
378395
IGitConfiguration config = git.GetConfiguration();
379396

380397
Assert.Throws<InvalidOperationException>(() => config.Unset(GitConfigurationLevel.All, "core.foobar"));
@@ -391,7 +408,8 @@ public void GitConfiguration_UnsetAll_UnsetsAllConfig()
391408
string gitPath = GetGitPath();
392409
var trace = new NullTrace();
393410
var env = new TestEnvironment();
394-
var git = new GitProcess(trace, env, gitPath, repoPath);
411+
var processManager = new TestProcessManager();
412+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
395413
IGitConfiguration config = git.GetConfiguration();
396414

397415
config.UnsetAll(GitConfigurationLevel.Local, "core.foobar", "foo*");
@@ -409,7 +427,8 @@ public void GitConfiguration_UnsetAll_All_ThrowsException()
409427
string gitPath = GetGitPath();
410428
var trace = new NullTrace();
411429
var env = new TestEnvironment();
412-
var git = new GitProcess(trace, env, gitPath, repoPath);
430+
var processManager = new TestProcessManager();
431+
var git = new GitProcess(trace, processManager, gitPath, repoPath);
413432
IGitConfiguration config = git.GetConfiguration();
414433

415434
Assert.Throws<InvalidOperationException>(() => config.UnsetAll(GitConfigurationLevel.All, "core.foobar", Constants.RegexPatterns.Any));

src/shared/Core.Tests/GitTests.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public void Git_GetCurrentRepository_NoLocalRepo_ReturnsNull()
1414
string gitPath = GetGitPath();
1515
var trace = new NullTrace();
1616
var env = new TestEnvironment();
17-
var git = new GitProcess(trace, env, gitPath, Path.GetTempPath());
17+
var processManager = new TestProcessManager();
18+
var git = new GitProcess(trace, processManager, gitPath, Path.GetTempPath());
1819

1920
string actual = git.GetCurrentRepository();
2021

@@ -29,7 +30,8 @@ public void Git_GetCurrentRepository_LocalRepo_ReturnsNotNull()
2930
string gitPath = GetGitPath();
3031
var trace = new NullTrace();
3132
var env = new TestEnvironment();
32-
var git = new GitProcess(trace, env, gitPath, workDirPath);
33+
var processManager = new TestProcessManager();
34+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
3335

3436
string actual = git.GetCurrentRepository();
3537

@@ -42,7 +44,8 @@ public void Git_GetRemotes_NoLocalRepo_ReturnsEmpty()
4244
string gitPath = GetGitPath();
4345
var trace = new NullTrace();
4446
var env = new TestEnvironment();
45-
var git = new GitProcess(trace, env, gitPath, Path.GetTempPath());
47+
var processManager = new TestProcessManager();
48+
var git = new GitProcess(trace, processManager, gitPath, Path.GetTempPath());
4649

4750
GitRemote[] remotes = git.GetRemotes().ToArray();
4851

@@ -57,7 +60,8 @@ public void Git_GetRemotes_NoRemotes_ReturnsEmpty()
5760
string gitPath = GetGitPath();
5861
var trace = new NullTrace();
5962
var env = new TestEnvironment();
60-
var git = new GitProcess(trace, env, gitPath, workDirPath);
63+
var processManager = new TestProcessManager();
64+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
6165

6266
GitRemote[] remotes = git.GetRemotes().ToArray();
6367

@@ -75,8 +79,9 @@ public void Git_GetRemotes_OneRemote_ReturnsRemote()
7579
string gitPath = GetGitPath();
7680
var trace = new NullTrace();
7781
var env = new TestEnvironment();
82+
var processManager = new TestProcessManager();
7883

79-
var git = new GitProcess(trace, env, gitPath, workDirPath);
84+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
8085
GitRemote[] remotes = git.GetRemotes().ToArray();
8186

8287
Assert.Single(remotes);
@@ -96,8 +101,9 @@ public void Git_GetRemotes_OneRemoteFetchAndPull_ReturnsRemote()
96101
string gitPath = GetGitPath();
97102
var trace = new NullTrace();
98103
var env = new TestEnvironment();
104+
var processManager = new TestProcessManager();
99105

100-
var git = new GitProcess(trace, env, gitPath, workDirPath);
106+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
101107
GitRemote[] remotes = git.GetRemotes().ToArray();
102108

103109
Assert.Single(remotes);
@@ -119,8 +125,9 @@ public void Git_GetRemotes_NonHttpRemote_ReturnsRemote(string url)
119125
string gitPath = GetGitPath();
120126
var trace = new NullTrace();
121127
var env = new TestEnvironment();
128+
var processManager = new TestProcessManager();
122129

123-
var git = new GitProcess(trace, env, gitPath, workDirPath);
130+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
124131
GitRemote[] remotes = git.GetRemotes().ToArray();
125132

126133
Assert.Single(remotes);
@@ -144,8 +151,9 @@ public void Git_GetRemotes_MultipleRemotes_ReturnsAllRemotes()
144151
string gitPath = GetGitPath();
145152
var trace = new NullTrace();
146153
var env = new TestEnvironment();
154+
var processManager = new TestProcessManager();
147155

148-
var git = new GitProcess(trace, env, gitPath, workDirPath);
156+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
149157
GitRemote[] remotes = git.GetRemotes().ToArray();
150158

151159
Assert.Equal(3, remotes.Length);
@@ -168,8 +176,9 @@ public void Git_GetRemotes_RemoteNoFetchOnlyPull_ReturnsRemote()
168176
string gitPath = GetGitPath();
169177
var trace = new NullTrace();
170178
var env = new TestEnvironment();
179+
var processManager = new TestProcessManager();
171180

172-
var git = new GitProcess(trace, env, gitPath, workDirPath);
181+
var git = new GitProcess(trace, processManager, gitPath, workDirPath);
173182
GitRemote[] remotes = git.GetRemotes().ToArray();
174183

175184
Assert.Single(remotes);
@@ -183,8 +192,9 @@ public void Git_Version_ReturnsVersion()
183192
string gitPath = GetGitPath();
184193
var trace = new NullTrace();
185194
var env = new TestEnvironment();
195+
var processManager = new TestProcessManager();
186196

187-
var git = new GitProcess(trace, env, gitPath, Path.GetTempPath());
197+
var git = new GitProcess(trace, processManager, gitPath, Path.GetTempPath());
188198
GitVersion version = git.Version;
189199

190200
Assert.NotEqual(new GitVersion(), version);

0 commit comments

Comments
 (0)