Skip to content

InvalidCastException thrown after TimerQueueTimer.CallCallback #81211

Closed
@tipa

Description

@tipa

Description

After upgrading my iOS app from classic Xamarin.iOS to .NET7 I started observing frequent InvalidCastExeptions in my crash reporting tool (AppCenter). I haven't changed much dependency-wise and I am unable to reproduce this locally.

Reproduction Steps

Unfortunately, I don't know what part of my code causes this behavior.
For the first stack trace, I assume that my code (or some dependencies code) is creating a CancellationTokenSource with a delay parameter.
For the second stack trace below, I assume that some call to Task.Delay is causing the crash.

Expected behavior

No crash :)

Actual behavior

SIGABRT: Arg_InvalidCastException

System.Threading.CancellationTokenSource.TimerCallback(Object )
System.Threading.TimerQueueTimer.CallCallback(Boolean )
System.Threading.TimerQueueTimer.Fire(Boolean )
System.Threading.TimerQueue.FireNextTimers()
System.Threading.TimerQueue.System.Threading.IThreadPoolWorkItem.Execute()
System.Threading.ThreadPoolWorkQueue.DispatchItemWithAutoreleasePool(Object , Thread )

this is here:

((CancellationTokenSource)state!).NotifyCancellation(throwOnFirstException: false); // skip ThrowIfDisposed() check in Cancel()

SIGABRT: Arg_InvalidCastException

System.Threading.Tasks.Task.DelayPromise.TimerCallback(Object )
System.Threading.TimerQueueTimer.CallCallback(Boolean )
System.Threading.TimerQueueTimer.Fire(Boolean )
System.Threading.TimerQueue.FireNextTimers()
System.Threading.TimerQueue.System.Threading.IThreadPoolWorkItem.Execute()
System.Threading.ThreadPoolWorkQueue.DispatchItemWithAutoreleasePool(Object , Thread )

this is here:

private static void TimerCallback(object? state) => ((DelayPromise)state!).CompleteTimedOut();

Both crashes originate from here:


The "_state" variable is nullable, and based on the reports, the crashes are caused by the variable to be indeed null.

Regression?

I did not see those crashes when using "old" Xamarin.iOS

Known Workarounds

No response

Configuration

.net7-ios
happens on various iPhone models and iOS versions

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions