Skip to content

GC sockets - #269

Merged
eborden merged 2 commits into
masterfrom
gc-socket
Dec 15, 2017
Merged

eborden merged 2 commits into
masterfrom
gc-socket

Conversation

@kazu-yamamoto

Copy link
Copy Markdown
Collaborator

When a Socket becomes unreachable but is not explicitly closed by close, GC should collect it.
This fixes #4 and #236.

@kazu-yamamoto

Copy link
Copy Markdown
Collaborator Author

@eborden You can confirm this by this script:

module Main (main) where

import Control.Concurrent
import System.Mem
import Network.Socket

main :: IO ()
main = do
    createSocket
    gc "with socket out of scope"

createSocket :: IO ()
createSocket = do
  putStrLn "create new socket"
  sock <- socket AF_INET Stream defaultProtocol
  gc "with socket in scope"
  let MkSocket fd _ _ _ _ = sock -- this keeps sock.
  print fd
  putStrLn "socket goes out of scope now"

gc :: String -> IO ()
gc s = do
  putStrLn $ s ++ ": 1st wait"
  threadDelay 1000000
  putStrLn $ s ++ ": perform gc"
  performGC
  putStrLn $ s ++ ": 2nd wait"
  threadDelay 1000000
  putStrLn $ s ++ ": exit gc"

You should insert putStrLn into the second argument of mkWeakMVar (237 in Socket.hsc) to know the finalizer is fired.

@eborden eborden left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good from a glance. I'd like to spend a bit more time exploring this tomorrow.

@eborden

eborden commented Dec 15, 2017

Copy link
Copy Markdown
Collaborator

LGTM

@eborden
eborden merged commit b1fded7 into master Dec 15, 2017
@eborden
eborden deleted the gc-socket branch December 15, 2017 17:09
kazu-yamamoto added a commit to kazu-yamamoto/network that referenced this pull request Jan 29, 2018
* closing socket when it becames unreachable for GC.

* GHC 7.4 does not provide mkWeakMVar.
@vdukhovni vdukhovni mentioned this pull request Jan 30, 2018
3 tasks done
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

Successfully merging this pull request may close these issues.

Sockets don't have finalizers

2 participants