kuksugen / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

Mongoose does not exit when mg_stop is called if keep alive is active #268

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Enable Keep-alive
2. Connect from a browser
3. Shut-down mongoose (using mg_stop)

What is the expected output? What do you see instead?
Mongoose should stop after receiving the mg_stop. However, Mongoose hangs in 
the read-call in the pull (call-stack: worker_thread -> process_new_connection 
-> read_request -> pull ->read. The read is blocking as long as the client 
keeps the socket open. When the client closes the connection the socket is 
closed and Mongoose is able to finish the mg_stop call.

What version of the product are you using? On what operating system?
(Embedded) Mongoose V3.0 on Linux and Windows.

Please provide any additional information below.
The read blocks as long as the socket is kept open. There should be some 
mechanism added to trigger the read from another thread. This could be 
implemented by using a select before the read, and have select open a 2nd fd to 
which the mg_stop can write data to wake the select. As there is no data in the 
external socket the read-thread will not enter the (blocking) read call and can 
continue to the thread-exit.

Original issue reported on code.google.com by borkh...@gmail.com on 21 Jul 2011 at 6:13

GoogleCodeExporter commented 9 years ago
This could be related or a duplicate of issue 273

Original comment by jcmay...@gmail.com on 12 Aug 2011 at 1:59

GoogleCodeExporter commented 9 years ago
I think they are related but not a duplicate. The problem here is that the 
socket is kept open, even if a mg_stop call is issued. There has to be a way to 
signal the read to quit.

Original comment by borkh...@gmail.com on 12 Aug 2011 at 6:20

GoogleCodeExporter commented 9 years ago
I had this issue as well. The problem boils down to Mongoose using the 
synchronous recv call instead of an asynchronous one (similarly with 
send...which is related to another issue I'm hitting).

The short term fix is to call set_non_blocking_mode on the socket, and double 
check that all the functions calling pull can properly handle asynchronous 
mode. And then in those outer loops calling pull, do a check against 
conn->ctx->stop_flag.

Of course, this is a short term fix, as this doesn't protect against any sort 
of malicious attacks. So I suggest having configurable timeouts in these 
threads as well.

Original comment by chester....@gmail.com on 14 Sep 2011 at 4:48

GoogleCodeExporter commented 9 years ago
Hi,
I’m working on Android platform and have the same problem. As this issue is 
very similar to bug #273 I tried the suggested solution there:
Adding the following code.
Then in the "process_new_connection" function after the line
  keep_alive_enabled = !strcmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes");

Add the following:

  if ( keep_alive_enabled )
  {
      // Enable read timeouts on the socket
      DWORD tv = 15 * 1000; //15 sec, probably should go in the config file...
      if (setsockopt(conn->client.sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,  sizeof(tv)))
      {
          cry(conn, "setsockopt failed");
      } 
  }

The problem is that setsockopt always returns -1. When I run the same code on 
WIN32 it’s working.
In the setsockopt documentation for RCVTIMEO it says “Note that not all 
implementations allow this option to be set”.
http://pubs.opengroup.org/onlinepubs/009604599/functions/setsockopt.html

is there a work around for Android?
Is this issue fixed on mongoos 3.1?
Any help will be very much appreciated! 
Guy Shachar
ggguuyyy@gmail.com

Original comment by ggguu...@gmail.com on 14 Feb 2012 at 8:41

GoogleCodeExporter commented 9 years ago
Solved this issue by adding a select-call to pull. The second socket used here 
is written when mg_stop is called.

Attached a diff file (diff to latest version in repository) containing the 
change.

Original comment by borkh...@gmail.com on 21 Jun 2012 at 10:03

Attachments:

GoogleCodeExporter commented 9 years ago
Also see http://code.google.com/r/borkhuis-issue268/source/browse for the new 
version.

Original comment by borkh...@gmail.com on 21 Jun 2012 at 1:41

GoogleCodeExporter commented 9 years ago
This must be fixed now.
Mongoose does select() in read_request, and exits if mg_stop is called.
Please verify.

Original comment by valenok on 23 Sep 2012 at 12:57

GoogleCodeExporter commented 9 years ago
Issue 273 has been merged into this issue.

Original comment by valenok on 23 Sep 2012 at 12:58

GoogleCodeExporter commented 9 years ago
This does appear to be working for me now. At least on Windows builds.

Original comment by jcmay...@gmail.com on 6 Nov 2012 at 9:30

GoogleCodeExporter commented 9 years ago
Awesome, thanks for clarifying.

Original comment by valenok on 25 Dec 2012 at 11:26