fix: dispatch lifecycle exit through live document

This commit is contained in:
codex
2026-08-03 17:23:44 +03:00
parent 81a075e684
commit edf1640c4f
2 changed files with 10 additions and 15 deletions
@@ -330,22 +330,17 @@ internal sealed class FirstLimitedApplyAutomationCoordinator
{
if (exitHandlerSubscribed) AcadApplication.Idle -= OnExitIdle;
exitHandlerSubscribed = false;
// LifecycleProbe deliberately opens no drawing. It is already
// executing in the first verified Idle callback, where the managed
// quit API has been runtime-confirmed to terminate AutoCAD cleanly.
if (document is null && string.Equals(config?.Phase, "LifecycleProbe", StringComparison.OrdinalIgnoreCase))
{
transitions.Add("LifecycleProbeApplicationQuitRequested");
TryWriteEvidence(state == CoordinatorState.Completed, null);
AcadApplication.Quit();
return;
}
if (document is null) throw new InvalidOperationException("AUTOMATION_EXIT_DOCUMENT_UNAVAILABLE");
// LifecycleProbe opens no fixture document, but the real startup
// document is available at this point (observed as Drawing1.dwg).
// Use that live document only to queue native QUIT; never retain it
// past this callback or use it for fixture/Plant work.
var exitDocument = document ?? AcadApplication.DocumentManager.MdiActiveDocument;
if (exitDocument is null) throw new InvalidOperationException("AUTOMATION_EXIT_DOCUMENT_UNAVAILABLE");
transitions.Add("NativeQuitCommandQueued");
// Persist the exit boundary before handing control back to AutoCAD;
// process termination can otherwise prevent the final evidence flush.
TryWriteEvidence(state == CoordinatorState.Completed, null);
document.SendStringToExecute("_.QUIT _N ", true, false, false);
exitDocument.SendStringToExecute("_.QUIT _N ", true, false, false);
document = null;
automationOpenedDocuments.Clear();
}