goelixiripcgen-tcp

Why I keep getting timeout with gen_tcp in Elixir?


I am trying to implement a simple Server (in Go) / Client (in Elixir).

However, I keep getting timeouts on Elixir side before my request reaches the server.

Server

package main

import (
    "fmt"
    "net"
    "net/http"
    "os"
)

func main() {
    socket := "/tmp/ipc_test.sock"
    os.Remove(socket)

    listener, err := net.Listen("unix", socket)
    if err != nil {
        fmt.Printf("Error listening on unix socket: %s\n", err)
        return
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        param := r.URL.Query().Get("param")
        fmt.Printf("%s", param)
        fmt.Fprint(w, param)
    })

    fmt.Printf("Server listening on %s\n", socket)
    http.Serve(listener, nil)
}

Client

defmodule IpcTest do
  def make_request(socket, request) do
    :gen_tcp.send(socket, request)
    {:ok, response} = :gen_tcp.recv(socket, 0)

    :gen_tcp.close(socket)
    response
  end

  def print_responses(responses) do
    responses
    |> Enum.each(&IO.puts/1)
  end

  def run(num_requests) do
    1..num_requests
    |> Enum.map(fn i ->
      request = "/?param=#{i}"

      Task.async(fn ->
        {:ok, socket} =
          :gen_tcp.connect({:local, "/tmp/ipc_test.sock"}, 0, [:binary, packet: 4, active: false])

        make_request(socket, request)
      end)
    end)
    |> Enum.map(&Task.await/1)
  end
end
> go run src/main.go
Server listening on /tmp/js_runner.sock

As you can see, the server seems to be running fine. However, I am getting the following error in my client.

> iex -S mix
> IpcTest.run 1
** (exit) exited in: Task.await(%Task{mfa: {:erlang, :apply, 2}, owner: #PID<0.237.0>, pid: #PID<0.263.0>, ref: #Reference<0.3740412160.2895708166.190144>}, 5000)
    ** (EXIT) time out
    (elixir 1.14.2) lib/task.ex:830: Task.await/2
    (elixir 1.14.2) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
    iex:2: (file)

Solution

  • By default, the :gen_tcp.connect/3 function has a timeout value of 5000 milliseconds (5 seconds). Increase to 10:

    {:ok, socket} = :gen_tcp.connect({:local, "/tmp/ipc_test.sock"}, 10000, [:
    

    But you should also try stuff like

    socat - UNIX-CONNECT:/tmp/ipc_test.sock
    

    Or the other stuff in: https://unix.stackexchange.com/questions/26715/how-can-i-communicate-with-a-unix-domain-socket-via-the-shell-on-debian-squeeze

    to see if the socket file is even working for ANY normal linux cli tool.