Hi,

I'm getting unhandled exceptions from calls to
LdapConnection.Disconnect() when setting when setting
connection.SecureSocketLayer = true and pointing the connection to an
open non-SSL LDAP port. There is currently a bug open with Novell on
this (opened by someone else), but, as Novell hasn't responded sine
1/14/2008, I thought I'd see if anyone on the mailing list has
experienced a similar issue or has any insight.

Here is the link to the bug report:
http://devzilla.novell.com/ldapcshar..._bug.cgi?id=11

To paraphrase the bug report:

It seems that the shutdown() method in Connection.cs is not thread safe.
Calls to LdapConnection.Disconnect(), the dying
Connection.ReaderThread.Run(), and the Connection destructor all call
shutdown(). This leads to unhandled exceptions coming out of the Ldap
libary.

The Novell LDAP C# library comes with sample code. I've modified the
SSL code sample SecureBind.cs to illustrate the problem. Note that port
389 is an open but non-SSL port on my server. Also note that the
try-catch around the call to conn.Disconnect() will NOT catch the
exceptions in the shutdown() method because they are on a different
thread (at least for .net >= 2.0). Thus, these exceptions can bring the
whole app down.
Code:

static void Main(string[] args)
{

for (int i = 0; i < 100; i++)
{
LdapConnection conn=null;
try
{
conn= new LdapConnection();
conn.SecureSocketLayer=true;
Console.WriteLine("Connecting...");
conn.Connect("192.168.1.60",389);
conn.Bind("user","pass");
Console.WriteLine(" SSL Bind Successfull");

}
catch(Exception e)
{
Console.WriteLine("Error:" + e.Message);


}
try
{
conn.Disconnect();
}
catch(Exception ex)
{
Console.WriteLine("Caught exception from
LdapConnection.Disconnect()");
Console.WriteLine("Exception is: {0}", ex.Message);
}
}
}

The reason for the loop is that the problem does not happen every time,
just often. This is due to the threading nature of the problem.

Here is the exception that I get:

Exception is (sorry, had to manually type this):
Unhandled Exception: System.Net.Sockets.SocketException: The socket is not
connected
at System.Net.Sockets.Socket.Shutdown (SocketShutdown how)
at Novell.Directory.Ldap.Connection.shutdown(System.S tring reason, Int32
semaphoreId, ...)
at Novell.Directory.Ldap.Connection+ReaderThread.Run( )

Finally, here is how I've modified Connection.cs to get the above sample
code to succeed. This is approximately lines 1130-1160 of Connection.cs.

if (socket != null || sock != null)
{
// Just before closing the sockets, abort the reader thread
if ((reader != null) && (reason != "reader: thread stopping"))
reader.Abort();
// Close the socket
try
{
if(Ssl && sock != null)
{
if (sock.Connected)
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
else if (socket != null)
{
if(in_Renamed != null)
in_Renamed.Close();

socket.Close();
}
}
catch (System.Net.Sockets.SocketException)
{
// ignore problem closing socket
}
socket = null;
sock = null;
in_Renamed=null;
out_Renamed=null;
}
}

I do not think these changes solve the bug, the only mask it. The root
cause is that this method is not thread safe. I see this problem with
version 2.1.10 and 2.1.9 of the Novell LDAP library. I'm using Mono
1.2.6 on SLES 10.

Has anyone else seen a similar issue?