Skip to content

VSCode (mac) debugger breakpoint on GetTensorDataAsSpan leads to app crash evaluating SpanDebugView #25023

Closed
@tuanchien

Description

@tuanchien

Describe the issue

Apologies if this is filed with the wrong project. I'm not sure which part of the ecosystem is the root cause.

This behaviour manifests on net9.0-maccatalyst with VSCode mac but does not seem to be reproducible on x64 net9.0-windows targets in VSCode or VSPro.

Setting a debug break point on a function that calls GetTensorDataAsSpan to access the OrtValue output of an InferenceSession.Run can lead to an app crash with the following crash message.

The target process exited with code 0 (0x00000000) while evaluating the function 'System.SpanDebugView<T>.SpanDebugView'.

If no break point is set, the code will produce results as expected.

More details will be provided in the section with instructions to reproduce the crash.

To reproduce

Machine details

M1 Mac. MacOS 15.5, XCode 16.4

VSCode details

Version: 1.100.3 (Universal)
Commit: 258e40fedc6cb8edf399a463ce3a9d32e7e1f6f3
Date: 2025-06-02T13:30:54.273Z (1 wk ago)
Electron: 34.5.1
ElectronBuildId: 11369351
Chromium: 132.0.6834.210
Node.js: 20.19.0
V8: 13.2.152.41-electron.0
OS: Darwin arm64 24.5.0

DotNet SDK

9.0.301 [/usr/local/share/dotnet/sdk]

Target

net9.0-maccatalyst arm64

Repo to reproduce problem
https://github.com/tuanchien/spandebugview-vscode-mac-onnxruntime-issue

[Alternatively]

Create a new project template with:

dotnet new maui

In the .csproj file, modify the TFM to

<TargetFrameworks>net9.0-maccatalyst</TargetFrameworks>

Add the following ProjectReferences

<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.22.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.Extensions.Dummy" Version="0.13.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.Managed" Version="1.22.0" />

Download the example MobileNet model from

https://github.com/microsoft/onnxruntime-inference-examples/blob/main/mobile/examples/Maui/MauiVisionSample/MauiVisionSample/Resources/Raw/mobilenetv2-12.onnx

putting this in your project's Resources/Raw directory. Rename it to model.onnx

In MainPage.xml.cs, modify the code to the following:

	private void OnCounterClicked(object? sender, EventArgs e)
	{
		RunOnnxStuff();

		SemanticScreenReader.Announce(CounterBtn.Text);
	}

	void RunOnnxStuff()
	{
		var env = OrtEnv.Instance();
		env.DisableTelemetryEvents();
		using SessionOptions sessionOptions = new()
		{
			GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL,
			EnableMemoryPattern = true,
			EnableProfiling = false,
			ExecutionMode = ExecutionMode.ORT_SEQUENTIAL,
			IntraOpNumThreads = 1,
		};

		using var session = new InferenceSession("Contents/Resources/model.onnx", sessionOptions);

		var inputLayerName = session.InputMetadata.Keys.Single();

		var outputNames = session.OutputMetadata.Keys.ToList();

		float[] sourceData = new float[224 * 224 * 3];
		long[] dimensions = [1, 3, 224, 224];
		using var inputOrtValue = OrtValue.CreateTensorValueFromMemory(sourceData, dimensions);

		var inputs = new Dictionary<string, OrtValue> {
			{ inputLayerName,  inputOrtValue }
		};

		using var runOptions = new RunOptions();
		using var output = session.Run(runOptions, inputs, outputNames);

		var output_0 = output[0];
		var outputData = output_0.GetTensorDataAsSpan<float>();
	}

Setting a break point anywhere in the RunOnnxStuff() method, including before GetTensorDataAsSpan is executed, will lead to an app crash.

To trigger the crash, build/launch the app in VSCode with a debugger attached, and click the button in the demo app.

Scenario where an attached debugger does NOT crash
Instead of having the onnx code run in RunOnnxStuff(), put it all in OnCounterClicked instead. The code might look like:

	private void OnCounterClicked(object? sender, EventArgs e)
	{
		var env = OrtEnv.Instance();
		env.DisableTelemetryEvents();
		using SessionOptions sessionOptions = new()
		{
			GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL,
			EnableMemoryPattern = true,
			EnableProfiling = false,
			ExecutionMode = ExecutionMode.ORT_SEQUENTIAL,
			IntraOpNumThreads = 1,
		};

		using var session = new InferenceSession("Contents/Resources/model.onnx", sessionOptions);

		var inputLayerName = session.InputMetadata.Keys.Single();

		var outputNames = session.OutputMetadata.Keys.ToList();

		float[] sourceData = new float[224 * 224 * 3];
		long[] dimensions = [1, 3, 224, 224];
		using var inputOrtValue = OrtValue.CreateTensorValueFromMemory(sourceData, dimensions);

		var inputs = new Dictionary<string, OrtValue> {
			{ inputLayerName,  inputOrtValue }
		};

		using var runOptions = new RunOptions();
		using var output = session.Run(runOptions, inputs, outputNames);

		var output_0 = output[0];
		var outputData = output_0.GetTensorDataAsSpan<float>();

		SemanticScreenReader.Announce(CounterBtn.Text);
	}

Setting a break point in this scenario does not lead to a crash.

Urgency

Not urgent

Platform

Mac

OS Version

15.5

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.22.0 (nuget)

ONNX Runtime API

C#

Architecture

ARM64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .net codeapi:CSharpissues related to the C# APIplatform:mobileissues related to ONNX Runtime mobile; typically submitted using template

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions