git/compat
Johannes Schindelin 19ed0dff8f win32: special-case ENOSPC when writing to a pipe
Since c6d3cce6f3 (pipe_command(): handle ENOSPC when writing to a
pipe, 2022-08-17), one `write()` call that results in an `errno` value
`ENOSPC` (which typically indicates out of disk space, which makes
little sense in the context of a pipe) is treated the same as `EAGAIN`.

However, contrary to expectations, as diagnosed in
https://github.com/python/cpython/issues/101881#issuecomment-1428667015,
when writing to a non-blocking pipe on Windows, an `errno` value of
`ENOSPC` means something else: the write _fails_. Completely. Because
more data was provided than the internal pipe buffer can handle.
Somewhat surprising, considering that `write()` is allowed to write less
than the specified amount, e.g. by writing only as much as fits in that
buffer. But it doesn't, it writes no byte at all in that instance.

Let's handle this by manually detecting when an `ENOSPC` indicates that
a pipe's buffer is smaller than what needs to be written, and re-try
using the pipe's buffer size as `size` parameter.

It would be plausible to try writing the entire buffer in a loop,
feeding pipe buffer-sized chunks, but experiments show that trying to
write more than one buffer-sized chunk right after that will immediately
fail because the buffer is unlikely to be drained as fast as `write()`
could write again. And the whole point of a non-blocking pipe is to be
non-blocking.

Which means that the logic that determines the pipe's buffer size
unfortunately has to be run potentially many times when writing large
amounts of data to a non-blocking pipe, as there is no elegant way to
cache that information between `write()` calls. It's the best we can do,
though, so it has to be good enough.

This fix is required to let t3701.60 (handle very large filtered diff)
pass with the MSYS2 runtime provided by the MSYS2 project: Without this
patch, the failed write would result in an infinite loop. This patch is
not required with Git for Windows' variant of the MSYS2 runtime only
because Git for Windows added an ugly work-around specifically to avoid
a hang in that test case.

The diff is slightly chatty because it extends an already-existing
conditional that special-cases a _different_ `errno` value for pipes,
and because this patch needs to account for the fact that
`_get_osfhandle()` potentially overwrites `errno`.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-30 13:59:16 -08:00
..
fsmonitor fsmonitor/darwin: mark unused parameters in system callback 2023-09-18 15:56:15 -07:00
linux treewide: remove unnecessary cache.h inclusion from several sources 2023-03-21 10:56:51 -07:00
nedmalloc nedmalloc: avoid new compile error 2022-05-24 15:58:31 -07:00
poll mingw: workaround for hangs when sending STDIN 2020-02-27 14:23:29 -08:00
regex compat/regex: move stdlib.h up in inclusion chain 2020-04-27 11:21:16 -07:00
simple-ipc cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
stub tr2: make process info collection platform-generic 2021-07-22 13:35:20 -07:00
vcbuild Makefile: stop hardcoding {command,config}-list.h 2021-09-23 15:06:47 -07:00
win32 Merge branch 'ds/maintenance-on-windows-fix' 2023-08-15 10:19:47 -07:00
.gitattributes Provide zlib's uncompress2 from compat/zlib-compat.c 2021-10-08 10:45:48 -07:00
access.c git-compat-util: work around for access(X_OK) under root 2019-04-25 17:49:44 +09:00
apple-common-crypto.h
basename.c
bswap.h compat/bswap.h: don't assume MSVC is little-endian 2020-11-11 11:24:47 -08:00
compiler.h bugreport: add compiler info 2020-04-16 15:23:42 -07:00
disk.h abspath.h: move absolute path functions from cache.h 2023-03-21 10:56:52 -07:00
fileno.c git-compat-util: work around for access(X_OK) under root 2019-04-25 17:49:44 +09:00
fopen.c git_fopen: fix a sparse 'not declared' warning 2017-05-26 12:33:55 +09:00
hstrerror.c
inet_ntop.c
inet_pton.c
memmem.c
mingw.c win32: special-case ENOSPC when writing to a pipe 2024-01-30 13:59:16 -08:00
mingw.h Merge branch 'ma/locate-in-path-for-windows' 2023-08-09 16:18:16 -07:00
mkdir.c tree-wide: apply equals-null.cocci 2022-05-02 09:50:37 -07:00
mkdtemp.c
mmap.c tree-wide: apply equals-null.cocci 2022-05-02 09:50:37 -07:00
msvc.c
msvc.h msvc: add pragmas for common warnings 2019-06-25 10:46:57 -07:00
nonblock.c mark unused parameters in trivial compat functions 2022-10-17 21:24:03 -07:00
nonblock.h compat: add function to enable nonblocking pipes 2022-08-17 09:21:40 -07:00
obstack.c compat/obstack: fix -Wcast-function-type warnings 2019-01-17 11:13:38 -08:00
obstack.h obstack: avoid computing offsets from NULL pointer 2020-01-28 23:13:25 -08:00
open.c Makefile: add OPEN_RETURNS_EINTR knob 2021-02-26 14:15:51 -08:00
pread.c treewide: remove double forward declaration of read_in_full 2023-04-11 08:52:11 -07:00
precompose_utf8.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
precompose_utf8.h precompose_utf8: make precompose_string_if_needed() public 2021-04-05 17:30:04 -07:00
qsort_s.c compat/qsort_s.c: avoid using potentially unaligned access 2022-01-07 14:20:58 -08:00
regcomp_enhanced.c use enhanced basic regular expressions on macOS 2023-01-08 10:06:34 +09:00
setenv.c
sha1-chunked.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
sha1-chunked.h
snprintf.c
stat.c
strcasestr.c
strdup.c compat: move strdup(3) replacement to its own file 2016-09-07 10:41:45 -07:00
strlcpy.c
strtoimax.c
strtoumax.c
terminal.c hashmap: use expected signatures for comparison functions 2023-08-19 21:17:53 -07:00
terminal.h terminal: don't assume stdin is /dev/tty 2022-03-16 12:24:43 -07:00
unsetenv.c unsetenv(3) returns int, not void 2021-10-29 15:00:58 -07:00
win32.h
win32mmap.c
winansi.c compat/winansi: check for errors of CreateThread() correctly 2023-02-01 14:36:19 -08:00
zlib-uncompress2.c compat: auto-detect if zlib has uncompress2() 2022-01-26 09:05:55 -08:00