Skip to content

Conversation

@kazu-yamamoto
Copy link
Collaborator

Currently sockets are unintentionally GCed in non-loop code while API is blocked.
The first commit shows it.

This tends to happen where fdSocket is used. For safety, the second commit introduced withFdSocket.

The third and fourth commits use withFdSocket instead of fdSocket, resulting in passing the test.

This takes over #398.

kazu-yamamoto and others added 4 commits April 22, 2019 10:50
`fdSocket` is fairly unsafe, because the socket may be finalized
(and therefore closed) while the file descriptor is still being
used. `withFdSocket` offers a safer way to use the file descriptor.
@kazu-yamamoto kazu-yamamoto requested a review from Mistuke April 22, 2019 02:45
@kazu-yamamoto
Copy link
Collaborator Author

Note that build history of CI shows that the bug has been fixed.

@kazu-yamamoto
Copy link
Collaborator Author

@fumieval Would you review this?

@kazu-yamamoto
Copy link
Collaborator Author

@Mistuke I'm now sure but should we do the same thing to socket2FD for Windows?

Copy link
Contributor

@fumieval fumieval left a comment

Choose a reason for hiding this comment

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

This commit looks fine otherwise.

_ <- forkIO gc
_ <- forkIO connect'
(_sock', addr) <- accept sock
-- check if an exception did not thrown.
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: is not thrown?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. I will fix.

threadDelay 200000
sock <- socket AF_INET Stream defaultProtocol
connect sock $ SockAddrInet 6000 $ tupleToHostAddress (127, 0, 0, 1)
it "can be GCed but not GCed when blocking" $ do
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this test that it gets GC'd? I thought what we want to make sure is that it does not get GC'd.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Historically, Socket cannot be GCed. The major feature of version 3 is that Socket can be GCed. So, Socket should be GCed in proper time. Anyway, I will consider how to rephrase it.

import GHC.Conc (closeFdWith)
import System.Posix.Types (Fd)
import Control.DeepSeq (NFData (..))
import GHC.Weak (Weak (..))
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm it looks like I forgot to fix redundant imports.

@fumieval
Copy link
Contributor

I added two commits to fix the warnings: 467eeeb and 58ca485 (tsurucapital:withFdSocket)

@kazu-yamamoto
Copy link
Collaborator Author

I added three commits based on @fumieval's comments.

@fumieval
Copy link
Contributor

LGTM

@Mistuke
Copy link
Collaborator

Mistuke commented Apr 22, 2019

@kazu-yamamoto hmm the only usage of socket2FD is one where the original socket does not require to stay in scope. So I think we're fine there.

Copy link
Collaborator

@Mistuke Mistuke left a comment

Choose a reason for hiding this comment

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

Change LGTM

threadDelay 200000
sock <- socket AF_INET Stream defaultProtocol
connect sock $ SockAddrInet 6000 $ tupleToHostAddress (127, 0, 0, 1)
it "should not be GCed while blocking" $ do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding a test 🥇

kazu-yamamoto added a commit to kazu-yamamoto/network that referenced this pull request Apr 22, 2019
@kazu-yamamoto kazu-yamamoto merged commit 16df0ad into haskell:master Apr 22, 2019
@kazu-yamamoto kazu-yamamoto deleted the gc-socket branch April 22, 2019 23:33
@kazu-yamamoto
Copy link
Collaborator Author

Merged. Thank you, all guys, for your contribution!

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.

5 participants