Bug #4039 Client shutsdown winsock, even when it wasn't repsonsible for starting it
Submitted: 7 Jun 2004 15:42 Modified: 18 Sep 2004 17:35
Reporter: Nathaniel Blanchard Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:3.23 OS:Windows (windows)
Assigned to: Assigned Account CPU Architecture:Any

[7 Jun 2004 15:42] Nathaniel Blanchard
Description:
We dynamically load the mysql client libs at run time in our application depending on user actions and then unloading it.  Even if the client wasn't responsible for launching winsock, it still shuts it down.

How to repeat:
Startup winsock before initializing the mysql client lib and then shutdown the lib.  

Suggested fix:
diff -ur mysql-4.0.16/mysys/my_init.c mysql-4.0.16-new/mysys/my_init.c
--- mysql-4.0.16/mysys/my_init.c    Fri Oct 17 12:29:16 2003
+++ mysql-4.0.16-new/mysys/my_init.c    Fri Oct 31 14:14:15 2003
@@ -35,6 +35,7 @@
 #include <crtdbg.h>
 #endif
 my_bool have_tcpip=0;
+my_bool responsible_for_tcpip=1;
 static void my_win_init(void);
 static my_bool win32_have_tcpip(void);
 static my_bool win32_init_tcp_ip();
@@ -184,7 +185,7 @@
 #endif /* THREAD */

 #ifdef __WIN__
-  if (have_tcpip)
+  if (have_tcpip && responsible_for_tcpip)
     WSACleanup();
 #endif /* __WIN__ */
   my_init_done=0;
@@ -335,7 +336,10 @@
      have_tcpip=1;
       }
       else
+     {
    have_tcpip=1;
+   responsible_for_tcpip=0;
+     }
     }
   }
   return(0);
[18 Jun 2004 2:46] MySQL Verification Team
Thank you for the bug report
[3 Aug 2004 15:26] Thomas Briggs
The proposed patch basically amounts to never calling WSACleanup(), and as such can be ignored.

The underlying problem here is that WSAStartup does not return an error if WinSock has already been initialized (which makes sense), so you can't know whether or not you need to shut it down.  All other libraries/database clients/etc. that I've dealt avoid this problem by requiring the parent process to manage initialization/startup of WinSock; given the inability of a DLL to know whether or not it is responsible for shutting down WinSock, this would seem to be the only way to solve the problem here as well.
[3 Aug 2004 17:07] MySQL Verification Team
Thank you for your comment.
[4 Aug 2004 12:06] Sergei Golubchik
Let's see what we can do here...
[16 Aug 2004 14:33] Sergei Golubchik
see also http://bugs.mysql.com/5062
[18 Aug 2004 17:35] Reggie Burnett
I need to know how this was verified as I can't seem to reproduce it.  Winsock maintains it's own reference count on the WSAStartup and WSACleanup routines.   I wrote a test program that inited sockets with WSAStartup and then dynamically loaded libmysql and attempted to create a socket connection to a database.  Then it unloaded libmysql and then try to execute a socket call.  If WSACleanup had actually unloaded winsock, then this last socket call would fail with an error stating that the library needs initializing.  Instead, the call succeeded.  

The docs for WSAStartup and WSACleanup state that the reference count is kept internally.  If this is true, then we would not need to reference count it ourselves.

Can the original poster post some code that shows the problem along with the exactly platform they are using?

Thanks
Reggie
[26 Aug 2004 13:28] Nico de Groot
estimated time till fix.. 
(http://bugs.mysql.com/5062 with a attached file)

i'm re-writing my program to my-sql and i'm getting al lot of av's caused by this
error.  Deadline for my program is next week (sept '04) !! 

since the error stands for a while I calculated it would be fixed by now... wrong choice??
[14 Feb 2005 22:54] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".