Skip to content

Commit bf3c248

Browse files
committed
* fix blocking issue on Windows : issue #793.
Adding await_readable before reading fd * fix broken pipe exception : issue #792. Use Unix.read_bigarray instead of Unix_cstruct.read * replace eio_windows_cstruct_stubs.c by Unix functions. Since Ocaml 5.2, Unix.read_bigarray and Unix.write_bigarray can be used.
1 parent c78db1a commit bf3c248

File tree

4 files changed

+17
-154
lines changed

4 files changed

+17
-154
lines changed

lib_eio_windows/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(foreign_stubs
77
(language c)
88
(include_dirs ../lib_eio/unix/include)
9-
(names eio_windows_stubs eio_windows_cstruct_stubs))
9+
(names eio_windows_stubs ))
1010
(c_library_flags :standard -lbcrypt -lntdll)
1111
(libraries eio eio.unix eio.utils fmt))
1212

lib_eio_windows/eio_windows_cstruct_stubs.c

Lines changed: 0 additions & 149 deletions
This file was deleted.

lib_eio_windows/low_level.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,25 @@ let rec do_nonblocking ty fn fd =
3939
do_nonblocking ty fn fd
4040

4141
let read fd buf start len =
42+
await_readable fd;
4243
Fd.use_exn "read" fd @@ fun fd ->
4344
do_nonblocking Read (fun fd -> Unix.read fd buf start len) fd
4445

45-
let read_cstruct fd buf =
46+
let read_cstruct fd (buf:Cstruct.t) =
47+
await_readable fd;
4648
Fd.use_exn "read_cstruct" fd @@ fun fd ->
47-
do_nonblocking Read (fun fd -> Unix_cstruct.read fd buf) fd
49+
do_nonblocking Read (fun fd -> Unix.read_bigarray fd buf.buffer buf.off buf.len) fd
4850

4951
let write fd buf start len =
52+
await_writable fd;
5053
Fd.use_exn "write" fd @@ fun fd ->
5154
do_nonblocking Write (fun fd -> Unix.write fd buf start len) fd
5255

56+
let write_cstruct fd (buf:Cstruct.t) =
57+
await_writable fd;
58+
Fd.use_exn "write_cstruct" fd @@ fun fd ->
59+
do_nonblocking Write (fun fd -> Unix.write_bigarray fd buf.buffer buf.off buf.len) fd
60+
5361
let sleep_until time =
5462
Sched.enter @@ fun t k ->
5563
Sched.await_timeout t k time
@@ -148,8 +156,11 @@ let readv fd bufs =
148156
do_nonblocking Read (fun fd -> eio_readv fd bufs) fd
149157

150158
let writev fd bufs =
151-
Fd.use_exn "writev" fd @@ fun fd ->
152-
do_nonblocking Write (fun fd -> Unix_cstruct.writev fd bufs) fd
159+
let rec loop buf = if Cstruct.length buf > 0 then begin
160+
let n = write_cstruct fd buf in
161+
loop @@ Cstruct.shift buf n
162+
end in
163+
List.iter loop bufs
153164

154165
let preadv ~file_offset fd bufs =
155166
Fd.use_exn "preadv" fd @@ fun fd ->

lib_eio_windows/low_level.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ val sleep_until : Mtime.t -> unit
2222
val read : fd -> bytes -> int -> int -> int
2323
val read_cstruct : fd -> Cstruct.t -> int
2424
val write : fd -> bytes -> int -> int -> int
25+
val write_cstruct : fd -> Cstruct.t -> int
2526

2627
val socket : sw:Switch.t -> Unix.socket_domain -> Unix.socket_type -> int -> fd
2728
val connect : fd -> Unix.sockaddr -> unit

0 commit comments

Comments
 (0)