Bug #21274 Cannot connect to WAN database on a windows service application
Submitted: 25 Jul 2006 13:55 Modified: 7 Aug 2006 8:15
Reporter: Jean Bédard Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:1.0.7 OS:Windows (Windows 2000/XP/2003)
Assigned to: CPU Architecture:Any
Tags: connect;remote database;failed, windows service

[25 Jul 2006 13:55] Jean Bédard
Description:
I found a very strange bug. My application is a Windows Service that is starting automatically on system boot up. In the service start routine, I'm trying to connect to a database. This works fine when the database IP address is in the LAN area (localhost or 192.168.1.20 for example works fine!). But, when the database IP address is not part of the LAN area (lets say test.dynip.com), the connection failed and I get the following stacktrace:

 at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
   at MySql.Data.Common.SocketStream.Read(Byte[] buffer, Int32 offset, Int32 count) in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\common\socketstream.cs:line 105
   at System.IO.BufferedStream.ReadByte()
   at MySql.Data.MySqlClient.PacketReader.ReadHeader() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\packetreader.cs:line 96
   at MySql.Data.MySqlClient.PacketReader.OpenPacket() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\packetreader.cs:line 133
   at MySql.Data.MySqlClient.NativeDriver.Open() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\nativedriver.cs:line 167
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionString settings) in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\driver.cs:line 117
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\mysqlpool.cs:line 125
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\mysqlpool.cs:line 113
   at MySql.Data.MySqlClient.MySqlPool.GetConnection() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\mysqlpool.cs:line 153
   at MySql.Data.MySqlClient.MySqlPoolManager.GetConnection(MySqlConnectionString settings) in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\mysqlpoolmanager.cs:line 68
   at MySql.Data.MySqlClient.MySqlConnection.Open() in c:\program files\mysql\mysql connector net 1.0.7\src\mysqlclient\connection.cs:line 300

Don't care about line number because I was using a custom version of 1.0.7.build. To add some details, lets say that the database at IP address test.dynip.com is inside the LAN area, but there is a UpnP forwarding on my router. Moreover, if I try to start the windows service manually (using the service console), it works fine. So I believe the problem may be because there is no user logged yet...

I added some custom log on the Connector.NET and I found that test.dynip.com is resolved correctly, but the Socket.Connect() method on the SocketStream.Connect() method is raising a Exception with code 10035 (normal) but the Socket.Select(null,write,error,timeout*1000*1000) method call is not raising any exception BUT write and error ArrayList are BOTH empty.

How to repeat:
Create a windows service that is connecting to a MySQL database server using a public IP address or a domain name. The windows service MUST be started automatically at system startup.

Suggested fix:
No idea... I'll try to open the database connection later in my service using a timer (not good for me!).
[25 Jul 2006 14:48] Tonci Grgin
Hi Jean and thanks for your problem report.
I think what's happening is that not all required services are up by the time your service starts. You should probably check dependencies of your service and add required ones.
[25 Jul 2006 14:53] Tonci Grgin
Jean, please report MySQL server versions, both for localhost and for remote.
[25 Jul 2006 15:07] Jean Bédard
Hi Tonci,

You are right, the problem seems to come from the MySQL server itself. I'm using version 5.0.15 NT. Look what I did to debug deeper:

I set my service to connect to database at "test.dynip.com" on port 2003. There is a UPnp record on my router that redirect everything on port 2003 to my computer's 2003 port. So I setup a TCP Listener (that does nothing except listening and accepting incoming connection on port 2003) and I reboot the PC where my windows service is installed. Surprise, the connection is made succesfully!

What I forgot to tell is that the database I'm using is on the same computer than the windows service. But I'm using a public IP address (or domain name test.dynip.com) instead of "localhost". I found that the test.dynip.com is correctly resolved (so that the DNS Client Service is correctly started). I also add a dependencies to the MySQL service wihtout any change. I fact, when using "localhost", everything works... to this is a matter of service started or not... I think. Maybe the MySQL service does not accept incomming connection from "remote" IP address when no user is actively logged into the system.

Any idea ?
[7 Aug 2006 8:15] Tonci Grgin
Hi Jean, sorry for the delay.
In my opinion, MySQL server starts up before your server obtains the public IP address resulting in error you get. I don't see a bug here.