TimeoutException in System.Reactive.PlatformServices.dll - c#

I'm new to Reactive Extensions. While running my application in Visual Studio, I get the following exception which I cannot reproduce reliably it but always happens given enough time:
System.TimeoutException was unhandled
Message: An unhandled exception of type 'System.TimeoutException' occurred in System.Reactive.PlatformServices.dll
Additional information: The operation has timed out.
The Break Mode tab shows the message:
Your app has entered a break state, but there is no code to show
because all threads were executing external code (typically system or
framework code).
I have no clue as to where to look for the problem, except that it might have to do with Reactive Extensions. Any ideas would be greatly appreciated.

The second message (Break Mode) is something that always gets shown in Visual Studio - that's the definition of "Break Mode" - In any application that you debug with VS you can "break" the app (using the "Pause" button, for example) - This allows you to better debug and understand the state of your parameters, objects and callstacks. The code is not showing up since you're using external code that your have no sources for.
Regarding the first exception, as you said, the platform services have a timeout. After a determined period of time something has not happened, the application will throw this timeout saying that the expected operation didn't complete in the amount of time that was specified for it. I can't tell you really why this is happening on your specific machine, but you should check if the extension requires something to initialize that you might not be providing it.

Related

How to monitor SqlClient SqlException events?

Visual Studio 2019 .NET Core C# Windows Forms development.
SqlDependency.Start is causing a SqlException. But it happens after .Start completes with return code True. So catch does not catch it. Happens periodically. Can see it in VS Output:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.SqlClient.dll
Checking SSMS log file:
Service Broker needs to access the master key in the DB. Error 32.
But I see this once. Whereas VS Output shows SqlException in groups of 5 separated by tens of seconds.
In general how can one setup a way to monitor all SqlException that happen outside of a response to a call. Say a time out exception perhaps not that I know if it happens.
In particular suggestions for resolving this are appreciated. I tried setting TRUSTWORTHY to YES but it did not help.
Not sure if SqlException and the log message are related.
Thanks!
That an exception is thrown is not necessarily an issue. It's quite normal for exceptions to be thrown. The VS debugger will show you every exception that is thrown but the vast majority of those are being thrown, caught and dealt with in system code and are most likely of no concern to you.
That said, you can configure VS to break whenever a particular type of exception is thrown, rather than the default of when it goes uncaught. While debugging, open the Exception Settings window, find the exception of interest and check the corresponding box to break whenever that exception gets thrown. It seems that you want the System.Data.SqlClient.SqlException under the Common Language Runtime Exceptions node.

Visual studio, debugging randomly throws SEHException unhandled exception in mscorlib.dll with no stackTrace

I've got a C# Windows Forms application that (outside of debug mode) works perfectly. When I run it in debug mode, each action I take has a random chance of crashing the application (like clicking a button, or closing the main form). There is a lot of async code running in the background, but the app crashes even when no tasks seem to be running (the _formClosing method is an entirely Synchronous method). Notably, the first thing most button clicks in my app do is change the visibile and enabled properties of some buttons, and then will start writing messages to the log - the app crashes before any logs get written (using NLog)
The error being thrown is:
System.Runtime.InteropServices.SEHException
HResult=0x80004005
Message=External component has thrown an exception.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
With output
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in mscorlib.dll
and trying to resume the application just ends the debugging session.
The fact that it's not any one specific section of code or event that's triggering the crash, and that the code never crashes outside of debug mode leads me to believe this might be a Visual Studio issue. Are there any settings I can configure within Visual studio to make this crash less prone to occurring? or if not, is there any way to get more information than 'cannot Evaluate the stackTrace'?
Other Information that may or may not be relevant, the memory and CPU usage are both very low at the time of crashing (40MB or so and about 5% CPU utilisation). I'm running the latest Visual Studio Community 2019. The app runs on .Net Framework 4.8. My OS is a local VM running windows 10 enterprise. I also don't think I'm running any Trusteer applications.
My code is also all heavily Try Catched, especially around the button clicks.
Thanks for any help, let me know if I'm missing any relevant information
Edit: I've discovered the line that's causing the issue, commenting out this line causes the app to run fine, completely error free
var ignore = currentJob.jobConfig.ToObject(t); //jobConfig is a JObject
The cast works fine, but by doing the cast, somehow the exception mentioned above and shown below is triggered
Edit #2: Repairing, updating and reinstalling visual studios has not changed the issue, the app still seems to work fine 10-40% of the time, and crash the rest of the time
I found a solution to my problem, by adding 'return;' statements everywhere and removing them one by one (removing large sections of my code). I eventually found the line that was causing problems. It turns out, if you serialise and then deserialise a CancellationToken, visual studio's debug session becomes prone to randomly crashing when, presumably, it tries to do a certain operation on the object you have deserialised to. The solution was just to make the cancellation token either private, or add [JsonIgnore]
Code to reproduce the problem: (run the void method, and then try closing the window)
private void BreakVS()
{
ClassWithCancellationToken someClass = new ClassWithCancellationToken();
someClass.ts = new CancellationToken();
var json = JsonConvert.SerializeObject(someClass);
JObject jObject = JObject.Parse(json);
var test = jObject.ToObject(typeof(ClassWithCancellationToken));
}
public class ClassWithCancellationToken
{
public CancellationToken ts; //change to private to
}
The answer to my original question then, is to start with Jeroan's solution to try debugging on a different device, if the code still breaks on said device then try what worked for me (hack and slash your code until you find what's breaking it) - or a more sophisticated approach to debugging your code more thoroughly. If your code works on another device, then try Micheal's approach to update/repair/reinstall your Visual studios install
Of course, the one thing I still don't know how to do, is to locate the faulty code without spending an hour removing sections of my code until I find the cause.

Silverlight web app Unhandled exception code 4004

I'm trying to design a Silverlight Application that accesses a SQL database through a WCF service. Operations that take place in the web app are fine, but as soon as I try to access data through the database I get this error:
An unhandled exception ('Unhandled Error in Silverlight Application)
Code: 4004
Category: ManagedRuntimeError
Message: System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid.
It then gives me a list of debuggers to choose from, but upon choosing one it tells me its "Unable to attach to the crashing process. A debugger is already attached."
I'm fairly new to this and haven't been able to find much conclusive advise elsewhere. Any input or similar experience to share is appreciated (: Also I'm not sure what else would be helpful to solve this problem, so let me know if there's some other piece of info I should provide.
Thanks!
The message is saying that the code is failing and the exception generated hasn't been handled.
So I recommend that you put in a try catch around the location where you call the webservice for data and simply display a message box with the exception text. And/or put a breakpoint in the location of where the silverlight application calls the webservice. Run the debugger. See what value comes back and how its handled.
Hence I whole-heartedly recommend that you put in try catches in your code and to also handle any future failures and report them appropriately; for this won't be the only exception your code will generate.

What's wrong with the code in MSDN article "How to: Schedule Work on the User Interface (UI) Thread"?

I am trying to launch the code of C# WPF app from online MSDN article: "How to: Schedule Work on the User Interface (UI) Thread" in Visual Studio 2010, Windows XP SP3, .NET4.0
The only differences I've made:
changed the namespace from wpfApplication1 to
WpfApplication1 (since it is in contradiction with article's "1. In
Visual Studio, create a WPF application project and name it."
substituted the line
string[] files = System.IO.Directory.GetFiles(#"C:\Users\Public\Pictures\Sample Pictures\", "*.jpg");
with
string[] files = System.IO.Directory.GetFiles(#"D:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\", "*.jpg");
(according to my Windows XP machine's configuration)
But after pressing the button, the app breaks with exception:
A Task's exception(s) were not observed either by Waiting on the Task
or accessing its Exception property. As a result, the unobserved
exception was rethrown by the finalizer thread
The code compiles without errors but the line:
Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow)
has Intellisense with wavy underline showing the popup warning:
"Possible 'System.NullRederenceException'"
My VS2010 solution (attempting to reproduce this article) can be downloaded from:
http://wikisend.com/download/404394/msdnHow2ScheduleWorkOnTheUI.rar
What's wrong with this code?
And how to correct it?
Update:
The question is not how to observe exception messages but how to launch the MSDN sample code (by a beginner' like me)
I'd prefer asking exception-specific questions separately
Update2:
Errata:
Sorry...
The application doesn't break as I wrote before.
I inserted MessageBox.Show("Finished"); at the end of button1_Click() button click event handler.
Upon first click on the button the MessageBox with "Finished!" shows up.
Upon 2nd click it Messagebox again is shown and but app also throws the mentioned above exception.
And having passed through exception messages and tasks results, I cannot figure out what is wrong!
I'd still like to stress that I am interested in the working sample of article's topic and will post my questions on debugging separately in other question(s)!
Is it reproducible by others?
The problem is that you have an exception that is occurring inside of your task. The message is pretty self-explanatory, but I guess only if you know about exception handling in the TPL. So, to further elaborate on your exact error:
When an exception is thrown from inside of a task, it must be "observed" in 3 different ways:
Check the task's Exception property
Attempt to read the task's Result property (Causing any stored exceptions to be thrown)
Attach an event handler to the TaskScheduler.UnobservedTaskException
If it is not observed in any of the above ways, then the exception will finally be thrown when the garbage collector tries to finalize the task. This is the reason for the exact message you are getting. However, for the deeper, original exception you will need to do one of the 3 above steps and review the actual exception
Here is an MSDN on exception handling in the TPL
Also, as an FYI, it seems that this unobserved exception may not HAVE to be handled, but I havent personally dug in. Even still, it would be better to handle these as you would any code. So, this change should not change how you write your code.

How to catch exceptions from another program (for logging)?

I am working on a tool that monitors a number of applications and ensures they are always running and in a clean state. Some of these applications have unhandled exceptions which do occur periodically and present the 'send crash report' window. I do not have the source code to these applications.
Is there any mechanism I could use to catch the exceptions, or simply identify their exception type, as well as identify the application's main executable file that threw the exception.
I'm not trying to do anything crazy like catch and handle it on the applications behalf, I'm simply trying to capture the exception type, log it and then restart the application.
Trapping unhandled exceptions requires calling SetUnhandledExceptionFilter() in the process. That's going to be difficult to do if you don't have source code, although it is technically possible by injecting a DLL into the process. This however cannot be done with managed code, you can't get the CLR initialized properly.
The default unhandled exception handler that Windows installs will always run WerFault.exe, the Windows Error Reporting tool. That can be turned off but that's a system setting. Expecting your user or admin to do this is not realistic. Only after WER runs will the JIT debugger get a shot at it.
I recommend a simpler approach, one that's also much more selective. Use the Process class to get the program you're interested in protecting started. When the Exited event fires, use the ExitCode property to find out how it terminated. Any negative value is a sure sign that the process died on an unhandled exception, the exit code matches the exception code. You can use the EventLog class to read the event message that WER writes to the Windows event log. And you can restart it the same way you got it started.
Without modifying the source of the application or injecting a DLL into the process I do not believe this is possible in a reliable fashion. What you're attempting to do is inspect type information across a process boundary. This is not easy to achieve for a number of reasons
Getting the name of the exception implies executing code in the target process. The name can be generated a number of ways (.ToString, or .GetType().Name) but all involve executing some method
You want to do this after the application has already crashed and hence may be in a very bad state. Consider trying to execute code if memory was corrupted (perhaps even corrupting the in memory definitions of the type).
The crash could occur in native code and not involve any managed data
If you want to monitor application crashes system wide, you can register yourself as a just-in-time debugger. You can edit the registry to specify which debugger to run when an application crashes. The example they give is Doctor Watson, but it could be your application instead.

Categories