jaspervdj / websockets-snap

Snap integration for the websockets library
Other
33 stars 21 forks source link

Ping thread never exits #21

Closed Yuras closed 5 years ago

Yuras commented 7 years ago

After upgrading snap and websockets I see memory leak in my application. To reproduce, create simple websockets server:

wsApp :: Snap ()
wsApp = WebSockets.runWebSocketsSnap $ \pc -> do
  print "client"
  c <- WebSockets.acceptRequest pc
  threadDelay (10000000)
  print "done"

Then run in with +RTS -S -RTS, make a number of connections and check memory usage. It grows even if the handler exists after 10 seconds. Disabling the built-in ping thread fixes the issue:

wsApp :: Snap ()
wsApp = WebSockets.runWebSocketsSnap $ \pc -> do
  print "client"
  c <- WebSockets.acceptRequest pc
    { WebSockets.pendingOnAccept = const $ return ()
    }
  threadDelay (10000000)
  print "done"

My conclusion: ping thread doesn't exit for some reason.

jaspervdj commented 7 years ago

This is surprising -- an exception should be raised when it tries to write to the socket.

I'll see if we can circumvent the problem by explicitly killing the thread -- that's a bit better anyway since we can get rid of the thread faster.