Abort CGI script execution when client closes connection?

109 views
Skip to first unread message

Victor

unread,
Jul 10, 2017, 11:44:27 AM7/10/17
to civetweb
Hi,

I am trying to figure out a way to abort a script when the client closes the connection. I have AJAX requests on the client side which serve the search results to the users. As queries are sometimes complex and can take multiple seconds to compute, i would like to reap any child CGI process in civetweb that is not needed anymore (as, for example, the user changed the query by just 1 character). Can you please help me with this?
Is this part anyhow related to what i am trying to accomplish:
#if !defined(_WIN32) && !defined(__SYMBIAN32__)
/* Ignore SIGPIPE signal, so if browser cancels the request, it
 * won't kill the whole process. */
    (void)signal(SIGPIPE, SIG_IGN);
#endif /* !_WIN32 && !__SYMBIAN32__ */


Regards,
Victor

bel

unread,
Jul 17, 2017, 2:56:31 PM7/17/17
to civetweb
I'm not sure how far this is possible in practice.

Connections can be closed or broken for several reasons and with several different close/shutdown sequences.
I don't know what browsers do when you close a tab, or when you hit reload, and I don't know if this is the same for all browsers.
It's maybe different for PUT/POST and GET requests as well - or different if you abort/reload/close while sending POST/PUT data from the browser to the server or reading the response from the server.
Network failure is a different issue, e.g. if you use mobile data in areas with less than perfect reception. It is not possible for principal reasons, to detect a network failure in a second, without false positives (considering active connections as broken).


The signal(SIGPIPE, SIG_IGN) does indeed tell a server running on a Linux operating system, to ignore if a client connection is closed by the client - otherwise, if one client connection is closed this way, the entire server stops (for all clients).
See https://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly
Various alternatives are by no means portable between different operating systems.

Depending on what you want to cover or ignore, it might be able to modify `handle_cgi_request` accordingly.

Victor

unread,
Jul 18, 2017, 6:04:18 AM7/18/17
to civetweb
Hi bel,

Thanks a lot, very detailed explanation. I tested 2 browsers and they seem to send the "FIN, ACK" when i cancel the request (either programmatically or via the Esc button) and also when closing the tab. When refreshing, the behavior looks the same (FIN, ACK client->server followed by an ACK server->client) and only then the new request starts. Now i begin to think it might be considered a bug, because if server does get the FIN and ACKs this request, it theoretically should be able to kill the respective CGI, at least as an option.
 Any subsequent tries to send the data over this connection to the client would result in RSTs from the client (and i think the RSTs that i could see in the wireshark might be those data from a zombie CGI indeed?).

Is there an easy way for non-C programmer to send kill(pid, SIGKILL); or trigger the "goto done" part of handle_cgi_request asynchronously for a given socket when server receives FIN, ACK on it?

Sincerely,
Victor

Victor

unread,
Jul 18, 2017, 6:32:13 AM7/18/17
to civetweb
Now after reading more about what FIN is, i get it that probably it is not as simple as i though as the client still can receive the data from the CGI? Nevertheless, i will continue to pursue my specific problem. I hope that my AJAX clients indeed send the FIN, ACK when they don't need the data/connection anymore -> terminate the CGI. I mentioned this can take seconds for an input suggestion, but i also have scientific computations which may require minutes and this could be detrimental to the server's performance.

bel

unread,
Jul 18, 2017, 7:35:16 AM7/18/17
to civetweb


On Tuesday, July 18, 2017 at 12:32:13 PM UTC+2, Victor wrote:
Now after reading more about what FIN is, i get it that probably it is not as simple as i though as the client still can receive the data from the CGI? Nevertheless, i will continue to pursue my specific problem. I hope that my AJAX clients indeed send the FIN, ACK when they don't need the data/connection anymore -> terminate the CGI. I mentioned this can take seconds for an input suggestion, but i also have scientific computations which may require minutes and this could be detrimental to the server's performance.


FIN and ACK are package types - I think you can understand them best by looking at the TCP state machine:

http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm

But the standard socket API does not directly reflect this.
If you want to access such details, you must look in the operating system and even operating system version specific extensions. What OS are we talking about? Is portability an issue?

In your above requirements, you only care about clients properly closing the connection (sending FIN/ACK), not about lost connections (network failures without any message). So you can ignore broken connections? If server and client are in the same LAN, most likely you can. If you consider mobile clients accessing the server through the Internet, you certainly should not ignore broken connections.

If you have long running calculations, I would not do it in the CGI script anyway. You could do a HTML site to control the calculation process. The "onclose" Javascript method can be used to inform the server the client closed the connection.




Reply all
Reply to author
Forward
0 new messages