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

Robert Wojciechowski robertw at ssginnovations.com
Fri Mar 2 13:08:37 MST 2007


Created a bug at

  http://bugzilla.ximian.com/show_bug.cgi?id=81010

-- Robert 

> -----Original Message-----
> From: Robert Wojciechowski 
> Sent: Thursday, March 01, 2007 11:04 PM
> To: bsd-sharp-list at forge.novell.com
> Cc: 'Francisco Figueiredo Jr.'
> Subject: Socket Connect and BeginConnect issues with threads
> 
> 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


More information about the Bsd-sharp-list mailing list