Skip to content

Commit eff3666

Browse files
committed
Suppress expected lifecycle worker errors
Carry shared worker failure reasons through compilation so the launch readiness probe can fall back quietly when Unity advances its lifecycle generation. Keep genuine worker failures on the existing error reporting path.
1 parent 6d498b4 commit eff3666

6 files changed

Lines changed: 203 additions & 21 deletions

File tree

Assets/Tests/Editor/DynamicCodeToolTests/ExternalCompilerPathResolverTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,70 @@ public async Task CompileAsync_WhenSharedWorkerBuildFails_ShouldFallbackToOneSho
293293
}
294294
}
295295

296+
/// <summary>
297+
/// Verifies a lifecycle transition during worker startup silently falls back to one-shot Roslyn.
298+
/// </summary>
299+
[Test]
300+
public async Task CompileAsync_WhenWorkerLifecycleClosesDuringStartup_ShouldFallbackWithoutErrorLogs()
301+
{
302+
ExternalCompilerPaths externalCompilerPaths = ExternalCompilerPathResolver.Resolve();
303+
Assert.That(externalCompilerPaths, Is.Not.Null, "Unity external compiler layout should be available.");
304+
305+
string sourcePath = Path.Combine(_tempDirectoryPath, "LifecycleFallbackSmoke.cs");
306+
string dllPath = Path.Combine(_tempDirectoryPath, "LifecycleFallbackSmoke.dll");
307+
File.WriteAllText(
308+
sourcePath,
309+
"public static class LifecycleFallbackSmoke { public static int Execute() { return 11; } }");
310+
List<string> references = new DynamicReferenceSetBuilderService().BuildReferenceSet(
311+
new List<string>(),
312+
null,
313+
externalCompilerPaths);
314+
int buildCount = 0;
315+
bool buildStarted = false;
316+
bool buildFinished = false;
317+
318+
DynamicCompilationHealthMonitor.ResetForTests();
319+
SharedRoslynCompilerWorkerHost.ShutdownForTests();
320+
LogAssert.NoUnexpectedReceived();
321+
322+
Func<ExternalCompilerPaths, string, string, string, CompilerMessage[]> previousWorkerCompiler =
323+
SharedRoslynCompilerWorkerHost.SwapWorkerAssemblyCompilerForTests(
324+
(ExternalCompilerPaths paths, string workerSourcePath, string workerAssemblyPath, string workerCompileResponseFilePath) =>
325+
{
326+
SharedRoslynCompilerWorkerHost.ShutdownForTests();
327+
return Array.Empty<CompilerMessage>();
328+
});
329+
330+
try
331+
{
332+
using CancellationTokenSource compileCancellationTokenSource = new CancellationTokenSource();
333+
compileCancellationTokenSource.CancelAfter(FallbackCompileTimeoutMilliseconds);
334+
DynamicCompilationBackendResult result = await RoslynCompilerBackend.CompileAsync(
335+
sourcePath,
336+
dllPath,
337+
references,
338+
externalCompilerPaths,
339+
new RoslynCompilerOptions(Array.Empty<string>(), false),
340+
compileCancellationTokenSource.Token,
341+
() => buildStarted = true,
342+
() => buildFinished = true,
343+
() => buildCount++);
344+
345+
Assert.That(result, Is.Not.Null);
346+
Assert.That(result.BackendKind, Is.EqualTo(DynamicCompilationBackendKind.OneShotRoslyn));
347+
Assert.That(File.Exists(dllPath), Is.True);
348+
Assert.That(buildCount, Is.EqualTo(1));
349+
Assert.That(buildStarted, Is.True);
350+
Assert.That(buildFinished, Is.True);
351+
LogAssert.NoUnexpectedReceived();
352+
}
353+
finally
354+
{
355+
SharedRoslynCompilerWorkerHost.SwapWorkerAssemblyCompilerForTests(previousWorkerCompiler);
356+
SharedRoslynCompilerWorkerHost.ShutdownForTests();
357+
}
358+
}
359+
296360
[Test]
297361
public void ReportInfrastructureFallback_WhenCompilerPathLayoutIsKnown_ShouldIncludeLayoutKind()
298362
{

Assets/Tests/Editor/DynamicCodeToolTests/SharedRoslynCompilerWorkerHostTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using NUnit.Framework;
8+
using UnityEngine.TestTools;
89
using UnityEditor.Compilation;
910

1011
using io.github.hatayama.UnityCliLoop.FirstPartyTools;
@@ -17,6 +18,39 @@ namespace io.github.hatayama.UnityCliLoop.Tests.Editor.DynamicCodeToolTests
1718
[TestFixture]
1819
public class SharedRoslynCompilerWorkerHostTests
1920
{
21+
/// <summary>
22+
/// Verifies lifecycle closure is returned as a non-error compile outcome.
23+
/// </summary>
24+
[Test]
25+
public void SharedWorkerCompileOutcome_WhenLifecycleCloses_ShouldCarryFailureReasonWithoutErrorLog()
26+
{
27+
DynamicCompilationHealthMonitor.ResetForTests();
28+
LogAssert.NoUnexpectedReceived();
29+
30+
SharedWorkerCompileOutcome outcome = SharedWorkerCompileOutcome.Failed(
31+
SharedWorkerFailureReasons.LifecycleClosed,
32+
new { reason = "lifecycle_generation_advanced" });
33+
34+
Assert.That(outcome.Succeeded, Is.False);
35+
Assert.That(outcome.FailureReason, Is.EqualTo(SharedWorkerFailureReasons.LifecycleClosed));
36+
Assert.That(outcome.IsLifecycleClosed, Is.True);
37+
LogAssert.NoUnexpectedReceived();
38+
}
39+
40+
/// <summary>
41+
/// Verifies non-lifecycle worker failures retain their failure reason for error reporting.
42+
/// </summary>
43+
[Test]
44+
public void SharedWorkerCompileOutcome_WhenWorkerStartFails_ShouldCarryFailureReason()
45+
{
46+
SharedWorkerCompileOutcome outcome = SharedWorkerCompileOutcome.Failed(
47+
"worker_start_failed",
48+
new { reason = "process_start_failed" });
49+
50+
Assert.That(outcome.Succeeded, Is.False);
51+
Assert.That(outcome.FailureReason, Is.EqualTo("worker_start_failed"));
52+
}
53+
2054
[Test]
2155
public void ConfigureWorkerDotnetRuntimeEnvironment_WhenCalled_ShouldDisableMultilevelLookup()
2256
{

Packages/src/Editor/FirstPartyTools/ExecuteDynamicCode/Compilation/DynamicCompilationHealthMonitor.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public static void ReportSharedWorkerFailure(string reason, object context = nul
6363
"Investigate worker startup, request handling, and Unity-version-specific runtime assumptions.");
6464
}
6565

66+
public static void ReportSharedWorkerLifecycleClosed(object context = null)
67+
{
68+
LogInfoOnce(
69+
$"shared_worker_lifecycle_closed::{SharedWorkerFailureReasons.LifecycleClosed}",
70+
"dynamic_code_shared_worker_lifecycle_closed",
71+
"execute-dynamic-code shared Roslyn worker closed during an expected Unity lifecycle transition",
72+
context ?? new { reason = SharedWorkerFailureReasons.LifecycleClosed });
73+
}
74+
6675
public static void ReportOneShotCompilerStartFailure(object context)
6776
{
6877
LogErrorOnce(
@@ -101,6 +110,25 @@ private static void LogErrorOnce(
101110
Debug.LogError(FormatConsoleErrorMessage(operation, message, context));
102111
}
103112

113+
private static void LogInfoOnce(
114+
string issueKey,
115+
string operation,
116+
string message,
117+
object context)
118+
{
119+
string effectiveIssueKey = CreateEffectiveIssueKey(issueKey);
120+
121+
lock (ReportedIssueLock)
122+
{
123+
if (!ReportedIssues.Add(effectiveIssueKey))
124+
{
125+
return;
126+
}
127+
}
128+
129+
VibeLogger.LogInfo(operation, message, context);
130+
}
131+
104132
private static string CreateEffectiveIssueKey(string issueKey)
105133
{
106134
Debug.Assert(!string.IsNullOrEmpty(issueKey), "issueKey must not be empty");

Packages/src/Editor/FirstPartyTools/ExecuteDynamicCode/DynamicCompilation/RoslynCompilerBackend.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,33 @@ public static async Task<DynamicCompilationBackendResult> CompileAsync(
7171
defineSymbols,
7272
allowUnsafeCode);
7373

74-
CompilerMessage[] workerMessages = await SharedRoslynCompilerWorkerHost.TryCompileAsync(
74+
SharedWorkerCompileOutcome workerOutcome = await SharedRoslynCompilerWorkerHost.TryCompileAsync(
7575
workerRequestFilePath,
7676
externalCompilerPaths,
7777
ct,
7878
markBuildStarted,
7979
markBuildFinished,
8080
incrementBuildCount).ConfigureAwait(false);
81-
if (workerMessages != null)
81+
UnityEngine.Debug.Assert(workerOutcome != null, "Shared worker compile outcome must not be null");
82+
if (workerOutcome.Succeeded)
8283
{
8384
return new DynamicCompilationBackendResult(
84-
workerMessages,
85+
workerOutcome.Messages,
8586
DynamicCompilationBackendKind.SharedRoslynWorker);
8687
}
8788

88-
DynamicCompilationHealthMonitor.ReportSharedWorkerFallback(
89-
"worker_unavailable",
90-
new
91-
{
92-
platform = UnityEngine.Application.platform.ToString(),
93-
dotnet_host_path = externalCompilerPaths.DotnetHostPath,
94-
compiler_dll_path = externalCompilerPaths.CompilerDllPath,
95-
layout_kind = externalCompilerPaths.LayoutKind.ToString()
96-
});
89+
if (!workerOutcome.IsLifecycleClosed)
90+
{
91+
DynamicCompilationHealthMonitor.ReportSharedWorkerFallback(
92+
"worker_unavailable",
93+
new
94+
{
95+
platform = UnityEngine.Application.platform.ToString(),
96+
dotnet_host_path = externalCompilerPaths.DotnetHostPath,
97+
compiler_dll_path = externalCompilerPaths.CompilerDllPath,
98+
layout_kind = externalCompilerPaths.LayoutKind.ToString()
99+
});
100+
}
97101

98102
ct.ThrowIfCancellationRequested();
99103
OneShotCompileResult oneShotResult = await CompileWithOneShotAsync(

Packages/src/Editor/FirstPartyTools/ExecuteDynamicCode/DynamicCompilation/SharedRoslynCompilerWorkerHost.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static void RegisterLifecycleForEditorStartup()
2828
EditorApplication.quitting += ShutdownForQuit;
2929
}
3030

31-
public static Task<CompilerMessage[]> TryCompileAsync(
31+
public static Task<SharedWorkerCompileOutcome> TryCompileAsync(
3232
string requestFilePath,
3333
ExternalCompilerPaths externalCompilerPaths,
3434
CancellationToken ct,
@@ -47,7 +47,7 @@ public static Task<CompilerMessage[]> TryCompileAsync(
4747
ct);
4848
}
4949

50-
private static async Task<CompilerMessage[]> TryCompileWithRetriesAsync(
50+
private static async Task<SharedWorkerCompileOutcome> TryCompileWithRetriesAsync(
5151
string requestFilePath,
5252
ExternalCompilerPaths externalCompilerPaths,
5353
CancellationToken ct,
@@ -71,7 +71,7 @@ private static async Task<CompilerMessage[]> TryCompileWithRetriesAsync(
7171

7272
if (attemptResult.Succeeded)
7373
{
74-
return attemptResult.Messages;
74+
return SharedWorkerCompileOutcome.SucceededWith(attemptResult.Messages);
7575
}
7676

7777
ServiceValue.ExecuteWithStateLock(ServiceValue.ShutdownProcessLocked);
@@ -81,13 +81,24 @@ private static async Task<CompilerMessage[]> TryCompileWithRetriesAsync(
8181
continue;
8282
}
8383

84-
DynamicCompilationHealthMonitor.ReportSharedWorkerFailure(
85-
attemptResult.FailureReason,
86-
AppendAttempt(attemptResult.FailureContext, attempt));
87-
return null;
84+
object failureContext = AppendAttempt(attemptResult.FailureContext, attempt);
85+
if (attemptResult.FailureReason == SharedWorkerFailureReasons.LifecycleClosed)
86+
{
87+
DynamicCompilationHealthMonitor.ReportSharedWorkerLifecycleClosed(failureContext);
88+
}
89+
else
90+
{
91+
DynamicCompilationHealthMonitor.ReportSharedWorkerFailure(
92+
attemptResult.FailureReason,
93+
failureContext);
94+
}
95+
96+
return SharedWorkerCompileOutcome.Failed(attemptResult.FailureReason, failureContext);
8897
}
8998

90-
return null;
99+
return SharedWorkerCompileOutcome.Failed(
100+
"worker_unknown_failure",
101+
new { reason = "retry_loop_exhausted" });
91102
}
92103

93104
private static async Task<WorkerAttemptResult> TryCompileOnceAsync(

Packages/src/Editor/FirstPartyTools/ExecuteDynamicCode/DynamicCompilation/SharedRoslynCompilerWorkerHostResults.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,47 @@
22

33
namespace io.github.hatayama.UnityCliLoop.FirstPartyTools
44
{
5+
internal static class SharedWorkerFailureReasons
6+
{
7+
internal const string LifecycleClosed = "shared_worker_lifecycle_closed";
8+
}
9+
10+
/// <summary>
11+
/// Carries shared worker compilation messages and the reason for a failed compilation.
12+
/// </summary>
13+
internal sealed class SharedWorkerCompileOutcome
14+
{
15+
public CompilerMessage[] Messages { get; }
16+
17+
public string FailureReason { get; }
18+
19+
public object FailureContext { get; }
20+
21+
private SharedWorkerCompileOutcome(
22+
CompilerMessage[] messages,
23+
string failureReason,
24+
object failureContext)
25+
{
26+
Messages = messages;
27+
FailureReason = failureReason;
28+
FailureContext = failureContext;
29+
}
30+
31+
public bool Succeeded => Messages != null;
32+
33+
public bool IsLifecycleClosed => FailureReason == SharedWorkerFailureReasons.LifecycleClosed;
34+
35+
public static SharedWorkerCompileOutcome SucceededWith(CompilerMessage[] messages)
36+
{
37+
return new SharedWorkerCompileOutcome(messages, null, null);
38+
}
39+
40+
public static SharedWorkerCompileOutcome Failed(string failureReason, object failureContext)
41+
{
42+
return new SharedWorkerCompileOutcome(null, failureReason, failureContext);
43+
}
44+
}
45+
546
/// <summary>
647
/// Carries the result data produced by Worker Attempt behavior.
748
/// </summary>
@@ -85,7 +126,7 @@ public static WorkerStartupResult ClosedLifecycleFailure()
85126
return new WorkerStartupResult(
86127
false,
87128
false,
88-
"shared_worker_lifecycle_closed",
129+
SharedWorkerFailureReasons.LifecycleClosed,
89130
new { reason = "lifecycle_generation_advanced" });
90131
}
91132
}

0 commit comments

Comments
 (0)