Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rombersoft opened this issue Jul 7, 2021 · 7 comments

Comments

@Rombersoft
Copy link

Rombersoft commented Jul 7, 2021

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!

@BrzVlad
Copy link
Member

BrzVlad commented Jul 13, 2021

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

@Rombersoft
Copy link
Author

Rombersoft commented Jul 13, 2021

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

👎 Application has continued to work. Runtime was not shutdown

@BrzVlad
Copy link
Member

BrzVlad commented Jul 13, 2021

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

@Rombersoft
Copy link
Author

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

What is unloading? Application continue to work!

@Rombersoft
Copy link
Author

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

@AbbasHabib
Copy link

Did you fin a solution for this ?

@Rombersoft
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants