Skip to content

Commit

Permalink
Wait for debugger to attach in Runner.Worker process (actions#1101)
Browse files Browse the repository at this point in the history
* Wait for debugger to attach in Worker

Only if GITHUB_ACTIONS_RUNNER_ATTACH_DEBUGGER is set

* Only wait if env variable parses to boolean 'true'

* Add 30s timeout to the wait

* Clean up leftover line

* Decrease wait to 20s

* Use ConvertToBoolean isntead of TryParse
  • Loading branch information
fhammerl authored May 27, 2021
1 parent 8863b1f commit 93ec16e
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Runner.Worker/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using GitHub.Runner.Common.Util;
using System;
using System;
using System.Globalization;
using System.Threading.Tasks;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;
using System.Diagnostics;

namespace GitHub.Runner.Worker
{
Expand All @@ -19,11 +19,16 @@ public static int Main(string[] args)

public static async Task<int> MainAsync(IHostContext context, string[] args)
{
Tracing trace = context.GetTrace(nameof(GitHub.Runner.Worker));
if (StringUtil.ConvertToBoolean(Environment.GetEnvironmentVariable("GITHUB_ACTIONS_RUNNER_ATTACH_DEBUGGER")))
{
await WaitForDebugger(trace);
}

// We may want to consider registering this handler in Worker.cs, similiar to the unloading/SIGTERM handler
//ITerminal registers a CTRL-C handler, which keeps the Runner.Worker process running
//and lets the Runner.Listener handle gracefully the exit.
var term = context.GetService<ITerminal>();
Tracing trace = context.GetTrace(nameof(GitHub.Runner.Worker));
try
{
trace.Info($"Version: {BuildConstants.RunnerPackage.Version}");
Expand Down Expand Up @@ -64,5 +69,25 @@ public static async Task<int> MainAsync(IHostContext context, string[] args)

return 1;
}



/// <summary>
/// Runner.Worker is started by Runner.Listener in a separate process,
/// so the two can't be debugged in the same session.
/// This method halts the Runner.Worker process until a debugger is attached,
/// allowing a developer to debug Runner.Worker from start to finish.
/// </summary>
private static async Task WaitForDebugger(Tracing trace)
{
trace.Info($"Waiting for a debugger to be attached. Edit the 'GITHUB_ACTIONS_RUNNER_ATTACH_DEBUGGER' environment variable to toggle this feature.");
int waitInSeconds = 20;
while (!Debugger.IsAttached && waitInSeconds-- > 0)
{
trace.Info($"Waiting for a debugger to be attached. {waitInSeconds} seconds left.");
await Task.Delay(1000);
}
Debugger.Break();
}
}
}

0 comments on commit 93ec16e

Please sign in to comment.