Skip to content

Commit

Permalink
Treat ERROR_BROKEN_PIPE on read as zero-read instead of error
Browse files Browse the repository at this point in the history
This matches the behaviour of win32unix since 3.11.1, see OCaml PR
4790.
  • Loading branch information
MisterDA committed Oct 18, 2021
1 parent 8b0590c commit 47e04b8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
the behavior of Lwt_io.with_temp_dir following symlinks to
directories on Win32. (#883, Antonin Décimo)

* On Windows, treat ERROR_BROKEN_PIPE on read as zero-read instead
of error. See OCaml PR #4790. (#898, Antonin Décimo)

====== Additions ======

* Lwt_bytes.blit_from_string: string complement of Lwt_bytes.blit (#882, Hugo Heuzard).
Expand Down
7 changes: 6 additions & 1 deletion src/unix/windows_c/windows_bytes_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ CAMLprim value lwt_unix_bytes_read(value fd, value buf, value vofs, value vlen)
numbytes, &numwritten, NULL))
err = GetLastError();
}
if (err) {
if (err == ERROR_BROKEN_PIPE) {
/* The write handle for an anonymous pipe has been closed. We match
the Unix behavior, and treat this as a zero-read instead of a
Unix_error. See OCaml PR #4790. */
numwritten = 0;
} else if (err) {
win32_maperr(err);
uerror("write", Nothing);
}
Expand Down
7 changes: 6 additions & 1 deletion src/unix/windows_c/windows_bytes_read_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ static value result_bytes_read(struct job_bytes_read *job)
value result;
DWORD error = job->error_code;
caml_remove_generational_global_root(&job->ocaml_buffer);
if (error) {
if (error == ERROR_BROKEN_PIPE) {
/* The write handle for an anonymous pipe has been closed. We match the
Unix behavior, and treat this as a zero-read instead of a Unix_error.
See OCaml PR #4790. */
job->result = 0;
} else if (error) {
lwt_unix_free_job(&job->job);
win32_maperr(error);
uerror("bytes_read", Nothing);
Expand Down
7 changes: 6 additions & 1 deletion src/unix/windows_c/windows_pread.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ CAMLprim value lwt_unix_pread(value fd, value buf, value vfile_offset,
&overlapped))
err = GetLastError();
}
if (err) {
if (err == ERROR_BROKEN_PIPE) {
/* The write handle for an anonymous pipe has been closed. We match
the Unix behavior, and treat this as a zero-read instead of a
Unix_error. See OCaml PR #4790. */
numwritten = 0;
} else if (err) {
win32_maperr(err);
uerror("pread", Nothing);
}
Expand Down
7 changes: 6 additions & 1 deletion src/unix/windows_c/windows_pread_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ static value result_pread(struct job_pread *job)
{
value result;
DWORD error = job->error_code;
if (error) {
if (error == ERROR_BROKEN_PIPE) {
/* The write handle for an anonymous pipe has been closed. We match the
Unix behavior, and treat this as a zero-read instead of a Unix_error.
See OCaml PR #4790. */
job->result = 0;
} else if (error) {
caml_remove_generational_global_root(&job->string);
lwt_unix_free_job(&job->job);
win32_maperr(error);
Expand Down
7 changes: 6 additions & 1 deletion src/unix/windows_c/windows_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ CAMLprim value lwt_unix_read(value fd, value buf, value vofs, value vlen)
if (!ReadFile(h, &Byte(buf, ofs), numbytes, &numwritten, NULL))
err = GetLastError();
}
if (err) {
if (err == ERROR_BROKEN_PIPE) {
/* The write handle for an anonymous pipe has been closed. We match
the Unix behavior, and treat this as a zero-read instead of a
Unix_error. See OCaml PR #4790. */
numwritten = 0;
} else if (err) {
win32_maperr(err);
uerror("read", Nothing);
}
Expand Down
7 changes: 6 additions & 1 deletion src/unix/windows_c/windows_read_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ static value result_read(struct job_read *job)
{
value result;
DWORD error = job->error_code;
if (error) {
if (error == ERROR_BROKEN_PIPE) {
/* The write handle for an anonymous pipe has been closed. We match the
Unix behavior, and treat this as a zero-read instead of a Unix_error.
See OCaml PR #4790. */
job->result = 0;
} else if (error) {
caml_remove_generational_global_root(&job->string);
lwt_unix_free_job(&job->job);
win32_maperr(error);
Expand Down

0 comments on commit 47e04b8

Please sign in to comment.