3

We have some code to send metrics to a SOCK_DGRAM where another daemon listens and aggregates/proxies these messages. Opening the socket looks like:

sock <- socket
(ai :: AddressInfo Inet Datagram UDP):_ <- getAddressInfo (Just "127.0.0.1") Nothing aiNumericHost
connect s (socketAddress ai) { port }
return sock

And at the moment we write to it like:

send sock payload mempty

I want to ensure the call above doesn't block for very long (or at the very least doesn't block indefinitely), but my understanding of unix sockets is not very deep and I'm having trouble understanding how exactly send blocks, looking at internals here and here.

There is a related question here that was helpful: When a non-blocking send() only transfers partial data, can we assume it would return EWOULDBLOCK the next call?

So my questions specifically are:

  • General socket question: I see in this implementation send is going to block (after busy-waiting) until there is room in the buffer. How exactly is this buffer related to the consumer? Does this mean send may block indefinitely if our listen-ing daemon is slow or stalls?
  • If I would rather abort and never block, do I need to make my own fork of System.Socket.Unsafe, or am I missing something?

I'm only concerned with linux here.

EDIT: Also, and what probably got me started with all of this is I find that when the metrics collector is not running, that every other of my send calls above throws a "connection refused" exception. So why that is, or whether it's normal is another question I have.

EDIT2: Here's a complete example illustrating the connection refused issue if anyone would like to help repro:

import Data.Functor
import System.Socket
import System.Socket.Family.Inet

repro :: IO ()
repro = do
  let port = 6565
  (s :: Socket Inet Datagram UDP) <- socket
  (ai :: AddressInfo Inet Datagram UDP):_ <- getAddressInfo (Just "127.0.0.1") Nothing aiNumericHost
  connect s (socketAddress ai) { port }

  putStrLn "Starting send"
  void $ send s "FOO" mempty
  void $ send s "BAR" mempty
  putStrLn "done"

I'm using socket-0.5.3.0.

EDIT3: this seems to be due to the connect call, somehow. (Testing on sockets latest):

{-# LANGUAGE ScopedTypeVariables, OverloadedStrings, NamedFieldPuns #-}
import Data.Functor
import System.Socket
import System.Socket.Protocol.UDP
import System.Socket.Type.Datagram
import System.Socket.Family.Inet

repro :: IO ()
repro = do
  (s :: Socket Inet Datagram UDP) <- socket

  -- Uncommenting raises eConnectionRefused, everytime:
  -- connect s (SocketAddressInet  inetLoopback  6565     :: SocketAddress Inet)
  putStrLn "Starting send"
  void $ sendTo s "FOO" mempty (SocketAddressInet  inetLoopback  6565     :: SocketAddress Inet)
  void $ sendTo s "BAR" mempty (SocketAddressInet  inetLoopback  6565     :: SocketAddress Inet)
  putStrLn "done"

As I understand it we should be able to use connect (at least the underlying syscall) to set the default send address. I haven't dug into the implementation of connect in the library yet.

I've opened this: https://github.com/lpeterse/haskell-socket/issues/55

6
  • 3
    STREAM and DGRAM sockets work very differently. A DGRAM socket doesn't know nor care if anything is actually listening on the other side. It just fires whatever you pass to it into interstellar space. Dec 18, 2017 at 19:47
  • 1
    It looks like the MSG_DONTWAIT flag to send is likely relevant. The Haskell interface passes the flags through, and presumably defines the constants somewhere. But my understanding of these things is probably shallower than yours.
    – dfeuer
    Dec 18, 2017 at 20:37
  • A DGRAM socket doesn't block a send. It just dumps a packet onto the network, and if the receiver's buffer is full then the packet is discarded. There is no connection, so nothing to block on. Dec 18, 2017 at 21:24
  • Maybe there's something peculiar happening then... when the metric collector (which I'm running with docker; perhaps that's part of the issue) is not running I get "connection refused" exceptions for every other send in my application, when testing in ghci
    – jberryman
    Dec 18, 2017 at 21:50
  • @n.m. do you have any ideas re. edit?
    – jberryman
    Dec 18, 2017 at 23:25

1 Answer 1

2

This isn't a Haskell issue -- it's expected behavior on Linux when sending two UDP packets to a localhost port with no listening process. The following C program:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/udp.h>

int main()
{
    int s = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in dstaddr = { AF_INET, htons(6565), {htonl(0x7f000001)} };
    if (connect(s, (struct sockaddr*) &dstaddr, sizeof(dstaddr)))
        perror("connect");
    if (send(s, "FOO", 3, 0) == -1)
        perror("first send");
    if (send(s, "BAR", 3, 0) == -1)
        perror("second send");
    return 0;
}

will print second send: Connection refused, assuming nothing is listening on localhost port 6565.

If you do any one of the following -- (i) send to a non-local host, (ii) drop the connect call and replace the sends with sendtos, or (iii) send packets to a port with a process listening for UDP packets -- then you won't get the error.

The behavior is a little complex and not well documented anywhere, though the manpages for udp(7) hint at it.

You may find the discussion in this Stack Overflow question helpful.

2
  • Awesome thanks, would you say this is what's happening: the second send call is failing early with the asynchronous error from the first call (and so the third call doesn't raise an error, but the fourth call will, and so on...)? I've come across this guide now, which I'll be consulting for these kinds of questions from now on: masterraghu.com/subjects/np/introduction/…
    – jberryman
    Dec 19, 2017 at 21:31
  • Yes, I believe that's correct: the first call actually sends a UDP packet and returns success. Internally, this generates an ICMP unreachable error which the second call reports instead of sending a UDP packet, so the third call can send a UDP packet and repeat the process. Because this is the loopback interface, it's entirely reliable -- send, error, send, error. On a real network interface, you'd see something similar but less predictably.
    – K. A. Buhr
    Dec 20, 2017 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.