ocaml-multicore / eio

Effects-based direct-style IO for multicore OCaml
Other
528 stars 67 forks source link

Explanation of "connect-in-progress" #721

Open Wenke-D opened 3 months ago

Wenke-D commented 3 months ago

Hello, when I try to connect a address where there is no tcp server listening, I will get an error (obviously), but the error message is very confusing

Fatal error: exception Eio.Io Net Connection_failure Refused Unix_error (Connection refused, "connect-in-progress", "")

What does the connect-in-progress mean ?

I trace this string down to the eio/lib_eio_posix/low_level.ml

let connect fd addr =
  try
    Fd.use_exn "connect" fd (fun fd -> Unix.connect fd addr)
  with
  | Unix.Unix_error ((EINTR | EAGAIN | EWOULDBLOCK | EINPROGRESS), _, _) ->
    await_writable "connect" fd;
    match Fd.use_exn "connect" fd Unix.getsockopt_error with
    | None -> ()
    | Some code -> raise (Err.wrap code "connect-in-progress" "")

but there is no explanation. Could you please explain a little ?

talex5 commented 3 months ago

It just means that connect failed.

Connecting is a 3 step process:

  1. Call connect to start connecting (getting EINPROGRESS).
  2. Wait until it's ready.
  3. Call connect again to get the result.

The -in-progress is just a hint that it was the second call that returned the error.

Maybe "connect (collecting result)" or something would be clearer.

Wenke-D commented 2 months ago

Thanks for the reply, so what does it mean for application ? In this case, how should I handle this error, if I still want to make the connection ?

talex5 commented 2 months ago

You can catch connection errors like this:

try ... with Eio.Io (Eio.Net.E Connection_failure _, _) -> ...

(see https://github.com/ocaml-multicore/eio?tab=readme-ov-file#error-handling)