Daisuke Shimamoto
Daisuke Shimamoto

Reputation: 5316

Should WebSockets be closed?

I was looking at some WebSocket library for Haskell, namely Wuss and found something that might render it unusable.

The latest commit (04ab5d3f72), uses Control.Exception.Bracket and always closes the connection.

Wuss.hs#L163

runSecureClientWithConfig host port path config options headers app = do
    context <- Connection.initConnectionContext
    Exception.bracket
        (Connection.connectTo context (connectionParams host port))
        Connection.connectionClose     -- <-- This line closes the connection
        (\connection -> do
            stream <-
                Stream.makeStream (reader config connection) (writer connection)
WebSockets.runClientWithStream stream host path options headers app)

If I'm not mistaking, shouldn't this function keep the connection open as it's a WebSocket (or close it only if something errored)?

I would be surpirsed for a library with recent commits (2017) to have such a bug and I'm no expert in Haskell nor in WebSockets so I might just be missing something...

Upvotes: 2

Views: 151

Answers (1)

Paul Johnson
Paul Johnson

Reputation: 17786

The idea of this function is that it runs app, and once app terminates the resources it used are recovered.

Typically app will be a long-running thing which listens at the port for connections and does the appropriate thing when one arrives. So closing the port and recovering the resources is the Right Thing. The previous version didn't do this, leading to a resource leak.

Upvotes: 4

Related Questions