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

net: Setting a deadline to a net.TCPConn has no effect if there was a "File()" method call. #7605

Closed
gopherbot opened this issue Mar 21, 2014 · 2 comments

Comments

@gopherbot
Copy link

by ilarkin@crystalnix.com:

What does 'go version' print?

  go version go1.2 linux/amd64


What steps reproduce the problem?

  Code: http://play.golang.org/p/TasQabnlRu

    1. Create a new *net.TCPConn (using net.Accept)
    2. Call File()
    3. Close new file
    4. Set Read Deadline to 5 seconds
    5. Start reading


What happened?

  The reading is never finishes. As the result i see the following output:

    > New Connection: 127.0.0.1:33079
    > Fd: 7
    > 


What should have happened instead?

    > New Connection: 127.0.0.1:33079
    > Fd: 7
    > Error: read tcp 127.0.0.1:33079: i/o timeout


Please provide any additional information below.

I'm trying to understand why this code sample doesn't work. As i understand, File()
creates a copy of the underlying fd. And the new fd has no effect to the original.

One more thing... If i use only the SetReadDeadline function - timeout doesn't work. But
if i use it in couple with the syscall.SetsockoptTimeval function - it works.
@gopherbot
Copy link
Author

Comment 1 by ilarkin@crystalnix.com:

Switching to nonblock mode for new fd can solve this issue:
  > syscall.SetNonblock(int(f.Fd()), true)
Is it necessary to block the original fd during duplicate creation?

@mikioh
Copy link
Contributor

mikioh commented Mar 23, 2014

Comment 2:

See dup and/or fcntl online manual. The duplicated file descritor will point the
original file entry, and some attributes/flags bound to the original file entry will be
shared. In this case, you need to do the following:
1) f2, err := c1.(*net.TCPConn).File()
2) c1.Close()
3) tweak attrs/flags via f2
4) c2, err := net.FileConn(f2)
5) f2.Close()
6) use c2 for network IO

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants