< Summary

Information
Class: InferenceEngine.Core.OutOfProcessBaseInferenceEngine<T1, T2>
Assembly: InferenceEngine.Core
File(s): /home/runner/work/inference-engine-lib/inference-engine-lib/InferenceEngine.Core/OutOfProcessBaseInferenceEngine.cs
Line coverage
93%
Covered lines: 215
Uncovered lines: 15
Coverable lines: 230
Total lines: 514
Line coverage: 93.4%
Branch coverage
86%
Covered branches: 86
Total branches: 100
Branch coverage: 86%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
CreateSerializer()100%11100%
get_ModelName()100%11100%
LoadAsync()100%22100%
LoadLockedAsync()100%11100%
LoadLockedCoreAsync()100%1212100%
PredictAsync()100%1010100%
StartWorker()81.25%161695.23%
TryGetExitCode(...)50%5466.66%
TransitionStatus(...)100%11100%
HandleWorkerStderrLine(...)87.5%88100%
TryGetWorkerPid()100%2266.66%
SetWorkerStreamsForTesting(...)100%11100%
WriteAsync()100%11100%
ReadAsync()100%11100%
KillWorker()75%9880%
ThrowIfDisposed()50%22100%
DisposeAsync()100%44100%
SendShutdownFrameAsync()100%7675%
TearDownPipeAndProcess()91.66%131283.33%
Dispose(...)75%121286.66%
PreProcessWithContext(...)100%210%
PostProcessWithContext(...)100%210%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics;
 4using System.IO;
 5using System.Linq;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using InferenceEngine.Core.Diagnostics;
 9using InferenceEngine.Core.Worker;
 10using Microsoft.Extensions.Logging;
 11
 12namespace InferenceEngine.Core;
 13
 14/// <summary>
 15/// Host-side proxy that offers the same contract as <see cref="BaseInferenceEngine{TInput,TOutput}"/>
 16/// but runs the ONNX session in a <b>child worker process</b> over a length-prefixed JSON pipe.
 17/// The host never loads <c>libonnxruntime</c>/comgr; the worker never loads Mesa/GL — avoiding the
 18/// Mesa <c>libLLVM-19</c> ↔ comgr <c>libLLVM-20</c> collision that aborts an in-process MIGraphX +
 19/// GL-rendered UI. <see cref="BaseInferenceEngine{TInput,TOutput}"/>.EngineInfo is mirrored from the worker's Load resp
 20/// diagnostics surface (provider, fallback reason, ordered checks) is identical to the in-process engine.
 21/// </summary>
 22/// <remarks>
 23/// <para>
 24/// This base overrides <see cref="LoadAsync"/> and <see cref="PredictAsync"/> to marshal to the
 25/// worker; it never creates an in-process <see cref="Microsoft.ML.OnnxRuntime.InferenceSession"/>.
 26/// The consumer supplies the worker executable path (its own app launched with worker args) and an
 27/// <see cref="OutOfProcessEngineOptions"/>.
 28/// </para>
 29/// <para>
 30/// <b>Congcurrency (H1):</b> all load/predict IPC is serialized by <see cref="_inFlight"/> so that
 31/// concurrent calls never interleave write/read on the shared pipe (which would silently pair the
 32/// wrong request with the wrong response). <see cref="Dispose"/> (M6) also takes that lock so it
 33/// cannot tear down the pipe while a call is mid-flight.
 34/// </para>
 35/// </remarks>
 36public abstract class OutOfProcessBaseInferenceEngine<TInput, TOutput> : BaseInferenceEngine<TInput, TOutput>, IAsyncDis
 37{
 38    private readonly OutOfProcessEngineOptions _oopOptions;
 39    private readonly IWorkerSerializer<TInput, TOutput> _serializer;
 40
 41    // Serializes all host↔worker IPC so concurrent PredictAsync calls cannot interleave their
 42    // write/read pairs on the shared stdin/stdout pipe (H1). Dispose acquires it too (M6).
 2443    private readonly SemaphoreSlim _inFlight = new(1, 1);
 44
 45    // Serializes this proxy's status transitions with the stderr sniffing path
 46    // (HandleWorkerStderrLine): a compile marker racing a terminal transition must be dropped,
 47    // never regress Loaded/Failed back to Loading.
 2448    private readonly object _statusGate = new();
 49
 50    private Process? _process;
 51    private Stream? _toWorker;   // host → worker stdin
 52    private Stream? _fromWorker; // worker stdout → host
 53    private bool _workerLoaded;
 54    private bool _disposed;
 55
 56    protected OutOfProcessBaseInferenceEngine(OutOfProcessEngineOptions options, ILogger? logger = null)
 2457        : base(options.EngineOptions, logger)
 58    {
 2459        _oopOptions = options ?? throw new ArgumentNullException(nameof(options));
 2460        _serializer = CreateSerializer();
 2461    }
 62
 63    /// <summary>Serializer used for the IPC payload. Override to customize; defaults to
 64    /// <see cref="SystemTextJsonWorkerSerializer{TInput,TOutput}"/>.</summary>
 65    protected virtual IWorkerSerializer<TInput, TOutput> CreateSerializer()
 2466        => new SystemTextJsonWorkerSerializer<TInput, TOutput>();
 67
 68    // The model lives in the worker; the host proxy owns no model.
 169    protected override string ModelName => _oopOptions.EngineId;
 70
 71    /// <summary>
 72    /// Spawn the worker, send <c>load</c>, and mirror the worker's <see cref="BaseInferenceEngine{TInput,TOutput}"/>.En
 73    /// proxy via <see cref="BaseInferenceEngine{TInput,TOutput}.ApplyLoadedState"/>. Does NOT build an
 74    /// in-process session. Idempotent and thread-safe.
 75    /// </summary>
 76    public override async Task LoadAsync()
 77    {
 2878        await _inFlight.WaitAsync().ConfigureAwait(false);
 79        try
 80        {
 2981            if (_workerLoaded) return;
 2582            TransitionStatus(new InferenceEngineStatus { State = EngineLoadState.Loading, Detail = "Starting engine work
 2583            await LoadLockedAsync().ConfigureAwait(false);
 1384        }
 85        finally
 86        {
 2787            _inFlight.Release();
 88        }
 1589    }
 90
 91    /// <summary>
 92    /// Pre-loads the worker without holding a caller-visible lock re-entrancy. Assumes the caller
 93    /// already holds <see cref="_inFlight"/>. Spawns the worker (once), sends <c>load</c>, and mirrors
 94    /// the worker's diagnostics onto this proxy.
 95    /// </summary>
 96    private async Task LoadLockedAsync()
 97    {
 2698        ThrowIfDisposed();
 99        try
 100        {
 26101            await LoadLockedCoreAsync().ConfigureAwait(false);
 14102        }
 12103        catch (Exception ex) when (Status.State != EngineLoadState.Failed)
 104        {
 105            // Pipe-level failure (worker died, timeout, EOF): no worker EngineInfo exists, but
 106            // the failure still surfaces through the shared status object model.
 7107            TransitionStatus(new InferenceEngineStatus { State = EngineLoadState.Failed, LoadError = ex.Message });
 7108            throw;
 109        }
 14110    }
 111
 112    private async Task LoadLockedCoreAsync()
 113    {
 26114        var sw = Stopwatch.StartNew();
 26115        StartWorker();
 26116        _logger.LogInformation("oop {EngineId}: worker spawned in {Elapsed}ms (pid={Pid}); sending load",
 26117            _oopOptions.EngineId, sw.ElapsedMilliseconds, TryGetWorkerPid());
 118        // From here the worker owns the timeline (provider selection, session construction, the
 119        // ~1-2 min first-run MIGraphX kernel compile); stderr sniffing refines the Detail while
 120        // we wait for the load frame.
 26121        TransitionStatus(new InferenceEngineStatus { State = EngineLoadState.Loading, Detail = "Waiting for engine to lo
 122
 26123        var engineOpts = _oopOptions.EngineOptions;
 26124        await WriteAsync(new WorkerEnvelope
 26125        {
 26126            Kind = "load",
 26127            EngineId = _oopOptions.EngineId,
 26128            Options = new WorkerEngineOptions
 26129            {
 26130                ModelPath = engineOpts.ModelPath,
 26131                UseGpuAcceleration = engineOpts.UseGpuAcceleration,
 26132                DeviceId = engineOpts.DeviceId,
 26133                WarmupOnLoad = engineOpts.WarmupOnLoad,
 26134                MIGraphXModelCacheDirectory = engineOpts.MIGraphXModelCacheDirectory,
 26135            },
 26136        }, _oopOptions.StartupTimeout).ConfigureAwait(false);
 137
 25138        var loaded = await ReadAsync(_oopOptions.StartupTimeout).ConfigureAwait(false)
 25139            ?? throw new InvalidOperationException("Worker closed the pipe during load.");
 19140        _logger.LogInformation("oop {EngineId}: load response received after {Elapsed}ms (provider={Provider}, fallback=
 19141            _oopOptions.EngineId, sw.ElapsedMilliseconds, loaded.EngineInfo?.Provider, loaded.EngineInfo?.IsFallback, lo
 142
 19143        if (loaded.Error is not null)
 144        {
 1145            _logger.LogError("oop {EngineId}: worker load error: {Error}", _oopOptions.EngineId, loaded.Error);
 146            // Keep the worker's partial EngineInfo (checks run before the failure) in the status.
 1147            TransitionStatus(new InferenceEngineStatus
 1148            {
 1149                State = EngineLoadState.Failed,
 1150                Info = loaded.EngineInfo,
 1151                LoadError = loaded.Error,
 1152            });
 1153            throw new InvalidOperationException($"Worker failed to load the engine: {loaded.Error}");
 154        }
 18155        if (loaded.EngineInfo is null)
 156        {
 4157            _logger.LogError("oop {EngineId}: worker load response had no EngineInfo", _oopOptions.EngineId);
 4158            TransitionStatus(new InferenceEngineStatus
 4159            {
 4160                State = EngineLoadState.Failed,
 4161                LoadError = "Worker load response had no EngineInfo.",
 4162            });
 4163            throw new InvalidOperationException("Worker load response had no EngineInfo.");
 164        }
 165
 166        // Mirror the worker's loaded diagnostics. Metadata (NodeMetadata) is ORT-specific and not
 167        // serialized in this revision; it stays null on the proxy (a follow-up can carry name/dims).
 14168        ApplyLoadedState(loaded.EngineInfo, inputMetadata: null, outputMetadata: null, primaryInputName: null);
 14169        _workerLoaded = true;
 14170        TransitionStatus(new InferenceEngineStatus { State = EngineLoadState.Loaded, Info = loaded.EngineInfo });
 14171    }
 172
 173    /// <summary>Marshal a <c>predict</c> to the worker and return its deserialized result.</summary>
 174    public override async Task<TOutput> PredictAsync(TInput input)
 175    {
 176        // Serialize the whole operation (auto-load + write + read) so two concurrent callers each
 177        // pair their own request with their own response (H1). A second caller simply waits.
 10178        await _inFlight.WaitAsync().ConfigureAwait(false);
 179        try
 180        {
 8181            if (!_workerLoaded)
 1182                await LoadLockedAsync().ConfigureAwait(false);
 8183            ThrowIfDisposed();
 184
 8185            await WriteAsync(new WorkerEnvelope { Kind = "predict", Payload = _serializer.SerializeInput(input) },
 8186                _oopOptions.InferenceTimeout).ConfigureAwait(false);
 187
 8188            var result = await ReadAsync(_oopOptions.InferenceTimeout).ConfigureAwait(false)
 8189                ?? throw new InvalidOperationException("Worker closed the pipe during predict.");
 190
 5191            if (result.Error is not null)
 192            {
 1193                _logger.LogError("oop {EngineId}: worker predict failed: {Error}", _oopOptions.EngineId, result.Error);
 1194                throw new InvalidOperationException($"Worker predict failed: {result.Error}");
 195            }
 4196            if (result.Payload is null)
 1197                throw new InvalidOperationException("Worker predict returned no payload.");
 198
 3199            return _serializer.DeserializeOutput(result.Payload);
 200        }
 201        finally
 202        {
 8203            _inFlight.Release();
 204        }
 3205    }
 206
 207    // ---- plumbing -----------------------------------------------------------
 208
 209    /// <summary>
 210    /// Spawn the worker process and wire its stdin/stdout. Override in tests to bypass process
 211    /// creation and inject in-memory streams (call <see cref="SetWorkerStreamsForTesting"/>).
 212    /// </summary>
 213    protected virtual void StartWorker()
 214    {
 215        // Clean up any previously-started worker (e.g. a Load retry after a failure) BEFORE spawning
 216        // a new one, to avoid leaking worker processes / GPU memory.
 5217        if (_process is not null)
 218        {
 5219            try { if (!_process.HasExited) _process.Kill(); }
 0220            catch { /* best-effort */ }
 2221            _toWorker?.Dispose();
 2222            _fromWorker?.Dispose();
 2223            _process.Dispose();
 2224            _process = null;
 2225            _toWorker = null;
 2226            _fromWorker = null;
 227        }
 228
 5229        var psi = new ProcessStartInfo(_oopOptions.WorkerExecutablePath)
 5230        {
 5231            UseShellExecute = false,
 5232            RedirectStandardInput = true,
 5233            RedirectStandardOutput = true,
 5234            RedirectStandardError = true,
 5235            CreateNoWindow = true,
 5236        };
 22237        foreach (var arg in _oopOptions.WorkerArguments)
 6238            psi.ArgumentList.Add(arg);
 12239        foreach (var kv in _oopOptions.WorkerEnvironment)
 1240            psi.Environment[kv.Key] = kv.Value;
 241
 5242        _logger.LogInformation("oop {EngineId}: spawning worker exe={Exe} args={Args} envOverrides={Env}",
 5243            _oopOptions.EngineId, _oopOptions.WorkerExecutablePath,
 5244            string.Join(" ", _oopOptions.WorkerArguments),
 5245            _oopOptions.WorkerEnvironment.Count == 0
 5246                ? "<none>"
 6247                : string.Join(",", _oopOptions.WorkerEnvironment.Select(kv => $"{kv.Key}={kv.Value}")));
 248
 5249        _process = new Process { StartInfo = psi, EnableRaisingEvents = true };
 250        // Worker logs pass through to the host logger (stderr only; stdout is protocol-only).
 5251        _process.ErrorDataReceived += (_, e) =>
 5252        {
 7253            if (!string.IsNullOrEmpty(e.Data))
 3254                HandleWorkerStderrLine(e.Data);
 12255        };
 256        // An unexpected worker death mid-session is the classic "engine silently stops working"
 257        // signature: capture the exit code forensically. Uses the sender (not the _process field)
 258        // because teardown may null the field before the event fires.
 9259        _process.Exited += (sender, _) => _logger.LogWarning(
 9260            "oop {EngineId}: worker process exited (code={ExitCode})",
 9261            _oopOptions.EngineId, TryGetExitCode(sender as Process));
 262
 5263        if (!_process.Start())
 0264            throw new InvalidOperationException($"Failed to start worker process: {_oopOptions.WorkerExecutablePath}");
 265
 5266        _process.BeginErrorReadLine();
 5267        _toWorker = _process.StandardInput.BaseStream;
 5268        _fromWorker = _process.StandardOutput.BaseStream;
 5269    }
 270
 271    private static int TryGetExitCode(Process? process)
 272    {
 4273        try { return process is not null && process.HasExited ? process.ExitCode : -1; }
 0274        catch { return -1; }
 4275    }
 276
 277    /// <summary>Publishes a proxy status transition under <see cref="_statusGate"/> so it is
 278    /// ordered against <see cref="HandleWorkerStderrLine"/> (which may fire from the stderr
 279    /// pump thread at any point during the load read).</summary>
 280    private void TransitionStatus(InferenceEngineStatus status)
 281    {
 77282        lock (_statusGate)
 77283            SetStatus(status);
 77284    }
 285
 286    /// <summary>
 287    /// Single choke point for every worker stderr line: logs it, and while a load is in flight
 288    /// maps the native MIGraphX execution provider's compile markers onto the status object
 289    /// model (<see cref="InferenceEngineStatus.Detail"/>) so consumers can show what the slow
 290    /// first-run kernel compile is doing instead of a bare "loading". The markers are pinned
 291    /// against the ORT 1.24.1.36 ROCm native build (<c>libonnxruntime_providers_migraphx</c>:
 292    /// "Model Compile: Begin" / "Model Compile: Complete"); any other line is logged only.
 293    /// Virtual test seam — never spawns a process itself.
 294    /// </summary>
 295    protected virtual void HandleWorkerStderrLine(string line)
 296    {
 8297        _logger?.LogWarning("worker stderr: {Line}", line);
 298
 299        // Guarded by _statusGate together with TransitionStatus: a marker that races a terminal
 300        // transition is dropped, so Loaded/Failed can never regress back to Loading.
 8301        lock (_statusGate)
 302        {
 11303            if (Status.State != EngineLoadState.Loading) return;
 5304            if (line.Contains("Model Compile: Begin", StringComparison.Ordinal))
 3305                SetStatus(new InferenceEngineStatus
 3306                {
 3307                    State = EngineLoadState.Loading,
 3308                    Detail = "Compiling MIGraphX kernels (first run can take 1-2 minutes)",
 3309                });
 2310            else if (line.Contains("Model Compile: Complete", StringComparison.Ordinal))
 1311                SetStatus(new InferenceEngineStatus
 1312                {
 1313                    State = EngineLoadState.Loading,
 1314                    Detail = "Kernel compile complete",
 1315                });
 2316        }
 8317    }
 318
 319    private int? TryGetWorkerPid()
 320    {
 26321        try { return _process?.Id; }
 0322        catch { return null; }
 26323    }
 324
 325    /// <summary>Test seam: wire the host↔worker streams directly without spawning a process.</summary>
 326    internal void SetWorkerStreamsForTesting(Stream toWorker, Stream fromWorker)
 327    {
 20328        _toWorker = toWorker;
 20329        _fromWorker = fromWorker;
 20330    }
 331
 332    private async Task WriteAsync(WorkerEnvelope envelope, TimeSpan timeout)
 333    {
 34334        ThrowIfDisposed();
 335        // Race the write against a timeout. Redirected stdin pipes do not reliably honor
 336        // cancellation, so on timeout we kill the worker (closing the read end of stdin) to unblock
 337        // the pending write and then surface a TimeoutException (H2).
 338        // Task.WaitAsync(TimeSpan) avoids the Task.Delay timer leak that Task.WhenAny would cause on
 339        // this per-prediction hot path.
 34340        var writeTask = WorkerFrame.WriteAsync(_toWorker!, envelope, CancellationToken.None);
 341        try
 342        {
 34343            await writeTask.WaitAsync(timeout).ConfigureAwait(false);
 33344        }
 1345        catch (TimeoutException)
 346        {
 1347            _logger.LogError("oop {EngineId}: pipe write timed out after {Timeout}; killing worker",
 1348                _oopOptions.EngineId, timeout);
 1349            KillWorker();
 1350            throw new TimeoutException($"Worker did not accept a write within {timeout}.");
 351        }
 33352    }
 353
 354    private async Task<WorkerEnvelope?> ReadAsync(TimeSpan timeout)
 355    {
 33356        ThrowIfDisposed();
 357        // Redirected process stdout pipes do not promptly honor CancellationToken, so race the read
 358        // against a timeout; on expiry kill the worker and dispose the read stream to unblock the
 359        // pending read, then surface a TimeoutException (H2).
 360        // Task.WaitAsync(TimeSpan) avoids the Task.Delay timer leak that Task.WhenAny would cause on
 361        // this per-prediction hot path.
 33362        var readTask = WorkerFrame.ReadAsync(_fromWorker!, CancellationToken.None);
 363        try
 364        {
 33365            return await readTask.WaitAsync(timeout).ConfigureAwait(false);
 366        }
 4367        catch (TimeoutException)
 368        {
 4369            _logger.LogError("oop {EngineId}: pipe read timed out after {Timeout}; killing worker",
 4370                _oopOptions.EngineId, timeout);
 4371            KillWorker();
 4372            throw new TimeoutException($"Worker did not respond within {timeout}.");
 373        }
 28374    }
 375
 376    /// <summary>
 377    /// Forcefully terminates the worker process and disposes the host-side streams so any pending
 378    /// blocked read/write on the pipe unblocks. Best-effort; swallow teardown errors.
 379    /// </summary>
 380    private void KillWorker()
 381    {
 11382        try { if (_process is not null && !_process.HasExited) _process.Kill(); }
 0383        catch { /* best-effort */ }
 10384        try { _fromWorker?.Dispose(); } catch { /* best-effort */ }
 10385        try { _toWorker?.Dispose(); } catch { /* best-effort */ }
 5386    }
 387
 388    private void ThrowIfDisposed()
 389    {
 101390        if (_disposed) throw new ObjectDisposedException(nameof(OutOfProcessBaseInferenceEngine<TInput, TOutput>));
 101391    }
 392
 393    /// <summary>
 394    /// Asynchronously tears down the worker: sends a graceful <c>shutdown</c> frame without
 395    /// blocking, then disposes the pipe/process and base session. Prefer
 396    /// <c>await using</c> over the synchronous <see cref="Dispose"/> — the synchronous path cannot
 397    /// (and must not) perform a sync-over-async write.
 398    /// </summary>
 399    public virtual async ValueTask DisposeAsync()
 400    {
 8401        if (!_disposed)
 402        {
 7403            await _inFlight.WaitAsync().ConfigureAwait(false);
 404            try
 405            {
 7406                if (!_disposed)
 407                {
 7408                    _disposed = true;
 7409                    await SendShutdownFrameAsync().ConfigureAwait(false);
 7410                    TearDownPipeAndProcess();
 411                }
 7412            }
 413            finally
 414            {
 7415                _inFlight.Release();
 416            }
 7417            _inFlight.Dispose();
 418        }
 419
 8420        Dispose(disposing: true);
 8421        GC.SuppressFinalize(this);
 8422    }
 423
 424    /// <summary>Best-effort asynchronous graceful-shutdown frame, only when a live pipe + process exist.</summary>
 425    private async ValueTask SendShutdownFrameAsync()
 426    {
 427        try
 428        {
 7429            if (_toWorker is not null && _process is not null && !_process.HasExited)
 430            {
 431                try
 432                {
 1433                    await WorkerFrame.WriteAsync(_toWorker, new WorkerEnvelope { Kind = "shutdown" }, CancellationToken.
 1434                        .ConfigureAwait(false);
 1435                }
 0436                catch { /* best-effort */ }
 437            }
 7438        }
 0439        catch { /* swallow teardown errors */ }
 7440    }
 441
 442    /// <summary>
 443    /// Disposes the host side of the pipe and the worker process. Disposing stdin FIRST closes the
 444    /// worker's read end, which signals EOF and unblocks any pending stdout/stderr write so
 445    /// <see cref="Process.WaitForExit(int)"/> cannot deadlock; then the process is reaped/killed.
 446    /// </summary>
 447    private void TearDownPipeAndProcess()
 448    {
 40449        try { _toWorker?.Dispose(); } catch { /* best-effort */ }
 40450        try { _fromWorker?.Dispose(); } catch { /* best-effort */ }
 20451        _toWorker = null;
 20452        _fromWorker = null;
 453
 454        try
 455        {
 20456            if (_process is not null && !_process.HasExited)
 457            {
 1458                if (!_process.WaitForExit(2000))
 0459                    try { _process.Kill(); } catch { /* best-effort */ }
 460            }
 20461        }
 0462        catch { /* swallow teardown errors */ }
 463
 20464        _process?.Dispose();
 20465        _process = null;
 20466    }
 467
 468    protected override void Dispose(bool disposing)
 469    {
 22470        if (disposing && !_disposed)
 471        {
 472            // Take the in-flight lock (M6) so Dispose cannot tear down the pipe while a Load/Predict is
 473            // mid-write/read. In-flight IPC either completes or times out (H2) before releasing the lock.
 13474            _inFlight.Wait();
 475            try
 476            {
 13477                if (!_disposed)
 478                {
 13479                    _disposed = true;
 480                    // Best-effort graceful shutdown via the synchronous WorkerFrame.Write (added on
 481                    // main) — this avoids the sync-over-async .GetAwaiter().GetResult() that
 482                    // originally lived here, while still sending an explicit shutdown frame (better
 483                    // than relying on stdin EOF alone). Only sent when a live pipe + process exist;
 484                    // the async DisposeAsync path sends the same frame without blocking.
 485                    try
 486                    {
 13487                        if (_toWorker is not null && _process is not null && !_process.HasExited)
 0488                            WorkerFrame.Write(_toWorker, new WorkerEnvelope { Kind = "shutdown" });
 13489                    }
 0490                    catch { /* best-effort */ }
 13491                    TearDownPipeAndProcess();
 492                }
 13493            }
 494            finally
 495            {
 13496                _inFlight.Release();
 13497            }
 498
 13499            _inFlight.Dispose();
 500        }
 22501        base.Dispose(disposing);
 22502    }
 503
 504    // The one-shot pre/post hooks are unused: this engine marshals to the worker.
 505    protected sealed override (List<Microsoft.ML.OnnxRuntime.NamedOnnxValue> InputTensors, object? Context)
 506        PreProcessWithContext(TInput input) =>
 0507        throw new NotSupportedException(
 0508            "Out-of-process engines marshal Load/Predict to a worker; PreProcessWithContext is not used.");
 509    protected sealed override TOutput PostProcessWithContext(
 510        Microsoft.ML.OnnxRuntime.IDisposableReadOnlyCollection<Microsoft.ML.OnnxRuntime.DisposableNamedOnnxValue> output
 511        object? context) =>
 0512        throw new NotSupportedException(
 0513            "Out-of-process engines marshal Load/Predict to a worker; PostProcessWithContext is not used.");
 514}