Sign in Join

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

Mailing lists / fpc-devel / 2011-10

fcl-web: A proper way to set SO_REUSEADDR socket option in THTTPApplication

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Vladimir Zhirov

Hi,

Currently, if THTTPApplication-based program on Linux was stopped manually during active connection I cannot start it again for 1-2 minutes, getting the exception like this:

exception at 0805304A: Binding of socket failed: 8080.

According to
http://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux setting SO_REUSEADDR option for server socket of embedded fcl-web server would allow to restart the application immediately after shutdown.

I wonder what is the proper way of implementing this option:

1) Should the setter code go to fcl-net's ssockets.pp and become "function TSocketServer.SetSocketOption"? Or "property TSocketServer.ReuseAddress: Boolean"? Or should I use fpSetSockOpt directly on
TSocketServer.Socket without changes to TSocketServer itself?

2) Should this option be hardcoded in
TFPCustomHttpServer.CreateServerSocket, or configurable via TCustomHTTPApplication?

Reply by email

Andrew Brunner
to Vladimir Zhirov

I think you should also look into setting linger options at a lower rate than present values. 120 seconds is too long. 15-30 seconds is a reasonable amount of time to give a dead socket.

In fpc componetent design, I would recommend to also make sure that sockets issue SHUT_DOWN for Both read and write before issuing close.

The poster's problems are infact, common linux behaviour, and can lead to permant socket non-resuabiltily if the kernel does not get the shutdown and/or close / and linger is not set. Since most kernel developers EXPECT users to close their sockets properly.

Vladimir Zhirov wrote 30 lines
Hi, Currently, if THTTPApplication-based program on Linux was stopped manually during active connection I cannot start it again for 1-2 minutes, getting the exception like this: > exception at 0805304A: > Binding of socket failed: 8080. According to http://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux setting SO_REUSEADDR option for server socket of embedded fcl-web server would allow to restart the application immediately after shutdown. I wonder what is the proper way of implementing this option: 1) Should the setter code go to fcl-net's ssockets.pp and become "function TSocketServer.SetSocketOption"? Or "property TSocketServer.ReuseAddress: Boolean"? Or should I use fpSetSockOpt directly on TSocketServer.Socket without changes to TSocketServer itself? 2) Should this option be hardcoded in TFPCustomHttpServer.CreateServerSocket, or configurable via TCustomHTTPApplication? _______________________________________________ fpc-devel maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Michael Van Canneyt
to Vladimir Zhirov: Currently, if THTTPApplication-based program on Linux was stopped manually during active…
Vladimir Zhirov wrote 22 lines
Hi, Currently, if THTTPApplication-based program on Linux was stopped manually during active connection I cannot start it again for 1-2 minutes, getting the exception like this: > exception at 0805304A: > Binding of socket failed: 8080. According to http://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux setting SO_REUSEADDR option for server socket of embedded fcl-web server would allow to restart the application immediately after shutdown. I wonder what is the proper way of implementing this option: 1) Should the setter code go to fcl-net's ssockets.pp and become "function TSocketServer.SetSocketOption"? Or "property TSocketServer.ReuseAddress: Boolean"? Or should I use fpSetSockOpt directly on TSocketServer.Socket without changes to TSocketServer itself?
I plan a set of socket options in TSocketServer;
I have the same problem as you, I just need a free moment to implement it :-)
2) Should this option be hardcoded in TFPCustomHttpServer.CreateServerSocket, or configurable via TCustomHTTPApplication?

Configurable seems like the best option.

Michael.

Reply by email

Michael Van Canneyt
to Andrew Brunner: I think you should also look into setting linger options at a lower rate than present…
Andrew Brunner wrote 6 lines
I think you should also look into setting linger options at a lower rate than present values. 120 seconds is too long. 15-30 seconds is a reasonable amount of time to give a dead socket. In fpc componetent design, I would recommend to also make sure that sockets issue SHUT_DOWN for Both read and write before issuing close.

Not without a linger option implemented, otherwise this has very bad side-effects on windows.

Michael.

Reply by email

Vladimir Zhirov
to Michael Van Canneyt: I plan a set of socket options in TSocketServer; I have the same problem as you, I just…

Thanks Michael and Andrew.

Michael Van Canneyt wrote
I plan a set of socket options in TSocketServer; I have the same problem as you, I just need a free moment to implement it :-)

Great, then I'll wait for your implementation.

I've done some changes in my local copy to get things working until better implementation is ready.
In case you are interested I'm attaching a diff of these changes (sorry, they got mixed with my earlier patch for #20370)

Configurable seems like the best option.

In the attached diff there is boolean ReuseAddress property for TCustomHTTPApplication. The drawback of this approach is that it's not uniform with TCustomFCgiApplication.ProtocolOptions (serving the same purpose for FCGI application). The advantage is that it does not declare new data structures and thus does not force us to add more lower-level units into main program's uses clause.

Reply by email