Bug #45978 Silent problem when net_write_timeout is exceeded
Submitted: 6 Jul 2009 18:42 Modified: 8 Apr 2010 14:29
Reporter: Pavel Bazanov Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:5.1.36 OS:Windows
Assigned to: Reggie Burnett CPU Architecture:Any
Tags: net_write_timeout FormatException

[6 Jul 2009 18:42] Pavel Bazanov
Description:
Hello,
First of all, I am not sure whether this problem is of MySql server, or Connector/NET.

If we are reading query resulset, and then sleep for a period, bigger than net_write_timeout, and then try to continue reading - FormatException occurs later, instead of correct exception saying something like "Timeout exceeded". Seems like that after timeout connector still reads some data (many cycles of datareader are done without problems, as I understand until part of resultset sent to the client ends) and then gets incorrect data (like "\0\0\0\0\0\0...") causing System.FormatException to be thrown in the end. 
Also, it is very strange, that this exception is not shown (I could see it only in Debug output or when I made additional try-catch) and the program simply hangs up (freezes) without any messages and so on, though I don't have any global exception handlers.

PS. The bug may be similar to http://bugs.mysql.com/bug.php?id=39878

How to repeat:
[Test]
public void Bug45978()
{
	MySqlCommand command = OpenConnectionAndCreateCommand();
	command.CommandText = "SET net_write_timeout = 1";
	command.ExecuteNonQuery();
	command.CommandText = "SELECT BlaBla FROM bigtable LIMIT 100000";
	using (MySqlDataReader reader = command.ExecuteReader())
	{
		Thread.Sleep(2000);
		// after this several cycles of DataReader.Read() are executed 
		// normally and then the problem, described above, occurs
		while (reader.Read())
		{
			object o = reader[0];
		}
	}
}

Suggested fix:
Server should send "net_write_timeout exceeded" status.
Connector should check for timeout and throw special custom exception.
[14 Jul 2009 9:49] Pavel Bazanov
Any news?
[16 Jul 2009 7:19] Tonci Grgin
Of course there are Pavel.

First of all, it really seems that Bug#39878 is very much related. Secondly, these wait timeouts are for disconnect, stalled clients whose threads MySQL server kills with sigusr1 or another signal about timeout (depends on threads lib on box).
My opinion is that connectors should check for this just like C API does (cl client throws 2013).

Let me consult more with connectors folks.
[20 Jul 2009 9:13] Tonci Grgin
I'd say this *is* a bug based on my last post.
[21 Aug 2009 18:50] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/81336

708 Reggie Burnett	2009-08-21
      changed MySqlStream.Read to throw an exception if the stream end is reached.  This is not ideal
        but we can't change it too much here in 5.2.8.  We'll look at a bigger change in 6.2. (bug #45978)
[21 Aug 2009 18:57] Reggie Burnett
Fixed in 5.2.8.  We no longer support 5.1.  The bug is not present in 6.0+
[4 Sep 2009 23:03] Pavel Bazanov
MySql Server 5.1.38 & Connector/Net 6.1.1
EndOfStreamException exception is thrown. But maybe it's better to throw something like NetWriteTimeoutExceededException instead of EndOfStreamException?
It would be easier for developers to understand real cause of the exception.

PS. Maybe it's better to change NetWriteTimeoutExpiring() test to wait for EndOfStreamException ?
[9 Sep 2009 10:08] Tony Bedford
An entry was added to the 5.2.8 changelog:

If the application slept for longer than the specified net_write_timeout, and then resumed Read operations on a connection, then the application failed silently.
[2 Apr 2010 19:54] Pavel Bazanov
Guys,
When I run the following test from Visual Studio (with the help of ReSharper, making it possible to run NUnit tests from VS) it always passes, but when I run it from NUnit GUI application it always fails:

		[Test, ExpectedException(typeof(MySqlException))]
		public void Bug45978ShouldNotAppearAnymore()
		{
			using (MySqlConnection conn = DB.ConnectToDb())
			{
				var command = new MySqlCommand("", conn);
				command.CommandText = "SET net_write_timeout = 1";
				command.ExecuteNonQuery();
				command.CommandText = "SELECT * FROM parts LIMIT 100000";
				using (MySqlDataReader reader = command.ExecuteReader())
				{
					Thread.Sleep(2000);
					while (reader.Read())
					{
						object o = reader["PartN"];
					}
				}
			}
		}

Any comments?
[8 Apr 2010 14:29] Pavel Bazanov
I installed a new version of ReSharper (5.0) and the test now also fails inside Visual Studio - exception is not thrown.

Why exception is not thrown anymore?