private async Task <InitResult> InitCoreAsync()
        {
            using Stopwatch _ = m_counters.StartStopwatch(SandboxedProcessFactory.SandboxedProcessCounters.SandboxedPipExecutorInitializingRemoteProcessManager);

            AnyBuildClient abClient;

            using (m_counters.StartStopwatch(SandboxedProcessFactory.SandboxedProcessCounters.SandboxedPipExecutorRemoteProcessManagerFindAnyBuild))
            {
                try
                {
                    Tracing.Logger.Log.FindAnyBuildClient(m_loggingContext, EngineEnvironmentSettings.AnyBuildInstallDir ?? "default");
                    abClient = AnyBuildClient.Find(EngineEnvironmentSettings.AnyBuildInstallDir);
                }
                catch (AnyBuildNotInstalledException e)
                {
                    throw new BuildXLException("Failed to remote process because AnyBuild client cannot be found", e);
                }
            }

            Contract.Assert(abClient != null);

            AnyBuildDaemonManager daemonManager;

            using (m_counters.StartStopwatch(SandboxedProcessFactory.SandboxedProcessCounters.SandboxedPipExecutorRemoteProcessManagerStartAnyBuildDaemon))
            {
                try
                {
                    string logDir      = m_configuration.Logging.LogsDirectory.ToString(m_executionContext.PathTable);
                    string extraParams = CreateAnyBuildParams();

                    Tracing.Logger.Log.FindOrStartAnyBuildDaemon(m_loggingContext, extraParams, logDir);

                    daemonManager = await abClient.FindOrStartAnyBuildDaemonAsync(
                        closeDaemonOnDispose : true,
                        m_executionContext.CancellationToken,
                        logDirectory : logDir,
                        additionalAnyBuildParameters : extraParams,
                        // TODO: Use available ports instead of the defaults. It may address the issue with /server-.
                        // daemonPort: GetUnusedPort(),
                        // shimPort: GetUnusedPort(),
                        inheritHandlesOnProcessCreation : false);
                }
                catch (Exception e)
                {
                    Tracing.Logger.Log.ExceptionOnFindOrStartAnyBuildDaemon(m_loggingContext, e.ToString());

                    throw new BuildXLException("Failed to remote process because AnyBuild daemon cannot be found or started");
                }
            }

            m_isDaemonStarted = true;

            IRemoteProcessFactory remoteProcessFactory;

            using (m_counters.StartStopwatch(SandboxedProcessFactory.SandboxedProcessCounters.SandboxedPipExecutorRemoteProcessManagerGetAnyBuildRemoteFactory))
            {
                try
                {
                    remoteProcessFactory = abClient.GetRemoteProcessFactory();
                }
                catch (Exception e)
                {
                    Tracing.Logger.Log.ExceptionOnGetAnyBuildRemoteProcessFactory(m_loggingContext, e.ToString());

                    throw new BuildXLException("Failed to remote process because AnyBuild remote process factory cannot be obtained");
                }
            }

            IsInitialized = true;

            return(new InitResult(abClient, daemonManager, remoteProcessFactory));
        }
 private record InitResult(AnyBuildClient AbClient, AnyBuildDaemonManager DaemonManager, IRemoteProcessFactory RemoteProcessFactory);