Bug #66578 CacheServerProperties can cause 'Packet too large' error
Submitted: 28 Aug 2012 14:46 Modified: 28 Sep 2012 18:58
Reporter: Poul Bak Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.6.1 OS:Any
Assigned to: Gabriela Martinez Sanchez CPU Architecture:Any
Tags: CacheServerProperties max_allowed_packet 'Packet too large'

[28 Aug 2012 14:46] Poul Bak
Description:
Under some circumstances setting 'CacheServerProperties = true' in connectionstring can cause a 'Packet too large' error.
This seems to happen 'randomly' and the fact that it's related to 'CacheServerProperties' is non-obvious, which is why I have marked it 'S2'.

How to repeat:
Several things must match for this error to happen:
1. Connection pooling must be on.
2. 'CacheServerProperties' must be true.
3. The first connection created will work as expected, no problems, so you must create more than one connection in the pool.
4. The second, third, etc. connections will fail, if the query exceeds 1024 bytes.

The reason can be found in 'Driver.cs', method 'LoadServerProperties'.
This method is only called on the first created connection, when 'CacheServerProperties' is on (that's the whole point of the caching).
However it sets the driver's 'maxPacketSize' property, meaning only the first connection will set this, the others will use the default of 1024 bytes.

Suggested fix:
Move the code up to the the 'Configure' method, so it's called on all connections.
The code to be moved:

            if (hash.Contains("max_allowed_packet"))
                maxPacketSize = Convert.ToInt64(hash["max_allowed_packet"]);

'hash' must be renamed to 'serverProps' to fit into the 'Configuration' method:

            if (serverProps.Contains("max_allowed_packet"))
                maxPacketSize = Convert.ToInt64(serverProps["max_allowed_packet"]);

I include 'Driver.cs' with suggested fixes, incl. fixes for http://bugs.mysql.com/bug.php?id=66477

The change should be able to go into all versions since Connector/Net 6.3.
[28 Aug 2012 14:47] Poul Bak
Driver.cs after change

Attachment: Driver.cs (text/plain), 18.70 KiB.

[4 Sep 2012 2:04] Poul Bak
A good way to see this bug (pseudo code):

{
//1. query - caches serverproperties, creates 1 connection
}

//Now spawn 2 threads to make the pool grow
Parallel.Invoke(() =>
{
// 2. query (probably reuses 1. connection)
}), () =>
{
// 3. query - creates a new connection. THIS WILL FAIL IF BIGGER THAN 1024 bytes!
});
[4 Sep 2012 22:29] Gabriela Martinez Sanchez
Hi Poul,

Could you please post the code you're using to set your query and use your connections?

I'm trying to reproduce it but haven't had any luck yet.

Thanks in advance.
[5 Sep 2012 20:05] Gabriela Martinez Sanchez
Bug Confirmed. in version 6.4.x. A fix is in progress.
[28 Sep 2012 18:58] John Russell
Added to changelog for 6.4.6, 6.5.5, 6.6.3: 

Under some circumstances, setting CacheServerProperties=true in the
connection string could cause a Packet too large error. With
connection pooling enabled and CacheServerProperties=true, the first
connection worked as expected, but the second, third, and so on
connections would fail if the query exceeded 1024 bytes.