Bug #11588 "Catastrophic failure" when you reuse connections after MySQL restart
Submitted: 27 Jun 2005 10:22 Modified: 10 Jun 2010 19:34
Reporter: Emmanuel KARTMANN Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / ODBC Severity:S2 (Serious)
Version:MyODBC 3.51.11 OS:Microsoft Windows (Windows XP, Windows 2003 Server)
Assigned to: CPU Architecture:Any

[27 Jun 2005 10:22] Emmanuel KARTMANN
Description:
Under certain circumstances on our production servers running MySQL 4.1.12 (Windows 2003 Server and/or Windows XP) we have a "catastrophic failure" error coming from the MyODBC driver version 3.51.11

The front is IIS - version 5.1 (on Windows XP) or version 6.0 (on Windows 2003 Server). Once the error occurs, the IIS server is unstable and access to MySQL is impossible (complete loss of service).

We store MyODBC connections into ASP/IIS Sessions and reuse than to avoid a reconnection cost. Unfortunatly, this leads to errors "An exception has occurred" and "Catastrophic failure".

The call stack is:
>	myodbc3.dll!_CrtIsValidHeapPointer(const void * pUserData=0x018fc610)  Line 1807	C
 	myodbc3.dll!_free_dbg_lk(void * pUserData=0x018fc610, int nBlockUse=1)  Line 1132 + 0x9	C
 	myodbc3.dll!_free_dbg(void * pUserData=0x018fc610, int nBlockUse=1)  Line 1070 + 0xd	C
 	myodbc3.dll!free(void * pUserData=0x018fc610)  Line 1025 + 0xb	C
 	myodbc3.dll!my_no_flags_free(char * ptr=0x018fc610)  Line 60 + 0x9	C
 	myodbc3.dll!reget_current_catalog(tagDBC * dbc=0x00137d78)  Line 913 + 0xf	C
 	myodbc3.dll!get_con_attr(void * hdbc=0x00137d78, long Attribute=109, void * ValuePtr=0x0108e4c0, long BufferLength=257, long * StringLengthPtr=0x0108e4b8)  Line 480 + 0x9	C
 	myodbc3.dll!SQLGetConnectAttr(void * hdbc=0x00137d78, long Attribute=109, void * ValuePtr=0x0108e4c0, long BufferLength=257, long * StringLengthPtr=0x0108e4b8)  Line 906 + 0x19	C

It seems that function reget_current_catalog is called too many times, and frees memory twice (which leads to heap corruption and crash).

Original source file is:

  my_bool reget_current_catalog(DBC FAR *dbc)
  {
      my_free((gptr) dbc->database,MYF(0));  // CRASHES HERE ON SECOND/THIRD LOOP
      if (odbc_stmt(dbc, "select database()"))
      {
          return 1;
      }

How to repeat:
I have a set of 3 ASP/HTML pages to reproduce the bug (I could post/send them by email). The main idea is:

 * open a connection to MyODBC
 * store connection in ASP Session
 * Restart MySQL (rendering the MyODBC connection unusable/obsolete)
 * reuse connection from ASP Session / reconnect when it fails with error "Lost connection to MySQL server"
 * MyODBC crashes

Suggested fix:
Do not free memory twice. A simple fix would be to NULLify the pointer after free and checking if it's not NULL before released again:

  my_bool reget_current_catalog(DBC FAR *dbc)
  {
      if (dbc->database)
      {
        my_free((gptr) dbc->database,MYF(0));
        dbc->database = NULL;
      }
      ...
[27 Jun 2005 10:23] Emmanuel KARTMANN
Start HTML Page for test case

Attachment: myodbc_catastrophic_failure.htm (text/html), 979 bytes.

[27 Jun 2005 10:24] Emmanuel KARTMANN
First ASP Page of test case - open connection and stores it in Session

Attachment: myodbc_catastrophic_failure1.asp (text/plain), 1.26 KiB.

[27 Jun 2005 10:24] Emmanuel KARTMANN
Second ASP Page of test case - fetch connection from Session and reuses it (and crashes!)

Attachment: myodbc_catastrophic_failure2.asp (text/plain), 3.51 KiB.

[27 Jun 2005 10:27] Emmanuel KARTMANN
Test case (in ASP) is available for download:
 * myodbc_catastrophic_failure.sql: creates the test case database
 * myodbc_catastrophic_failure.htm: start page
 * myodbc_catastrophic_failure1.asp: first ASP page; opens connection and stores it in Session
 * myodbc_catastrophic_failure2.asp: second ASP page; reuses connection from Session (YOU MUST RESTART MYSQL before calling this page).
[27 Jun 2005 10:27] Emmanuel KARTMANN
Creates test case database and table

Attachment: myodbc_catastrophic_failure.sql (text/plain), 679 bytes.

[27 Jun 2005 13:38] Miguel Solorzano
I was unable to repeat the reported behavior on my XP Pro box,
I was able for to reload the first asp page after the second and
expected not connect message with 2nd asp page.
[27 Jun 2005 15:25] Emmanuel KARTMANN
I knew this one was tricky to reproduce (took me one month to get it ;-)

Did you get a success message on ASP page 2 ("Test completed with no error!")?

Can you please try the test case again with a slight change:
  * myodbc_catastrophic_failure.htm (click on "GO!")
  * myodbc_catastrophic_failure1.asp (RESTART MySQL, click on "GO!")
  * myodbc_catastrophic_failure2.asp (use the "Reload" button from Internet Explorer - sometimes it takes 2 calls to this page to crash).
  * myodbc_catastrophic_failure2.asp (you should get error "An exception occurred" displayed in red).

Regards,

E.
[10 Jun 2010 17:09] Edwin van Putten
I can confirm this problem!

See bug report #48406 and #54404
[10 Jun 2010 19:34] Lawrenty Novitsky
Thank you Edwin.
Marking as a duplicate of the Bug#46910