audreyt / network-multicast

[Haskell] The "Network.Multicast" module is for sending UDP datagrams over multicast (class D) addresses.
http://github.com/audreyt/network-multicast
Other
23 stars 12 forks source link

multicastSender can crash leaving unclosable socket #2

Closed joeyh closed 12 years ago

joeyh commented 12 years ago

multicastSender etc create sockets that I cannot find a way to close.

(The remainder of this bug report was wrong. See next message.)

I've tried:

None of these seem to fail (ie, close()) returns 0), but after each my process still has the socket listed in /proc/pid/fd/

I know that sClose does notthing because Network.Socket has a flag that indicates if the socket is open, which is not set on these sockets. I tried setting the flag, and sClose presumably calls c_close then, but as noted that doesn't close them either.

It seems like one way might be to bind() the socket, and then perhaps close would work on it, but I have not managed to get that to work yet.

joeyh commented 12 years ago

Ok, I was mistaken about what was going on. Here's the real bug.

inet_addr will throw an exception if passed an invalid hostname. Here "invalid" includes "ipv6 address". Which I was passing it sometimes.

Unfortunatly, multicastSender first opens a socket, and only then calls inet_addr. So this open socket is left open after the exception is thrown, and there's no possible way to get at it to close it.

sClose works fine on multicast sockets, if they're actually returned.

Suggested fix: Run inet_addr before allocating a socket. So if it crashes, the socket is not opened yet.

Since this line is independant of the ones that come before, simply moving it to the top of the do block is sufficient!

addr <- fmap (SockAddrInet port) (inet_addr host)