Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting whether a client vanished #953

Open
Profpatsch opened this issue Nov 15, 2023 · 3 comments
Open

Detecting whether a client vanished #953

Profpatsch opened this issue Nov 15, 2023 · 3 comments

Comments

@Profpatsch
Copy link
Contributor

Is there a way to detect whether a client has vanished during a request?

setOnClose will only fire at the very end as far as I can see.

The following code tries to use responseStream, and by flushing before every delay I hoped it would realize the client is gone:

bar :: IO ()
bar = Warp.runSettings
  ( Warp.defaultSettings
      & Warp.setPort 1234
      & Warp.setOnClose (\sockAddr -> print "connection vanished")
  )
  $ \req respond -> do
    respond $ Wai.responseStream ok200 [] $ \write flush -> do
      print "sleeping"
      traverse_
        ( \_ -> do
            flush
            print "loop"
            threadDelay 1_000_000
        )
        [1 .. 10]
      write "done"
      print "done"

When I run this and start a curl http://localhost:1234 which I Ctrl+c after ~1-2 seconds, it will print:

"sleeping"
"loop"
"loop"
"loop"
"loop"
"loop"
"loop"
"loop"
"loop"
"loop"
"loop"
"done"
"connection vanished"

regardless of my client going away.

There is an old-ish SO question that creates a workaround by trying to read from the client socket in regular intervals.

@Vlix
Copy link
Contributor

Vlix commented Nov 30, 2023

This unfortunately is a long-standing issue (cf. #196 & #914), but there's plans to add functionality to GHC to get access to that information (cf. #937)

@Profpatsch
Copy link
Contributor Author

I came to the conclusion that for our use-case, it would be best to add functionality like this to the application protocol, because layer 2/3 connection information really depends on the setup; e.g. when adding a proxy it might break the connection detection again.

@Vlix
Copy link
Contributor

Vlix commented Mar 4, 2024

Could you elaborate on that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants