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

send should check socket status and fail if the socket is closed #169

Closed
Yuras opened this issue May 29, 2015 · 8 comments
Closed

send should check socket status and fail if the socket is closed #169

Yuras opened this issue May 29, 2015 · 8 comments
Assignees

Comments

@Yuras
Copy link
Contributor

Yuras commented May 29, 2015

The Network.Socket.close claims that

All future operations on the socket object will fail

It changes socket state to Closed and calls close(2) C function, so OS is free to reuse the FD.

But Network.Socket.send and friends don't check socket status. When sending data to a closed socket usually an exception is thrown (close(2) returns EBADF), but there is a chance that the FD is reused after close but before send, so the data will be silently sent to a wrong destination.

One can argue that writing to a closed socket is a bad practice anyway, then please consider it as a documentation bug.

@Yuras
Copy link
Contributor Author

Yuras commented Feb 6, 2016

Bump.

Will you accept a PR fixing the code to match the documentations? Note that is will add one withMVar per send and recv, also it will serialize sending and receiving.

Alternatively will you accept a PR fixing the documentation to match the implementation?

@eborden
Copy link
Collaborator

eborden commented Feb 6, 2016

Please submit the PR and we can discuss the cost and benefit. Checking seems like the right thing to do.

@Yuras
Copy link
Contributor Author

Yuras commented Feb 6, 2016

@eborden Thank you for your response!

Which PR should I send? The one that fixes the code or that fixes documentation?

I don't think withMVar really makes sense here. @Peaker suggested read/write lock, but it is out of my abilities because it is too intrusive and I can't test all CPP configurations.

Checking socket status without locking can help to make the issue more visible, though it will not really fix. I think that fixing documentation is the first step.

Yuras added a commit to Yuras/network that referenced this issue Feb 7, 2016
Add a note that sending data to or receiving data from closed
socket in unsafe. See haskell#169
@eborden
Copy link
Collaborator

eborden commented Feb 7, 2016

I'd prefer the bug fix over a doc fix. A naive implementation using an mvar can get the ball rolling and we can refine from there. Likely the read/write lock is the final destination, but high level code can suss out some details first. Thanks for being persistent with this issue.

Yuras added a commit to Yuras/network that referenced this issue Feb 7, 2016
It is unsafe to write or read to closed FD because OS is free
to reuse it, so it may lead to writing to some other destination.
See haskell#169
@kazu-yamamoto kazu-yamamoto self-assigned this Apr 26, 2016
@kazu-yamamoto
Copy link
Collaborator

I discussed this with my friend.

  • This bug is serious and should be fixed somehow.
  • I don't want to modify the current API. The pull request introduces significant overhead. I would avoid this way.
  • So, let's create Safe module. send and other APIs in Safe module checks the socket status.

What do you think, guys?

@Yuras
Copy link
Contributor Author

Yuras commented Apr 26, 2016

It is definitely better then doing nothing. And the documentation for the old API should be updated to describe the issue and point to Safe module.

Yuras added a commit to Yuras/network that referenced this issue Jun 15, 2016
Add a note that sending data to or receiving data from closed
socket in unsafe. See haskell#169
@enolan
Copy link
Contributor

enolan commented Jul 16, 2016

I'm working on a PR with a Network.Socket.Safe module, along with Network.Socket.ByteString.Safe and Network.Socket.ByteString.Lazy.Safe.

enolan pushed a commit to enolan/network that referenced this issue Jul 16, 2016
Add a note that sending data to or receiving data from closed
socket in unsafe. See haskell#169
enolan added a commit to enolan/network that referenced this issue Jul 16, 2016
enolan added a commit to enolan/network that referenced this issue Jul 17, 2016
kazu-yamamoto pushed a commit that referenced this issue Jul 26, 2016
Add a note that sending data to or receiving data from closed
socket in unsafe. See #169
@kazu-yamamoto
Copy link
Collaborator

I close this in favor of #212.

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

4 participants