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

UDP errors on Windows #83

Closed
stunndard opened this issue May 31, 2016 · 20 comments
Closed

UDP errors on Windows #83

stunndard opened this issue May 31, 2016 · 20 comments

Comments

@stunndard
Copy link

stunndard commented May 31, 2016

When running on Windows, after 1-2 minute of downloading, I get non stop error messages that keep scrolling infinitely. When I restart torrent, it works ok, but after 1 minute the same happens again.

Screenshot

When I checked the network activity of the torrent process after the error kicks in, I find out that it "loses" all UDP connections and only TCP connections remain active.

Screenshot

This might be an error in UDP socket implementation for Windows in the network libraries you're using. Or something else.

@anacrolix
Copy link
Owner

Thanks for this. I don't do testing on Windows. This would occur if the UDP connection in use had been closed. It may be closed in some circumstances if there's an unexpected error in either DHT or uTP. Are you able to get the first few messages before this avalanche of "use of closed network connection"? The reason should be there. In the meanwhile I'll try to suppress the "use of closed network connection" error message as it's not useful.

@anacrolix
Copy link
Owner

I've done some more poking. It's very likely that there's an unexpected read error from Windows in the uTP package, possibly a temporary error that needs to be handled, that triggers the uTP socket to be destroyed. This socket is shared with DHT, which doesn't expect this to happen. Let me know if you can find that first error message.

@anacrolix
Copy link
Owner

What version of Go are you using?

@stunndard
Copy link
Author

This is the info about my system.

d:\work\src\Go\src\github.com\anacrolix\torrent\cmd>go version
go version go1.6.2 windows/amd64

d:\work\src\Go\src\github.com\anacrolix\torrent\cmd>go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=d:\work\src\Go
set GORACE=
set GOROOT=d:\Devel\go
set GOTOOLDIR=d:\Devel\go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=1
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

I will try to get the first UDP error and will let you know.

@stunndard
Copy link
Author

Is that it?

2016/06/01 11:19:42 client.go:408: read udp 0.0.0.0:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.

@stunndard
Copy link
Author

https://lists.freedesktop.org/archives/nice/2014-October/000968.html

The error seems to be Windows specific and related to the TTL value set for the UDP socket. On my machinge (Windows 7) TTL is 64. I will try to modify the TTL (if Go allows it) for the UDP socket and will let you know how it goes.

@stunndard
Copy link
Author

I did some debugging. It's not related to TTL at all.
I've added some debug prints to utp/socket.go:

Here you're creating the udp listening socket:

func NewSocket(network, addr string) (s *Socket, err error) {
    if network == "" {
        network = "udp"
    }
    pc, err := listenPacket(network, addr)
    if err != nil {
        return
    }

        // aded code
    ptrVal := reflect.ValueOf(pc)
    //fmt.Println(ptrVal)
    val := reflect.Indirect(ptrVal)
    //fmt.Println(val)
    //next line will get you the net.netFD
    fdmember := val.FieldByName("fd")
    //fmt.Println(fdmember)
    val1 := reflect.Indirect(fdmember)
    //fmt.Println(val1)
    netFdPtr := val1.FieldByName("sysfd")
    //fmt.Println(netFdPtr)
    fd := syscall.Handle(netFdPtr.Uint())
    fmt.Println("socket handle: ", fd)

Here you're closing socket (I think the main udp socket should NEVER be closed, as it's used for DHT and UTP?)

func (s *Socket) destroy() {
    delete(sockets, s)
    s.destroyed.Set()
    // TODO: Perhaps we should only Close the PacketConn if we created it
    // ourselves.
        // aded code
    fmt.Println("socket destroyed")
    ptrVal := reflect.ValueOf(s.pc)
    //fmt.Println(ptrVal)
    //log.Printf("ptrVal: %s", ptrVal)
    val := reflect.Indirect(ptrVal)
    //fmt.Println(val)
    //next line will get you the net.netFD
    fdmember := val.FieldByName("fd")
    //fmt.Println(fdmember)
    val1 := reflect.Indirect(fdmember)
    //fmt.Println(val1)
    netFdPtr := val1.FieldByName("sysfd")
    //fmt.Println(netFdPtr)
    fd := syscall.Handle(netFdPtr.Uint())
    fmt.Println("socket handle: ", fd)

This is the output:

d:\work\src\Go\src\github.com\anacrolix\torrent\cmd\torrent>torrent  ******.torrent
creating utp listen socket
socket handle:  192
TTL before:  128 <nil>
<nil>
TTL after:  255 <nil>
created utp listen socket
&{0xc082056178 map[] {<nil> false} map[] {<nil> false} {<nil> false} 0xc082030ae0 {{{0 0 <nil>} {<nil> false} <nil>} {{0 0 <nil>} {<nil> false} <nil>}} <nil>}
2016/06/01 14:50:53 tracker_scraper.go:38: error preparing announce to "http://retracker.local/announce": lookup retracker.local: getaddrinfow: No such host is known.
&{{0xc08202e600}}  socket destroyed
socket handle:  192
2016/06/01 14:51:49 client.go:412: read udp 0.0.0.0:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.

As we can see the socket handle (192) is the same. That socket should never be destroyed in func (s *Socket) destroy().

@stunndard
Copy link
Author

I've added panic to func (s *Socket) destroy() to trace where it's coming from. This is the result:

panic: socket destroyed

goroutine 19 [running]:
panic(0x863740, 0xc0868ed180)
        d:/Devel/go/src/runtime/panic.go:481 +0x3f4
github.com/anacrolix/utp.(*Socket).destroy(0xc0820ff520)
        d:/work/src/Go/src/github.com/anacrolix/utp/socket.go:537 +0x473
github.com/anacrolix/utp.(*Socket).reader(0xc0820ff520)
        d:/work/src/Go/src/github.com/anacrolix/utp/socket.go:176 +0x1a1
created by github.com/anacrolix/utp.NewSocketFromPacketConn
        d:/work/src/Go/src/github.com/anacrolix/utp/socket.go:116 +0x18e

So we can see destroy() is called from func (s *Socket) reader()

func (s *Socket) reader() {
    mu.Lock()
    defer mu.Unlock()
    defer s.destroy()
....

So the socket gets destroyed after each read? Of course, if it's a listening udp socket, and it's closed, all subsequent Accept() calls on that socket will fail. I don't understand why the socket is destroyed after each read here and why it works on Linux. If I comment defer s.destroy() there's no more error shown and I can see UDP connections active for the torrent process.

@anacrolix
Copy link
Owner

The error you've shown is it. The cause appears to be ReadFrom errors that occur only on Windows, that aren't actually fatal. However any error returned from ReadFrom is currently treated as fatal in Socket.reader. Note that you've misinterpreted how Socket.reader works, it doesn't destroy after every read. Socket.destroy is called if Socket.reader returns, as it's assumed something unrecoverable has occurred. You're correct that this also destroyed the UDP connection used by DHT. The PacketConn should not be closed if the connection was not created inside uTP, but this is not the real issue. The real issue is that we must filter out errors from ReadFrom which do not indicate the connection is no longer usable. Since the only reason the socket would be closed is due to the caller that provided the original PacketConn closing it, my interim solution will be to ignore (or log) errors from ReadFrom in Socket.reader, unless Socket.Close has been explicitly called, in which case Socket.reader will return on error. Secondarily, the PacketConn will only be closed if it was created from within the utp packet, and not provided by an external source (as occurs when called from the torrent packet in NewClient). I'll issue a patch shortly. If you can try it out, it will be appreciated. I'm also trying to test on wine, as I have no Windows available to me.

@stunndard
Copy link
Author

Thanks for clarifying, now I get it. I tried to change the return from Socket.reader to continue, and there's no more this error. I will test your patch when it's done as well.

@anacrolix
Copy link
Owner

I've pushed the change to the utp package. It's a quick fix but I believe it'll solve it for now.

@deranjer
Copy link

Just an FYI, I'm getting a very similar error very randomly during my testing when adding torrents via magnet link. Here is the output:

magnet:?xt=urn:btih:9f9165d9a281a9b8e782cd5176bbcc8256fd1871&dn=Ubuntu+16.04.1+LTS+Desktop+64-bit&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969
Ubuntu 16.04.1 LTS Desktop 64-bit
Addingping
panic: read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.

goroutine 21 [running]:
github.com/anacrolix/go-libutp.(*Socket).packetReader(0xc0421191f0)
	C:/Users/deranjer/go/src/github.com/anacrolix/go-libutp/socket.go:92 +0x186
created by github.com/anacrolix/go-libutp.NewSocket
	C:/Users/deranjer/go/src/github.com/anacrolix/go-libutp/socket.go:60 +0x2ca

Process finished with exit code 2

@anacrolix
Copy link
Owner

Do you want to try with the panic commented out, or changed to a log.Print?

@anacrolix anacrolix reopened this Sep 26, 2017
@deranjer
Copy link

`read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.
panic: interface conversion: net.Addr is nil, not *net.UDPAddr

goroutine 6 [running]:
github.com/anacrolix/go-libutp.netAddrToLibSockaddr(0x0, 0x0, 0x1, 0x95)
C:/Users/deranjer/go/src/github.com/anacrolix/go-libutp/sockaddr.go:101 +0xb6
github.com/anacrolix/go-libutp.(*Socket).packetReader(0xc0420f72d0)
C:/Users/deranjer/go/src/github.com/anacrolix/go-libutp/socket.go:95 +0xc7
created by github.com/anacrolix/go-libutp.NewSocket
C:/Users/deranjer/go/src/github.com/anacrolix/go-libutp/socket.go:61 +0x2ca

Process finished with exit code 2`

@anacrolix
Copy link
Owner

Sorry, and add a continue, after the new log line.

@anacrolix
Copy link
Owner

It's awful that this is happening, but I'm not exposed to Windows systems so I can't guess what the correct solution is.

@stunndard
Copy link
Author

I guess you could rely on WSA* Winsock functions at least in the UDP library, instead of using standard Go's runtime ones.

@deranjer
Copy link

With the continue it does keep working, this is the output, the "ping"s are from my websocket code pinging from the frontend to the backend...

read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the op
eration was in progress.
read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the op
eration was in progress.
ping
read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the op
eration was in progress.
read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the op
eration was in progress.
read udp [::]:50007: wsarecvfrom: The connection has been broken due to keep-alive activity detecting a failure while the op
eration was in progress.
ping
ping

akuma06 added a commit to NyaaPantsu/nyaa that referenced this issue Oct 19, 2017
Seems like this error is a known bug from  anacrolyx torrent anacrolix/torrent#83

To prevent it, I'm creating a single client and modifying the socket.go to make it not raise a panic but a simple error log.
akuma06 added a commit to NyaaPantsu/nyaa that referenced this issue Oct 21, 2017
* [WIP] Torrent Generation on not found error
As asked in #1517, it allows on-the-fly torrent generation. Since it uses magnet links, it needs some time to connect to peers. So it can't be instant generation, we need the user to wait and try after a minute at least.

* Replace Fatal by simple error

* attempt at fixing travis

* del

* Add Anacrolyx dependency

* Add back difflib

* Remove .torrent suffix in the url example

* Add some explanations when file missing page shown

* Ignore downloads directory

* Either use cache (third-party site) or own download directory

* Wrong import

* If there is an error then it means we aren't generating a torrent file

May it be "torrent not found" or "We do not store torrent files" which are the two only existing errors for this page

* hash is never empty

* TorrentLink may be empty at times

So we add a /download/:hash link if it is

* Update README.md

* Made a mistake here, need to check if false

* Update en-us.all.json

* Update CHANGELOG.md

* Torrent file generation can be triggered by click on button if JS enabled

* Update download.go

* Update download.go

* Use c.JSON instead of text/template

* Return to default behavior if we don't generate the file

* Don't do the query if returned to default behavior

* Add "Could not generate torrent file" error

* Fix JS condition & lower delay until button updates

* Start download automatically once torrent file is generated

* Fix torrentFileExists() constantly returning false if external torrent download URL

* torrent-view-data is two tables instead of one

This allows the removal of useless things without any problem (e.g Website link), but also a better responsibe design since the previous one separated stats after a certain res looking very wonky

* CSS changes to go along

* Remove useless <b></b>

* Update main.css

* In torrentFileExists, check if filestorage path exists instead of looking at the domain in torrent link

When checking if the file is stored on another server i used to simply check if the domain name was inside the torrent link, but we can straight up check for filestorage length

* Fix JS of on-demand stat fetching

* ScrapeAge variable accessible through view.jet.html

Contains last scraped time in hours, is at -1 is torrent has never been scraped
Stats will get updated if it's either at -1 or above 1460 (2 months old)

* Refresh stats if older than two months OR unknown and older than 24h

Show last scraped date even if stats are unknown

* Add StatsObsolete variable to torrent

Indicating if:
- They can be shown
- They need to be updated

* Update scraped data even if Unknown, prevent users from trying to fetch stats every seconds

* Torrent file stored locally by default

* no need to do all of that if no filestorage

* fix filestorage path

* Fix torrent download button stuck on "Generating torrent file" at rare times

* fix some css rules that didn't work on IE

* Fix panic error

Seems like this error is a known bug from  anacrolyx torrent anacrolix/torrent#83

To prevent it, I'm creating a single client and modifying the socket.go to make it not raise a panic but a simple error log.
Kiloutre pushed a commit to NyaaPantsu/nyaa that referenced this issue Oct 21, 2017
* [WIP] Torrent Generation on not found error
As asked in #1517, it allows on-the-fly torrent generation. Since it uses magnet links, it needs some time to connect to peers. So it can't be instant generation, we need the user to wait and try after a minute at least.

* Replace Fatal by simple error

* attempt at fixing travis

* del

* Add Anacrolyx dependency

* Add back difflib

* Remove .torrent suffix in the url example

* Add some explanations when file missing page shown

* Ignore downloads directory

* Either use cache (third-party site) or own download directory

* Wrong import

* If there is an error then it means we aren't generating a torrent file

May it be "torrent not found" or "We do not store torrent files" which are the two only existing errors for this page

* hash is never empty

* TorrentLink may be empty at times

So we add a /download/:hash link if it is

* Update README.md

* Made a mistake here, need to check if false

* Update en-us.all.json

* Update CHANGELOG.md

* Torrent file generation can be triggered by click on button if JS enabled

* Update download.go

* Update download.go

* Use c.JSON instead of text/template

* Return to default behavior if we don't generate the file

* Don't do the query if returned to default behavior

* Add "Could not generate torrent file" error

* Fix JS condition & lower delay until button updates

* Start download automatically once torrent file is generated

* Fix torrentFileExists() constantly returning false if external torrent download URL

* torrent-view-data is two tables instead of one

This allows the removal of useless things without any problem (e.g Website link), but also a better responsibe design since the previous one separated stats after a certain res looking very wonky

* CSS changes to go along

* Remove useless <b></b>

* Update main.css

* In torrentFileExists, check if filestorage path exists instead of looking at the domain in torrent link

When checking if the file is stored on another server i used to simply check if the domain name was inside the torrent link, but we can straight up check for filestorage length

* Fix JS of on-demand stat fetching

* ScrapeAge variable accessible through view.jet.html

Contains last scraped time in hours, is at -1 is torrent has never been scraped
Stats will get updated if it's either at -1 or above 1460 (2 months old)

* Refresh stats if older than two months OR unknown and older than 24h

Show last scraped date even if stats are unknown

* Add StatsObsolete variable to torrent

Indicating if:
- They can be shown
- They need to be updated

* Update scraped data even if Unknown, prevent users from trying to fetch stats every seconds

* Torrent file stored locally by default

* no need to do all of that if no filestorage

* fix filestorage path

* Fix torrent download button stuck on "Generating torrent file" at rare times

* fix some css rules that didn't work on IE

* Fix panic error

Seems like this error is a known bug from  anacrolyx torrent anacrolix/torrent#83

To prevent it, I'm creating a single client and modifying the socket.go to make it not raise a panic but a simple error log.

* Removing some logs
anacrolix added a commit to anacrolix/go-libutp that referenced this issue Oct 22, 2017
@MrGlp
Copy link

MrGlp commented Aug 27, 2023

ths

@anacrolix
Copy link
Owner

que?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants