Bug #45978 Silent problem when net_write_timeout is exceeded
Submitted: 6 Jul 2009 20:42 Modified: 9 Sep 2009 12:08
Reporter: Pavel Bazanov
Status: Closed
Category:Connector/Net Severity:S2 (Serious)
Version:5.1.36 OS:Microsoft Windows
Assigned to: Reggie Burnett Target Version:
Tags: net_write_timeout FormatException
Triage: D2 (Serious)

[6 Jul 2009 20: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 11:49] Pavel Bazanov
Any news?
[16 Jul 2009 9: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 11:13] Tonci Grgin
I'd say this *is* a bug based on my last post.
[21 Aug 2009 20: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 20:57] Reggie Burnett
Fixed in 5.2.8.  We no longer support 5.1.  The bug is not present in 6.0+
[5 Sep 2009 1: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 12: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.