Skip to content

Commit 376152c

Browse files
committed
Attempt to work around Unix.socketpair issue on Win32
`Unix.socketpair` occasionally fails on Windows: ```diff --- a/_build/default/lib/picos_io/picos_io.mli +++ b/_build/default/lib/picos_io/.mdx/picos_io.mli.corrected @@ -753,7 +753,5 @@ end send_string "Hello, world!"; send_string "POSIX with OCaml"; end - Hello, world! - POSIX with OCaml - - : unit = () + Exception: Unix.Unix_error(Unix.EADDRINUSE, "socketpair", "") ]} *) ``` This introduces a work around to retry `Unix.sockepair` a few times on Win32 when the `EADDRINUSE` error is raised. We shuld remove the workaround once the root issue is fixed.
1 parent 2e0567a commit 376152c

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/picos_io/picos_io.ml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,29 @@ module Unix = struct
431431
Fd.create (Unix.socket ?cloexec socket_domain socket_type protocol)
432432

433433
(* https://pubs.opengroup.org/onlinepubs/9699919799/functions/socketpair.html *)
434-
let socketpair ?cloexec socket_domain socket_type mystery =
435-
let fst, snd = Unix.socketpair ?cloexec socket_domain socket_type mystery in
436-
(Fd.create fst, Fd.create snd)
434+
let socketpair =
435+
if Sys.win32 then
436+
(* This is a workaround for [Unix.socketpair] on Win32 to avoid CI
437+
failures. We should be able to remove this once the root issue is
438+
fixed. *)
439+
let rec socketpair_win32 ?cloexec socket_domain socket_type mystery
440+
retries =
441+
match Unix.socketpair ?cloexec socket_domain socket_type mystery with
442+
| sockets -> sockets
443+
| exception Unix.Unix_error (EADDRINUSE, _, _) when 0 < retries ->
444+
socketpair_win32 ?cloexec socket_domain socket_type mystery
445+
(retries - 1)
446+
in
447+
fun ?cloexec socket_domain socket_type mystery ->
448+
let fst, snd =
449+
socketpair_win32 ?cloexec socket_domain socket_type mystery 5
450+
in
451+
(Fd.create fst, Fd.create snd)
452+
else fun ?cloexec socket_domain socket_type mystery ->
453+
let fst, snd =
454+
Unix.socketpair ?cloexec socket_domain socket_type mystery
455+
in
456+
(Fd.create fst, Fd.create snd)
437457

438458
(* https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html *)
439459
let accept ?cloexec fd =

0 commit comments

Comments
 (0)