Bug #38476 SemaphoreFullException with MySQL Connector/NET 5.2.2
Submitted: 30 Jul 2008 23:49 Modified: 6 Aug 2008 14:54
Reporter: Philip Hofer Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:5.2.2 OS:Microsoft Windows (Server 2008)
Assigned to: CPU Architecture:Any
Tags: Semaphore Connector/NET

[30 Jul 2008 23:49] Philip Hofer
Description:
"Adding the given count to the semaphore would cause it to exceed its maximum count."

"   at System.Threading.Semaphore.Release(Int32 releaseCount)\r\n   at
System.Threading.Semaphore.Release()\r\n   at
MySql.Data.MySqlClient.MySqlPool.RemoveConnection(Driver driver)\r\n   at
MySql.Data.MySqlClient.MySqlPoolManager.RemoveConnection(Driver driver)\r\n   at
MySql.Data.MySqlClient.Driver.Dispose(Boolean disposing)\r\n   at
MySql.Data.MySqlClient.NativeDriver.Dispose(Boolean disposing)\r\n   at
MySql.Data.MySqlClient.Driver.Close()\r\n   at
MySql.Data.MySqlClient.NativeDriver.Finalize()"

With iis7 Server, when it first restarts everything is fine, but randomly minutes to a 1/2 hour later iis7 locks up and gives this error.  I'm using the MySQL Connector 5.2.2 

[web.config]
<add key="MySQLConnStr" value="Server=localhost;Port=3306;Database=comicinfo;Uid=comicdbuser;Pwd=itsasecret" />

[default.aspx]
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>
<script runat="server">
	void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			ShowTopTen();
			ShowVertAd();
			ShowHeaderAd();
			ShowTopTenHits();
			ShowLeftAds();
		}
	}
</script>
<* buncha html code here *>

<script runat="server" type="text/javascript">
	
private void ShowLeftAds()
{
  try
  {
    // Connection string for a typical local MySQL installation
	string cnnString = ConfigurationSettings.AppSettings["MySQLConnStr"];

    // Create a connection object and data adapter
    MySqlConnection cnx = new MySqlConnection(cnnString);
    MySqlDataAdapter adapter = new MySqlDataAdapter();

    // Create a SQL command object
	string cmdText = "SELECT * from comic where imgurlrect <> '' ORDER BY rand() LIMIT 10";
    MySqlCommand cmd = new MySqlCommand(cmdText, cnx);

    // Create a fill a Dataset
    DataSet ds = new DataSet();
    adapter.SelectCommand = cmd;
    adapter.Fill(ds);

    // Bind the DataSet
    ShowLeftAdsRepeater.DataSource = ds;
    ShowLeftAdsRepeater.DataBind();
    
    }
    catch (Exception ex) {
    lblErrorLeftAds.Text = ex.Message;
  }
  

}
</script> 
	<br />
<table width="202" cellspacing="1" cellpadding="0">	
	<asp:label cssclass="Error" enableviewstate="False" runat="server" id="lblErrorLeftAds"></asp:label>
      <asp:repeater runat="server" id="ShowLeftAdsRepeater">
        <headertemplate>
        </headertemplate> 
        <itemtemplate>
        <tr>
			<td>
       	<a href="http://www.frumph.net/out.asp?url=<%# DataBinder.Eval(Container.DataItem, "url")%>" target="_blank">
       	<img src="<%# DataBinder.Eval(Container.DataItem, "imgurlrect")%>" border="0" alt="<%# DataBinder.Eval(Container.DataItem, "name")%>" /></a><br />
       		</td>
       	</tr>
        </itemtemplate>
        <footertemplate>
        </footertemplate>
      </asp:repeater>
</table>

<* few more duplicates of that private void doing different queries *>

I'm just saying this works, for a time than it dumps out with that error.

How to repeat:
Restart IIS7 and it goes back to doing it again.

Suggested fix:
Unknown.
[31 Jul 2008 8:14] Tonci Grgin
Hi Philip and thanks for your report.

This is a duplicate of Bug#36688 which is closed as fixed but I'll reopen it now.

Thanks for your interest in MySQL.
[31 Jul 2008 8:19] Tonci Grgin
Reggie says it should not be the problem in 5.2 branch. Reopening, not a duplicate so far.
[31 Jul 2008 8:22] Tonci Grgin
Philip, what happens if you turn pooling on? Put "Max pool size" > 20 and retest please. Inform me of result.
[31 Jul 2008 10:07] Philip Hofer
http://www.connectionstrings.com/?carrier=mysql

doesnt have the parameter listed for it, I just copied how other strings do it:

[code]
 <add key="MySQLConnStr" value="Server=localhost;Port=3306;Database=comicinfo;Uid=comicdbuser;Pwd=[hidden];Pooling=True;Max Pool Size=100;" />
[/code]

Is that what you mean?
[31 Jul 2008 10:24] Tonci Grgin
Philip, yes, that's what I meant. Define also min pool size and retest. What is .NET FW version on 2k8? 3.5? And IIS?
[31 Jul 2008 10:45] Philip Hofer
[code]
System.Threading.SemaphoreFullException was unhandled
  Message="Adding the given count to the semaphore would cause it to exceed its maximum count."
  Source="System"
  StackTrace:
       at System.Threading.Semaphore.Release(Int32 releaseCount)
       at System.Threading.Semaphore.Release()
       at MySql.Data.MySqlClient.MySqlPool.RemoveConnection(Driver driver)
       at MySql.Data.MySqlClient.MySqlPoolManager.RemoveConnection(Driver driver)
       at MySql.Data.MySqlClient.Driver.Dispose(Boolean disposing)
       at MySql.Data.MySqlClient.NativeDriver.Dispose(Boolean disposing)
       at MySql.Data.MySqlClient.Driver.Close()
       at MySql.Data.MySqlClient.NativeDriver.Finalize()
  InnerException: 
[/code]

This is with this conn str:

[code]
<add key="MySQLConnStr" value="Server=localhost;Port=3306;Database=comicinfo;Uid=comicdbuser;Pwd=[youdlikeotknow];Pooling=True;Max Pool Size=100;Min Pool Size=5;" />
[/code]

Windows Server 2008
IIS7 
Framework: ASP.Net_2.0.50727.0 (whats used on all of the iis7 instances)
MySql Connector/NET 5.2.2
MySql Server 5.0

Installed:
MS .NET Compact Framework 2.0 (SP2)
MS .NET Compact Framework 3.5
MS .NET Framework 3.5 SP1 (BETA)
[2 Aug 2008 16:07] Philip Hofer
I ended up reformating the server and reinstalled everything from fresh downloads.  It still happens.
[4 Aug 2008 19:52] Philip Hofer
We can close this one out.

The Solve:

If the IIS7's servers default website has a bin directory with an older version of the .dll in it, some mapping somewhere found that .dll first and used it before looking at the other virtual hosted bin directory.

I basically checked all the bin directories on all the sites and that was the only one that had the older version in it, once I updated that one, all the rest stopped giving me semaphor errors.
[6 Aug 2008 14:54] Reggie Burnett
Closed on user's request.  This was a problem on the user's side.  Thanks for letting us know!