Skip to content

Commit e2cb36c

Browse files
committed
mingw: workaround for hangs when sending STDIN
Explanation ----------- The problem here is flawed `poll()` implementation. When it tries to see if pipe can be written without blocking, it eventually calls `NtQueryInformationFile()` and tests `WriteQuotaAvailable`. However, the meaning of quota was misunderstood. The value of quota is reduced when either some data was written to a pipe, *or* there is a pending read on the pipe. Therefore, if there is a pending read of size >= then the pipe's buffer size, poll() will think that pipe is not writable and will hang forever, usually that means deadlocking both pipe users. I have studied the problem and found that Windows pipes track two values: `QuotaUsed` and `BytesInQueue`. The code in `poll()` apparently wants to know `BytesInQueue` instead of quota. Unfortunately, `BytesInQueue` can only be requested from read end of the pipe, while `poll()` receives write end. The git's implementation of `poll()` was copied from gnulib, which also contains a flawed implementation up to today. I also had a look at implementation in cygwin, which is also broken in a subtle way. It uses this code in `pipe_data_available()`: fpli.WriteQuotaAvailable = (fpli.OutboundQuota - fpli.ReadDataAvailable) However, `ReadDataAvailable` always returns 0 for the write end of the pipe, turning the code into an obfuscated version of returning pipe's total buffer size, which I guess will in turn have `poll()` always say that pipe is writable. The commit that introduced the code doesn't say anything about this change, so it could be some debugging code that slipped in. These are the typical sizes used in git: 0x2000 - default read size in `strbuf_read()` 0x1000 - default read size in CRT, used by `strbuf_getwholeline()` 0x2000 - pipe buffer size in compat\mingw.c As a consequence, as soon as child process uses `strbuf_read()`, `poll()` in parent process will hang forever, deadlocking both processes. This results in two observable behaviors: 1) If parent process begins sending STDIN quickly (and usually that's the case), then first `poll()` will succeed and first block will go through. MAX_IO_SIZE_DEFAULT is 8MB, so if STDIN exceeds 8MB, then it will deadlock. 2) If parent process waits a little bit for any reason (including OS scheduler) and child is first to issue `strbuf_read()`, then it will deadlock immediately even on small STDINs. Possible solutions ------------------ 1) Somehow obtain `BytesInQueue` instead of `QuotaUsed` I did a pretty thorough search and didn't find any ways to obtain the value from write end of the pipe. 2) Also give read end of the pipe to `poll()` That can be done, but it will probably invite some dirty code, because `poll()` * can accept multiple pipes at once * can accept things that are not pipes * is expected to have a well known signature. 3) Make `poll()` always reply "writable" for write end of the pipe Afterall it seems that cygwin (accidentally?) does that for years. Also, it should be noted that `pump_io_round()` writes 8MB blocks, completely ignoring the fact that pipe's buffer size is only 8KB, which means that pipe gets clogged many times during that single write. This may invite a deadlock, if child's STDERR/STDOUT gets clogged while it's trying to deal with 8MB of STDIN. Such deadlocks could be defeated with writing less then pipe's buffer size per round, and always reading everything from STDOUT/STDERR before starting next round. Therefore, making `poll()` always reply "writable" shouldn't cause any new issues or block any future solutions. 4) Increase the size of the pipe's buffer The difference between `BytesInQueue` and `QuotaUsed` is the size of pending reads. Therefore, if buffer is bigger then size of reads, `poll()` won't hang so easily. However, I found that for example `strbuf_read()` will get more and more hungry as it reads large inputs, eventually surpassing any reasonable pipe buffer size. Chosen solution --------------- Make `poll()` always reply "writable" for write end of the pipe. Hopefully one day someone will find a way to implement it properly. Signed-off-by: Alexandr Miloslavskiy <[email protected]>
1 parent d8437c5 commit e2cb36c

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

compat/poll/poll.c

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,10 @@ win32_compute_revents (HANDLE h, int *p_sought)
139139
INPUT_RECORD *irbuffer;
140140
DWORD avail, nbuffer;
141141
BOOL bRet;
142-
IO_STATUS_BLOCK iosb;
143-
FILE_PIPE_LOCAL_INFORMATION fpli;
144-
static PNtQueryInformationFile NtQueryInformationFile;
145-
static BOOL once_only;
146142

147143
switch (GetFileType (h))
148144
{
149145
case FILE_TYPE_PIPE:
150-
if (!once_only)
151-
{
152-
NtQueryInformationFile = (PNtQueryInformationFile)(void (*)(void))
153-
GetProcAddress (GetModuleHandleW (L"ntdll.dll"),
154-
"NtQueryInformationFile");
155-
once_only = TRUE;
156-
}
157-
158146
happened = 0;
159147
if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0)
160148
{
@@ -166,22 +154,9 @@ win32_compute_revents (HANDLE h, int *p_sought)
166154

167155
else
168156
{
169-
/* It was the write-end of the pipe. Check if it is writable.
170-
If NtQueryInformationFile fails, optimistically assume the pipe is
171-
writable. This could happen on Win9x, where NtQueryInformationFile
172-
is not available, or if we inherit a pipe that doesn't permit
173-
FILE_READ_ATTRIBUTES access on the write end (I think this should
174-
not happen since WinXP SP2; WINE seems fine too). Otherwise,
175-
ensure that enough space is available for atomic writes. */
176-
memset (&iosb, 0, sizeof (iosb));
177-
memset (&fpli, 0, sizeof (fpli));
178-
179-
if (!NtQueryInformationFile
180-
|| NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
181-
FilePipeLocalInformation)
182-
|| fpli.WriteQuotaAvailable >= PIPE_BUF
183-
|| (fpli.OutboundQuota < PIPE_BUF &&
184-
fpli.WriteQuotaAvailable == fpli.OutboundQuota))
157+
/* It was the write-end of the pipe. Unfortunately there is no
158+
reliable way of knowing if it can be written without blocking.
159+
Just say that it's all good. */
185160
happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND);
186161
}
187162
return happened;

t/t3903-stash.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,4 +1285,9 @@ test_expect_success 'stash handles skip-worktree entries nicely' '
12851285
git rev-parse --verify refs/stash:A.t
12861286
'
12871287

1288+
test_expect_success 'stash handles large files' '
1289+
printf "%1023s\n%.0s" "x" {1..16384} >large_file.txt &&
1290+
git stash push --include-untracked -- large_file.txt
1291+
'
1292+
12881293
test_done

0 commit comments

Comments
 (0)