AndroidAsync icon indicating copy to clipboard operation
AndroidAsync copied to clipboard

stop() Doesn't Stop the AsyncHttpServer

Open commonsguy opened this issue 9 years ago • 8 comments

Based on the JavaDocs, I had presumed that stop(), called on an AsyncHttpServer, would revert the listen() call and stop the server from listening on the port. That does not seem to the case.

This project demonstrates the problem: https://github.com/commonsguy/cw-omnibus/tree/master/Diagnostics/WebServer

There are two launcher activities for a debug build. One brings up a list of recent Stack Overflow android questions, using Picasso. The other starts a service that in turn starts an AsyncHttpServer, listening on port 4999. If you bring up the / page on that port, you get some stats from Picasso about the images that were downloaded. There is a link to a /stop page that calls stopSelf() on the service, and in onDestroy(), the service calls stop() on the AsyncHttpServer. I have confirmed that onDestroy() is called and stop() is called as expected. However, the AsyncHttpServer continues to listen on that port and serve pages until the app's process is terminated.

My apologies if stop() is not the correct way to stop an AsyncHttpServer.

commonsguy avatar May 16 '15 20:05 commonsguy

Yeah, I think there's a bug where keep alive sockets are not cleaned up on the http server.

koush avatar May 16 '15 21:05 koush

Well, a feature really :)

So the server keeps running with any existing connections, but stops allowing any new connection.

You can stop the underlying AsyncServer (the reactor thread) instance to kill all the associated sockets.

koush avatar May 16 '15 21:05 koush

I can confirm that AsyncServer.getDefault().stop() does shut down the sockets and such -- thanks!

Consider this a feature request to have a stop(boolean) or janeStopThisCrazyThing() or something on AsyncHttpServer that does AsyncServer.getDefault().stop(). stop(boolean) would take a flag to indicate whether to do just what stop() does today or whether it should also stop() the AsyncServer. IMHO, the fact that AsyncHttpServer uses AsyncServer is an implementation detail, and the current approach for fully stopping the server forces us to know about, and rely upon, that detail.

Thanks again!

commonsguy avatar May 16 '15 21:05 commonsguy

will do

koush avatar May 16 '15 21:05 koush

I ran into the same issue which took me 3 hours to find this issue.

The feature per se is good to have something the Google folks call "lameducking", meaning that the server will continue to server ongoing requests until they are finished.

I suggest something like

stopListening()
terminate()

where the first has the old meaning and the second will also close all connections to the webserver. And if this is the wish well, I'd like to have a way to terminate(long millis) that will terminate after the last connection has closed or the millis have passed.

One remark: The AsyncServer.getDefault().stop() will probably kill all servers, which might not what you want to have if you run more than one AsyncHttpServer.

kurthuwig avatar Jan 25 '17 07:01 kurthuwig

+1 for separate methods with clear names. Maybe stop(boolean closeOpenSockets)

weir avatar Aug 03 '17 20:08 weir

You can call AsyncServerSocket.stop to stop listening on the port (lameducking). https://github.com/koush/AndroidAsync/blob/master/AndroidAsync/src/com/koushikdutta/async/AsyncServerSocket.java

You can call AsyncServer.stop to shut everything down. https://github.com/koush/AndroidAsync/blob/master/AndroidAsync/src/com/koushikdutta/async/AsyncServer.java#L249

You can also run AsyncHttpServers on separate AsyncServers, to independently manage them.

koush avatar Aug 03 '17 22:08 koush

Any news? I have the same problem, I will start Http server and web socket server but I only want to stop one of them.

t104360088 avatar Aug 28 '23 07:08 t104360088