mono icon indicating copy to clipboard operation
mono copied to clipboard

Why sometimes happens System.Threading.ThreadAbortException: Thread was being aborted.

Open Rombersoft opened this issue 4 years ago • 7 comments

Steps to Reproduce

It can not be reproduced always because it happens very very rarely

Task.Run(() =>
            {
                try
                {
                    for (byte i = 0; i < seconds * 2; i++)
                    {
                        Thread.Sleep(500); //here it happened this ThreadAbortException
                        if (PaymentLogicBasket.IsBasketTransfered && PaymentProvider.PaymentLogic.IsPaymentClosed)
                            break;
                    }
                    if (InterfaceSettings.CurentPage == InterfacePage.Waiting && Waiting.Instance.ForPay)
                        InterfaceSettings.SetCurentPage(InterfacePage.PrintCheque, PaymentLogicBasket.IsOpenedBasket ? PaymentLogicBasket.GetBasket() : null);
                }
                catch (Exception ex)
                {
                    Logs.AppError.Debug(ex.ToString());
                }
            });

Current Behavior

System.Threading.ThreadAbortException: Thread was being aborted.

Expected Behavior

executing code from start to finish

On which platforms did you notice this

[ ] macOS [*] Linux Arch [ ] Windows

Version Used:

mono 5.2

Stacktrace

[16:38:34:279][Warning][Thread Pool Worker] System.Threading.ThreadAbortException: Thread was being aborted.
  at (wrapper managed-to-native) System.Threading.Thread.JoinInternal(System.Threading.Thread,int)
  at System.Threading.Thread.Join (System.Int32 millisecondsTimeout) [0x00019] in /opt/tmp/yaourt-tmp-root/aur-mono-stable/src/mono-5.16.0.179/mcs/class/referencesource/mscorlib/system/threading/thread.cs:708 
  at Terminal.Payment.UI.Commons+<>c__DisplayClass189_0.<PayOrdinaryPayment>b__1 () [0x000ae] in /media/destructor/Documents/Projects/CSharp/TerminalClient_v7/GUI/Standard/TerminalUI/Commons.cs:2584 

RUNTIME CONTINUED TO EXECUTE AFTER THIS ThreadAbortException!!! I want to understand why thread was aborted. And what may I do in order to thread works fine!

Rombersoft avatar Jul 07 '21 11:07 Rombersoft

During normal runtime shutdown all background threads are being aborted. I suspect this is the culprit

BrzVlad avatar Jul 13 '21 08:07 BrzVlad

During normal runtime shutdown all background threads are being aborted. I suspect this is the culprit

:-1: Application has continued to work. Runtime was not shutdown

Rombersoft avatar Jul 13 '21 09:07 Rombersoft

AppDomain unloading can also trigger aborts for threads running code in that domain.

BrzVlad avatar Jul 13 '21 11:07 BrzVlad

AppDomain unloading can also trigger aborts for threads running code in that domain.

What is unloading? Application continue to work!

Rombersoft avatar Aug 06 '21 11:08 Rombersoft

AppDomain unloading can also trigger aborts for threads running code in that domain.

What is unloading? Application continue to work!

Rombersoft avatar Aug 12 '21 13:08 Rombersoft

Again it happened but this time with Thread class

[08:52:15:324][Warning][31] System.Threading.ThreadAbortException
  at (wrapper managed-to-native) System.Net.Sockets.Socket:Connect_internal (intptr,System.Net.SocketAddress,int&,bool)
  at System.Net.Sockets.Socket.Connect_internal (System.Net.Sockets.SafeSocketHandle safeHandle, System.Net.SocketAddress sa, System.Int32& error, System.Boolean blocking) [0x00006] in /media/flash/mono-5.2.0.224/mcs/class/System/System.Net.Sockets/Socket.cs:1160 
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00079] in /media/flash/mono-5.2.0.224/mcs/class/System/System.Net.Sockets/Socket.cs:871 
  at Terminal.Providers.Network.AsyncTelemetry.AsyncTcpClient.<Connect>b__33_0 () [0x00159] in /media/destructor/SSD/Projects/TerminalClient_v7/RegulTerminal.Providers.Network/AsyncTelemetry/AsyncTcpClient.cs:119 

I think that it exists something bad in this managed-to-native

Rombersoft avatar Aug 12 '21 13:08 Rombersoft

Did you fin a solution for this ?

AbbasHabib avatar Jun 05 '22 18:06 AbbasHabib

No I did not. I notice that it happens often when use Task.Run() with Thread.Sleep() inside body. I've even found some recommendation from Microsoft to not use Task for long time execution. Therefore I decide to use new Thread().Start() instead Task.Run() but it resolves problem particularly

Rombersoft avatar Jun 06 '22 17:06 Rombersoft