-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Long-running application and closed connections #196
Comments
Chris mentioned {-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent (threadDelay)
import Control.Exception (bracket)
import Control.Monad (forever)
import Data.Conduit.Network (bindPort)
import Network.Socket (accept, isConnected, sClose)
main :: IO ()
main = bracket (bindPort 4000 "*") sClose $ \listener -> forever $ do
(incoming, _) <- accept listener
forever $ do
isConnected incoming >>= print
threadDelay 1000000 |
Submitted pull request to network to document this: haskell/network#116 |
Bump, this would be nice to have. I'm implementing a sockjs wai middleware and some tests in the specification require the server to close the connection if a poll request is aborted - however there is no way to detect this in warp/wai now. If i understand it correctly one can detect the socket disconnect by checking whether recv returns a zero length bytestring. The only problem is that whatever thread is checking this needs to eagerly buffer incoming data just to see whether the connection has been dropped while the app is processing. Not fun. However the user could provide a maximum buffer size - then the server could detect disconnects as long as unprocessed incoming data fits in this buffer. I think at least for polling protocols this would be a good enough "best effort" solution - polling requests are pretty much empty after all |
I just ran into this when using a |
I'm not sure what the state of this issue is. Was something implemented for this purpose? |
Current behavior seems to still not notice that the connection was gone. The post http://stefan.buettcher.org/cs/conn_closed.html describes a way to get async notification about a closed connection: It would be nice if warp supported excepting the application early, but at least added to the docs. In the mean time, some workarounds to consider, depending on
Could this be reopened? @snoyberg |
I can reopen, but I'm not planning on looking into this myself. If you're interested in taking a stab at this, feel free. However, given the likely dangerous nature of introducing more asynchronous exceptions at surprising points of the codebase, I can't guarantee anything will get merged in. |
Hi, I'm trying to build a simple web server with WARP. |
I have a question about this: how would middlewares act then? For example, a client terminates the connection midway while transmitting the request body. If I'm getting this correctly, anything that consumes the request body will never terminate? Will the body logger ever terminate? I've been debugging a memory leak in our API gateway service for months now, without any success. And I just want to rule out the issue having an external source. |
Here is how other programs handle this:
It looks like the GHC IO Manager does not yet have a way to use Perhaps there's a different way we can detect disconnects, e.g. with a |
I created a proof-of-concept using the networking primitives available in userland so far: robinp@31a89b4 It is not very nice, but not too horrible either. When testing locally, got the job done. Maybe it sparks some ideas. |
I believe this is the cause of snoyberg/keter#29. It's not necessarily a bug in Warp, but it's a design point that requires some consideration. Consider the following interaction between an HTTP client, Warp, and a WAI application:
What we'd theoretically want is some kind of a notification in the Application when the connection is closed. Either the Application could do some polling to determine that, or an async exception could be thrown whenever the connection is closed. One idea would be for WAI to include a new field,
clientIsListener :: IO Bool
. That field wouldn't necessarily be accurate, so perhaps aMaybe Bool
response would be more accurate.Another possibility is to say that the current behavior is acceptable, and it's the responsibility of the Application to institute some kind of a heartbeat.
Pinging @chrisdone and @jwiegley.
The text was updated successfully, but these errors were encountered: