Bug #43190 MySqlConnection.Open breaks debugging in Visual Studio 2005/2008
Submitted: 25 Feb 2009 14:38 Modified: 17 Mar 2009 16:57
Reporter: Juergen Steinblock Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:5.2.5.0 OS:Windows (XP / Vista)
Assigned to: CPU Architecture:Any
Tags: connector.net, debugging, MySql.Data

[25 Feb 2009 14:38] Juergen Steinblock
Description:
Since 2 month we have a serious problem within Visual Studio.
If some of our applications are run within visual studio and an exception ius thrown, the process stops unexpectedly without any warning (it should stop at the line with the exception marked yellow in Visual Studio)

I wasn't able to detect why this happens in some apps and in some not.
Until now. This seems to happen if I call the Open Method of an MySqlConnection Object. After the first time you do so, every Exception that is thrown causes the application that is debugged to terminate without warning.
Not even a global exception handler is called (if you use Application Framework with Application Events and a UnhandledException handler)

The error occures with:
- Visual Studio 2005/2008, SharpDevelop
- Visual Basic / C# as a programming Language
- MySql Connector 5.1.2.2, 5.1.6.0, 5.2.5.0
- MySql Server 5.0.7 beta, 5.1.30

How to repeat:
- Create a new C# Windows Forms Project within Visual Studio
- Add a reference to MySql.Data.dll and a "using MySql.Data.MySqlClient;" statement
- Drop two buttons on the form
- Add code to button1 click event that throws an exception (i.e.):
        private void button1_Click(object sender, EventArgs e)
        {
            throw new Exception();
        }
- Add code to button2 click event to open a mysql connection (i.e.):
        private void button2_Click(object sender, EventArgs e)
        {
            using (MySqlConnection con = new MySqlConnection("server=localhost;database=mysql;uid=root")) {
                con.Open();
                MessageBox.Show(con.State.ToString());
            }
        }
- Debug your app
- Click button1, a exception should be thrown
- Click button2, and after the messagebox is shown, click button1, which causes the form to close and the debugging session ends.

Hint: don't forget to update the connectionstring to fit to your environment.

Suggested fix:
Workaround:
In a C# project, go and find your program.cs file.
add a line

   Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

before the

Application.Run(new Form1());

statement.
It's not exactly the behavior as without this bug (the debugger stops at the line with the exception, if you want to change the source you get a dialog where you can choose if you want to modify the code or close the application.

In this mode the debugger seems to be less robust against code changes, sometimes if I just add a blank line or delete a code comment, I have to restart debugging.

One last thing,
the change happend back in the first weeks of this year (don't know the exact day) from one day to another on all our developer machines (2 Vista, one XP) so this may be somehow related to a windows update. We didn't even change the MySql.Data.dll.
Anyway, it only occures if I call the Open Method.
[25 Feb 2009 14:57] Juergen Steinblock
Please note:
If I enable the Application Framework not event the workaround has an effect.
[25 Feb 2009 18:59] Tonci Grgin
Hi Juergen and thanks for your report.

I'm having troubles understanding the procedure...
- Debug your app

Does this mean I should start it from VS2005 IDE or should I attach with WinDBG, for example?

- Click button1, a exception should be thrown

If I'm in IDE by clicking this button I loose any other option but to break debugging session...

- Click button2, and after the messagebox is shown, click button1, which causes the form to close and the debugging session ends.

As I said in previous point, following your instructions in IDE *never* get's me to this point...

However, running executable shows no problems like you reported but then again, that is not a debug session...

So, how to test this behavior?
[26 Feb 2009 7:23] Juergen Steinblock
I know the error seems a bit strange and is hard to discribe.
What I ment was to start the app from within visual studio (to get debugging functionality) and to try it for two times.

Let's modify the code a bit:
---------------------------------------------
        private void button1_Click(object sender, EventArgs e)
        {
            using (MySqlConnection con = new
MySqlConnection("server=localhost;database=mysql;uid=root")) {
                MessageBox.Show(con.State.ToString());
                throw new Exception("This exception causes the debugger to break here");
            }
        }
- Add code to button2 click event to open a mysql connection (i.e.):
        private void button2_Click(object sender, EventArgs e)
        {
            using (MySqlConnection con = new
MySqlConnection("server=localhost;database=mysql;uid=root")) {
                con.Open();
                MessageBox.Show(con.State.ToString());
                throw new Exception("This exception causes the app to close");
            }
        }
---------------------------------------------

If I click on button1 the exception is throw within the IDE but if I start another debug session the app closes without any further warning or error.
As you can see, this happen only if I call the con.Open() method.

One thing to mention, I tracked the date since this happens down to 2008/12/18 (this is where we noticed it)
[26 Feb 2009 7:31] Juergen Steinblock
If I compile this example with csc command line tool and use coredbg to debug, this is the output:

button1 click (throws an exception as it should be):
----------------------------------------------------
(cordbg) go
Warning: couldn't load symbols for C:\Windows\assembly\GAC_MSIL\Accessibility\2.
0.0.0__b03f5f7f11d50a3a\Accessibility.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_MSIL\mscorlib.resourc
es\2.0.0.0_de_b77a5c561934e089\mscorlib.resources.dll
Warning: couldn't load symbols for C:\temp\mysql\MySql.Data.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_32\System.Data\2.0.0.
0__b77a5c561934e089\System.Data.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_32\System.Transaction
s\2.0.0.0__b77a5c561934e089\System.Transactions.dll
First chance exception generated: (0x1faa590) <System.Exception>
Unhandled exception generated: (0x1faa590) <System.Exception>
  _className=<null>
  _exceptionMethod=<null>
  _exceptionMethodString=<null>
  _message=<null>
  _data=<null>
  _innerException=<null>
  _helpURL=<null>
  _stackTrace=(0x1faa770) <System.SByte[]>
  _stackTraceString=<null>
  _remoteStackTraceString=<null>
  _remoteStackIndex=0
  _dynamicMethods=<null>
  _HResult=-2146233088
  _source=<null>
  _xptrs=0
  _xcode=-532459699
Exception is called:UNHANDLED

024:                 throw new Exception();
----------------------------------------------------

button2 click (Debugging stops and program closes):
----------------------------------------------------
(cordbg) go
Warning: couldn't load symbols for C:\Windows\assembly\GAC_MSIL\Accessibility\2.
0.0.0__b03f5f7f11d50a3a\Accessibility.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_MSIL\mscorlib.resourc
es\2.0.0.0_de_b77a5c561934e089\mscorlib.resources.dll
Warning: couldn't load symbols for C:\temp\mysql\MySql.Data.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_32\System.Data\2.0.0.
0__b77a5c561934e089\System.Data.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_32\System.Transaction
s\2.0.0.0__b77a5c561934e089\System.Transactions.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_MSIL\System.Configura
tion\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll
Warning: couldn't load symbols for C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0
.0__b77a5c561934e089\System.Xml.dll
[thread 0x64c] Thread created.
Warning: couldn't load symbols for C:\Windows\assembly\GAC_32\System.EnterpriseS
ervices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll
First chance exception generated: (0x20938a4) <System.Exception>
ExceptionUnwind is called:
        AppDomain: Program.exe
        TID      : 0x1318
        EventType: UNWIND_BEGIN
Process exited.
----------------------------------------------------
[17 Mar 2009 16:57] Juergen Steinblock
Ticket can be considered as closed.
It was hard work but I was able to track the bug down to
   MySql.Data.Common.StreamCreator.GetHostEntry(string hostname)
which is called within connection.Open()

The line of code that actually breaks debugging is:
   if (IPAddress.TryParse(hostname, out addr))

which belongs to the framework so I moved this bug to Microsoft Connect
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=424433