Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-05

"Aborting " a socket operation under Linux

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

I have a thread managing a socket (using synapse but I don't think that it matters, just keep in mind that each operation is blocking). Inside the thread, I try to connect, send and read in a loop. If I see an error, I just exit the loop and, if
the thread is terminated, that's it, otherwise I try a new connection and so on and so forth.
In the thread destructor (called in the context of the main thread), apart from terminate, I use the synapse AbortSocket call (which in turn calls CloseSocket, which in turn calls fpClose under linux), which supposedly terminates any pending call on the socket in the thread, so it can terminate immediately.
It works fine under windows (i.e the socket calls terminate immediately inside the thread), but under Linux it doesn't until the timeout expires, and that's not very convenient, especially if I'm trying to quickly close the program and I have many of those threads running. Is there a way to make the pending call exit immediately?

Bye

Luca
Rainer Stratmann
to Luca Olivetti

May be socketoption so_linger would be a solutuion. http://www.developerweb.net/forum/archive/index.php/t-2982.html

Rainer

Luca Olivetti wrote 18 lines
I have a thread managing a socket (using synapse but I don't think that it matters, just keep in mind that each operation is blocking). Inside the thread, I try to connect, send and read in a loop. If I see an error, I just exit the loop and, if the thread is terminated, that's it, otherwise I try a new connection and so on and so forth. In the thread destructor (called in the context of the main thread), apart from terminate, I use the synapse AbortSocket call (which in turn calls CloseSocket, which in turn calls fpClose under linux), which supposedly terminates any pending call on the socket in the thread, so it can terminate immediately. It works fine under windows (i.e the socket calls terminate immediately inside the thread), but under Linux it doesn't until the timeout expires, and that's not very convenient, especially if I'm trying to quickly close the program and I have many of those threads running. Is there a way to make the pending call exit immediately? Bye
Luca Olivetti
to Rainer Stratmann

En/na Rainer Stratmann ha escrit:

May be socketoption so_linger would be a solutuion. http://www.developerweb.net/forum/archive/index.php/t-2982.html

Nope, no change.
I'm testing a connect to a non existent host in the thread and it won't terminate before the (quite long, 3 minutes, since it ignores the 5 seconds I set) timeout, no matter what the value of linger->l_onoff

Thanks anyway

Show quoted text 25 lines
Rainer Am Freitag, 29. Mai 2009 18:43 schrieb Luca Olivetti: > I have a thread managing a socket (using synapse but I don't think that > it matters, just keep in mind that each operation is blocking). > Inside the thread, I try to connect, send and read in a loop. > If I see an error, I just exit the loop and, if > the thread is terminated, that's it, otherwise I try a new connection > and so on and so forth. > In the thread destructor (called in the context of the main thread), > apart from terminate, I use the synapse AbortSocket call (which in turn > calls CloseSocket, which in turn calls fpClose under linux), which > supposedly terminates any pending call on the socket in the thread, so > it can terminate immediately. > It works fine under windows (i.e the socket calls terminate immediately > inside the thread), but under Linux it doesn't until the timeout > expires, and that's not very convenient, especially if I'm trying to > quickly close the program and I have many of those threads running. > Is there a way to make the pending call exit immediately? > > Bye _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Rainer Stratmann
to Luca Olivetti
Luca Olivetti wrote 9 lines
En/na Rainer Stratmann ha escrit: > May be socketoption so_linger would be a solutuion. > http://www.developerweb.net/forum/archive/index.php/t-2982.html Nope, no change. I'm testing a connect to a non existent host in the thread and it won't terminate before the (quite long, 3 minutes, since it ignores the 5 seconds I set) timeout, no matter what the value of linger->l_onoff

In case of connection to a host:
Simply use nonblocking socket mode, than you get immediately a result. And then you can write your own timeout function.

procedure setnonblockingsocket( s : integer );
var  arg : longint;
begin
  arg := fpfcntl( s , F_GETFL );
  if arg >= 0 then begin
   arg := arg or O_NONBLOCK;
   fpfcntl( s , F_SETFL , arg );
  end;
end;
...
timeout := 0;
repeat
 c := fpconnect( c_socket , @inetaddr_client_remote ,
sizeof( inetaddr_client_remote ) );
 timeout := gettimeoutval;
until ( c = 0 ) or timeout;
Show quoted text 31 lines
Thanks anyway > Rainer > > Am Freitag, 29. Mai 2009 18:43 schrieb Luca Olivetti: >> I have a thread managing a socket (using synapse but I don't think that >> it matters, just keep in mind that each operation is blocking). >> Inside the thread, I try to connect, send and read in a loop. >> If I see an error, I just exit the loop and, if >> the thread is terminated, that's it, otherwise I try a new connection >> and so on and so forth. >> In the thread destructor (called in the context of the main thread), >> apart from terminate, I use the synapse AbortSocket call (which in turn >> calls CloseSocket, which in turn calls fpClose under linux), which >> supposedly terminates any pending call on the socket in the thread, so >> it can terminate immediately. >> It works fine under windows (i.e the socket calls terminate immediately >> inside the thread), but under Linux it doesn't until the timeout >> expires, and that's not very convenient, especially if I'm trying to >> quickly close the program and I have many of those threads running. >> Is there a way to make the pending call exit immediately? >> >> Bye > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Luca Olivetti
to Rainer Stratmann

En/na Rainer Stratmann ha escrit:

Show quoted text 13 lines
Am Freitag, 29. Mai 2009 20:29 schrieb Luca Olivetti: > En/na Rainer Stratmann ha escrit: >> May be socketoption so_linger would be a solutuion. >> http://www.developerweb.net/forum/archive/index.php/t-2982.html > Nope, no change. > I'm testing a connect to a non existent host in the thread and it won't > terminate before the (quite long, 3 minutes, since it ignores the 5 > seconds I set) timeout, no matter what the value of linger->l_onoff In case of connection to a host: Simply use nonblocking socket mode, than you get immediately a result. And then you can write your own timeout function.

Ok, the problem was that synapse used an integer instead of a timeval for SO_SNDTIMEO/SO_RCVTIMEO. I don't want to rewrite synapse and I want my program to work at least under windows and linux, so the nonblocking way is not an option.
*However* that's not my problem: I expect that a close would interrupt the socket operation (so the thread can terminate) and it doesn't.

Bye

Luca
Luca Olivetti
to Luca Olivetti

En/na Luca Olivetti ha escrit:

Show quoted text 21 lines
En/na Rainer Stratmann ha escrit: > Am Freitag, 29. Mai 2009 20:29 schrieb Luca Olivetti: >> En/na Rainer Stratmann ha escrit: >>> May be socketoption so_linger would be a solutuion. >>> http://www.developerweb.net/forum/archive/index.php/t-2982.html >> Nope, no change. > >> I'm testing a connect to a non existent host in the thread and it won't >> terminate before the (quite long, 3 minutes, since it ignores the 5 >> seconds I set) timeout, no matter what the value of linger->l_onoff > > In case of connection to a host: > Simply use nonblocking socket mode, than you get immediately a result. > And then you can write your own timeout function. Ok, the problem was that synapse used an integer instead of a timeval for SO_SNDTIMEO/SO_RCVTIMEO. I don't want to rewrite synapse and I want my program to work at least under windows and linux, so the nonblocking way is not an option. *However* that's not my problem: I expect that a close would interrupt the socket operation (so the thread can terminate) and it doesn't.

Well, I tried with a c program and it does the same (closing the socket from the main program doesn't interrupt the ongoing connect in a thread), so it's not a freepascal problem.
Googling for a way to abort the socket operation this mailing list thread is the first result, so I guess that there's no way to do it :-(

Bye

Luca