< Summary

Information
Class: InferenceEngine.Core.Worker.InferenceWorkerHost
Assembly: InferenceEngine.Core
File(s): /home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/Worker/InferenceWorkerHost.cs
Line coverage
100%
Covered lines: 76
Uncovered lines: 0
Coverable lines: 76
Total lines: 179
Line coverage: 100%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RunAsync()95.83%2424100%
HandleLoadAsync()100%22100%
HandlePredictAsync()50%22100%

File(s)

/home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/Worker/InferenceWorkerHost.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics;
 3using System.IO;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Microsoft.Extensions.Logging;
 7using Microsoft.Extensions.Logging.Abstractions;
 8
 9namespace InferenceEngine.Core.Worker;
 10
 11/// <summary>
 12/// The child-process message loop. The consumer's worker executable calls
 13/// <see cref="RunAsync{TInput,TOutput}"/> with a factory that builds ITS engine (a normal
 14/// <see cref="BaseInferenceEngine{TInput,TOutput}"/> — exactly like the headless
 15/// <c>--diag inference</c> path). The loop reads length-prefixed JSON requests on stdin and writes
 16/// responses on stdout; logging passes through on stderr. Because this process loads no
 17/// Avalonia/GL, comgr's LLVM is the only LLVM in it → no Mesa/comgr collision.
 18/// </summary>
 19public static class InferenceWorkerHost
 20{
 21    /// <param name="engineFactory">Builds the consumer's engine from the host-supplied options.</param>
 22    /// <param name="serializer">Optional serializer; defaults to <see cref="SystemTextJsonWorkerSerializer{TInput,TOutp
 23    /// <param name="input">Optional input stream (stdin). Tests inject a pipe; production uses console stdin.</param>
 24    /// <param name="output">Optional output stream (stdout). Tests inject a pipe; production uses console stdout.</para
 25    /// <param name="cancellationToken">Observed between requests.</param>
 26    /// <param name="logger">Optional logger for the worker loop itself (frame tracing, load
 27    /// outcomes, timings). stdout stays protocol-only; logger output goes to whatever sink the
 28    /// consumer configured (typically stderr, which the host proxy forwards).</param>
 29    public static async Task<int> RunAsync<TInput, TOutput>(
 30        Func<InferenceEngineOptions, BaseInferenceEngine<TInput, TOutput>> engineFactory,
 31        IWorkerSerializer<TInput, TOutput>? serializer = null,
 32        Stream? input = null,
 33        Stream? output = null,
 34        CancellationToken cancellationToken = default,
 35        ILogger? logger = null)
 36    {
 1137        var log = logger ?? NullLogger.Instance;
 1138        serializer ??= new SystemTextJsonWorkerSerializer<TInput, TOutput>();
 39
 40        // Redirect engine logging away from stdout (stdout is protocol-only); use stderr.
 1141        var stdin = input ?? Console.OpenStandardInput();
 1142        var stdout = output ?? Console.OpenStandardOutput();
 43
 44        // In production (no injected streams), redirect Console.Out to Console.Error so any stray
 45        // Console.WriteLine from managed code or libraries cannot corrupt the stdout IPC channel.
 46        // (When streams are injected — i.e. in tests — leave the console untouched.)
 1147        if (output is null)
 48        {
 149            Console.SetOut(Console.Error);
 50        }
 51        // Keep stdout free of any stray text writes — protocol frames only.
 52
 1153        log.LogInformation("worker loop: started (pid={Pid}, {Mode})",
 1154            Environment.ProcessId, output is null ? "console stdio" : "injected streams");
 55
 1156        BaseInferenceEngine<TInput, TOutput>? engine = null;
 57        try
 58        {
 2359            while (!cancellationToken.IsCancellationRequested)
 60            {
 61                WorkerEnvelope? msg;
 62                try
 63                {
 2364                    msg = await WorkerFrame.ReadAsync(stdin, cancellationToken).ConfigureAwait(false);
 2265                }
 166                catch (OperationCanceledException)
 67                {
 168                    log.LogInformation("worker loop: cancelled; exiting");
 169                    break;
 70                }
 2271                if (msg is null) // host closed stdin → exit
 72                {
 373                    log.LogInformation("worker loop: stdin closed by host; exiting");
 374                    break;
 75                }
 76
 1977                switch (msg.Kind)
 78                {
 79                    case "load":
 680                        engine = await HandleLoadAsync(engineFactory, serializer, msg, stdout, log, cancellationToken).C
 681                        break;
 582                    case "predict" when engine is not null:
 483                        await HandlePredictAsync(engine, serializer, msg, stdout, log, cancellationToken).ConfigureAwait
 484                        break;
 85                    case "shutdown":
 786                        log.LogInformation("worker loop: shutdown received; exiting 0");
 787                        return 0;
 88                    default:
 289                        log.LogWarning("worker loop: unknown or unexpected message kind '{Kind}'", msg.Kind);
 290                        await WorkerFrame.WriteAsync(stdout,
 291                            new WorkerEnvelope { Kind = "result", Error = $"Unknown or unexpected message kind: '{msg.Ki
 292                            cancellationToken).ConfigureAwait(false);
 93                        break;
 94                }
 95            }
 496        }
 97        finally
 98        {
 1199            (engine as IDisposable)?.Dispose();
 100        }
 4101        return 0;
 11102    }
 103
 104    private static async Task<BaseInferenceEngine<TInput, TOutput>?> HandleLoadAsync<TInput, TOutput>(
 105        Func<InferenceEngineOptions, BaseInferenceEngine<TInput, TOutput>> engineFactory,
 106        IWorkerSerializer<TInput, TOutput> serializer,
 107        WorkerEnvelope msg,
 108        Stream stdout,
 109        ILogger log,
 110        CancellationToken ct)
 111    {
 6112        var o = msg.Options ?? new WorkerEngineOptions();
 6113        var engineOptions = new InferenceEngineOptions
 6114        {
 6115            ModelPath = o.ModelPath,
 6116            UseGpuAcceleration = o.UseGpuAcceleration,
 6117            DeviceId = o.DeviceId,
 6118            WarmupOnLoad = o.WarmupOnLoad,
 6119            MIGraphXModelCacheDirectory = o.MIGraphXModelCacheDirectory,
 6120        };
 121
 6122        var sw = Stopwatch.StartNew();
 123        BaseInferenceEngine<TInput, TOutput> engine;
 124        try
 125        {
 6126            engine = engineFactory(engineOptions);
 5127        }
 128        catch (Exception ex)
 129        {
 1130            log.LogError(ex, "worker load: engine factory threw for engine '{EngineId}'", msg.EngineId);
 1131            await WorkerFrame.WriteAsync(stdout, new WorkerEnvelope { Kind = "loaded", Error = ex.ToString() }, ct).Conf
 1132            return null;
 133        }
 134
 135        try
 136        {
 5137            await engine.LoadAsync().ConfigureAwait(false); // real EP selection happens here
 4138            log.LogInformation("worker load: engine '{EngineId}' loaded in {Elapsed}ms (provider={Provider}, fallback={I
 4139                msg.EngineId, sw.ElapsedMilliseconds, engine.EngineInfo.Provider, engine.EngineInfo.IsFallback, engine.E
 4140            await WorkerFrame.WriteAsync(stdout, new WorkerEnvelope { Kind = "loaded", EngineInfo = engine.EngineInfo },
 4141        }
 1142        catch (Exception ex)
 143        {
 1144            log.LogError(ex, "worker load: engine '{EngineId}' LoadAsync failed after {Elapsed}ms",
 1145                msg.EngineId, sw.ElapsedMilliseconds);
 1146            await WorkerFrame.WriteAsync(stdout,
 1147                new WorkerEnvelope { Kind = "loaded", EngineInfo = engine.EngineInfo, Error = ex.ToString() },
 1148                ct).ConfigureAwait(false);
 149        }
 5150        return engine;
 6151    }
 152
 153    private static async Task HandlePredictAsync<TInput, TOutput>(
 154        BaseInferenceEngine<TInput, TOutput> engine,
 155        IWorkerSerializer<TInput, TOutput> serializer,
 156        WorkerEnvelope msg,
 157        Stream stdout,
 158        ILogger log,
 159        CancellationToken ct)
 160    {
 4161        var sw = Stopwatch.StartNew();
 162        try
 163        {
 4164            var input = serializer.DeserializeInput(msg.Payload ?? string.Empty);
 4165            var output = await engine.PredictAsync(input).ConfigureAwait(false);
 3166            log.LogDebug("worker predict: ok in {Elapsed}ms", sw.ElapsedMilliseconds);
 3167            await WorkerFrame.WriteAsync(stdout,
 3168                new WorkerEnvelope { Kind = "result", Payload = serializer.SerializeOutput(output) },
 3169                ct).ConfigureAwait(false);
 3170        }
 1171        catch (Exception ex)
 172        {
 1173            log.LogError(ex, "worker predict: failed after {Elapsed}ms", sw.ElapsedMilliseconds);
 1174            await WorkerFrame.WriteAsync(stdout,
 1175                new WorkerEnvelope { Kind = "result", Error = ex.ToString() },
 1176                ct).ConfigureAwait(false);
 177        }
 4178    }
 179}