Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0f527f7

Browse files
committed
Fix treatment of *lpNumberOfBytesRecvd == 0: that's a completion condition.
pgwin32_recv() has treated a non-error return of zero bytes from WSARecv()as being a reason to block ever since the current implementation wasintroduced in commita4c40f1. However, so far as one can tellfrom Microsoft's documentation, that is just wrong: what it means isgraceful connection closure (in stream protocols) or receipt of azero-length message (in message protocols), and neither case should resultin blocking here. The only reason the code worked at all was that controlthen fell into the retry loop, which did *not* treat zero bytes specially,so we'd get out after only wasting some cycles. But as of 9.5 we do notnormally reach the retry loop and so the bug is exposed, as reported byShay Rojansky and diagnosed by Andres Freund.Remove the unnecessary test on the byte count, and rearrange the codein the retry loop so that it looks identical to the initial sequence.Back-patch of commit90e61df. Theoriginal plan was to apply this only to 9.5 and up, but after discussionand buildfarm testing, it seems better to back-patch. The noblock codepath has been at risk of this problem since it was introduced (in 9.0);if it did happen in pre-9.5 branches, the symptom would be that a walsenderwould wait indefinitely rather than noticing a loss of connection. Whilewe lack proof that the case has been seen in the field, it seems possiblethat it's happened without being reported.
1 parent6a0d63d commit0f527f7

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

‎src/backend/port/win32/socket.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,10 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
323323
wbuf.buf=buf;
324324

325325
r=WSARecv(s,&wbuf,1,&b,&flags,NULL,NULL);
326-
if (r!=SOCKET_ERROR&&b>0)
327-
/* Read succeeded right away */
328-
returnb;
326+
if (r!=SOCKET_ERROR)
327+
returnb;/* success */
329328

330-
if (r==SOCKET_ERROR&&
331-
WSAGetLastError()!=WSAEWOULDBLOCK)
329+
if (WSAGetLastError()!=WSAEWOULDBLOCK)
332330
{
333331
TranslateSocketError();
334332
return-1;
@@ -344,7 +342,7 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
344342
return-1;
345343
}
346344

347-
/*No error, zero bytes (win2000+) or error+WSAEWOULDBLOCK (<=nt4) */
345+
/*We're in blocking mode, so wait for data */
348346

349347
for (n=0;n<5;n++)
350348
{
@@ -353,25 +351,22 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
353351
return-1;/* errno already set */
354352

355353
r=WSARecv(s,&wbuf,1,&b,&flags,NULL,NULL);
356-
if (r==SOCKET_ERROR)
354+
if (r!=SOCKET_ERROR)
355+
returnb;/* success */
356+
if (WSAGetLastError()!=WSAEWOULDBLOCK)
357357
{
358-
if (WSAGetLastError()==WSAEWOULDBLOCK)
359-
{
360-
/*
361-
* There seem to be cases on win2k (at least) where WSARecv
362-
* can return WSAEWOULDBLOCK even when
363-
* pgwin32_waitforsinglesocket claims the socket is readable.
364-
* In this case, just sleep for a moment and try again. We try
365-
* up to 5 times - if it fails more than that it's not likely
366-
* to ever come back.
367-
*/
368-
pg_usleep(10000);
369-
continue;
370-
}
371358
TranslateSocketError();
372359
return-1;
373360
}
374-
returnb;
361+
362+
/*
363+
* There seem to be cases on win2k (at least) where WSARecv can return
364+
* WSAEWOULDBLOCK even when pgwin32_waitforsinglesocket claims the
365+
* socket is readable. In this case, just sleep for a moment and try
366+
* again. We try up to 5 times - if it fails more than that it's not
367+
* likely to ever come back.
368+
*/
369+
pg_usleep(10000);
375370
}
376371
ereport(NOTICE,
377372
(errmsg_internal("could not read from ready socket (after retries)")));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp