[Bsd-sharp-list] Socket Connect and BeginConnect issues with threads

Robert Wojciechowski robertw at ssginnovations.com
Thu Mar 1 21:03:42 MST 2007


I have been working on a problem in the last few days related to
threading and sockets on FreeBSD when I started noticing that a threaded
database application that uses Npgsql would throw the following
exception randomly (and not very frequently) when connecting to our
PostgreSQL database at localhost:5432:

  Unhandled Exception: Npgsql.NpgsqlException:
  Socket is already connected
    at Npgsql.NpgsqlClosedState.Open (Npgsql.NpgsqlConnector context)
[0x00000] 
    at Npgsql.NpgsqlConnector.Open () [0x00000] 
    at Npgsql.NpgsqlConnectorPool.GetPooledConnector
(Npgsql.NpgsqlConnection Connection) [0x00000] 
    ...snip...

Npgsql uses Socket.BeginConnect to establish a connection to PostgreSQL
(in the latest SVN), but why would I get an EISCONN from
Socket.BeginConnect? I tried reverting to a blocking Socket.Connect call
but it still happened once in a while. After researching further, things
started to point towards the Mono win32 compatibility layer and the
_wapi_connect method in io-layer/sockets.c, not my threaded application
or Npgsql.

It seems I'm getting the EISCONN errno back from connect() as it runs in
a do/while loop. The first call must not succeed for whatever reason
(EINTR from a signal?) and the second one gives EISCONN which mono
throws back up as an error because it doesn't know how to handle it.

The first patch attached checks the errno for EISCONN and treats it as a
success. That seemed to still throw exceptions throughout the day,
though. The second patch should be a little more robust -- it has been
adapted from the following page:

  http://www.madore.org/~david/computers/connect-intr.html

I'm testing that out right now on my server. It's hard to trace down
because I don't have a test case that causes this behavior... I just
have to wait for it to happen again until I can create one.

I also wonder if this problem is related to bug #80839, a problem with
Socket.BeginConnect on Solaris. Hmm!

Anyone have any ideas if I'm on the right path to fixing this problem?

-- Robert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mono-sockets.c.patch
Type: application/octet-stream
Size: 254 bytes
Desc: mono-sockets.c.patch
Url : http://forge.novell.com/pipermail/bsd-sharp-list/attachments/20070301/780659e5/mono-sockets.c.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mono-sockets.c.patch2
Type: application/octet-stream
Size: 2193 bytes
Desc: mono-sockets.c.patch2
Url : http://forge.novell.com/pipermail/bsd-sharp-list/attachments/20070301/780659e5/mono-sockets.c-0001.obj


More information about the Bsd-sharp-list mailing list