     {:ok, handler_pid} = ServerSpy.start_link
 
     on_exit fn ->
-      [handler_pid, client, server]
-      |> Enum.each(fn pid ->
-        ref = Process.monitor(pid)
-        Process.exit(pid, :normal)
-        receive do
-          {:DOWN, ^ref, _, _, _} ->
-            :ok
-        end
-      end)
+      wait_for_exit(handler_pid)
+      wait_for_exit(client)
+      wait_for_exit(server)
     end
 
     {:ok, port: port, client: client, server: server}

  alias Thrift.Generator.ServiceTest.SimpleService.Binary.Framed.Client
  alias Thrift.Generator.ServiceTest.UsernameTakenException

  def start_server(port, recv_timeout \\ :infinity) do
    :thrift_socket_server.start(
      handler: ServerSpy,
      port: port,

    end
  end

  thrift_test "clients retry when making an RPC on a closed server when retry is true" do
    new_server_port = random_port()
    {:ok, server} = start_server(new_server_port, 20)
    {:ok, client} = Client.start_link("127.0.0.1", new_server_port, [tcp_opts: [retry: true]])
