Re: Why is my overlapped WriteFile getting blocked? (repost)

From: Yan-Hong Huang[MSFT] (yhhuang_at_online.microsoft.com)
Date: 06/21/04


Date: Mon, 21 Jun 2004 06:39:51 GMT

Hello,

Ray discussed it to me and I am looking into this issue.

Based on my understanding, now the issue is: You are using overlapped IO in named pipe programming. However, you found that if using . in
pipe name, WriteFile could return immediately. However, if using machine name in pipe name, WriteFile won't return when the server side is
not reading pipe. The main concern here is that WriteFile doesn't return immediately, which we feel it shoud be. Right?

I tested it on my side. However, I didn't reproduce the problem. Here are my testing steps for your reference:

Let's start with that NPServer and NPClient sample in Platform SDK.

1) In NPServer, in ServerProc function, we just add a sleep:
   TellAll(""); // Forces a paint, draws a red spool
                                       // and name for this client.

                                       // Do loop which basically reads from
                                       // this specific client, and then
                                       // uses TellAll() to broadcast the
                                       // message to all the connected
                                       // clients.
   do{
          Sleep(5000); ///////////// we added code here
                                       // Read the pipe.
      retCode = ReadFile (hPipe, inBuf, PLEASE_READ, &bytesRead, &OverLapRd);
...

2) In client side, in NPClient, in ClientDlgProc function, in IDB_SEND, we revised the code as the following:
                  sendBuf[OUT_BUF_SIZE - 1] = '\0'; // null terminate it

                                       // Do the overlapped write.
      SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"beginning writing pipe...");
                                       // Add a new line.
      SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"\r\n");

          retCode = WriteFile (hPipe, sendBuf, PLEASE_WRITE,
                     &bytesWritten, &OverLapWrt);
          if (!retCode)
            {
            lastError = GetLastError();
                                       // If Error = IO_PENDING, wait til
                                       // the event signals success.
            if (lastError == ERROR_IO_PENDING)
                        {
      SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"Now we are in ERROR_IO_PENGING...");
                                       // Add a new line.
      SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"\r\n");
              WaitForSingleObject (hEventWrt, (DWORD)-1);
          SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"Now we are leaving ERROR_IO_PENGING...");
                                       // Add a new line.
      SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"\r\n");
                        }
                        else
                        {
          SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"This time we don't enter ERROR_IO_PENGING...");
                                       // Add a new line.
      SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
                   EM_REPLACESEL,
                   0, (LONG_PTR)"\r\n");
                        }
            }

So we run the test sample, the log in the client window is:

beginning writing pipe...
Now we are in ERROR_IO_PENGING...
Now we are leaving ERROR_IO_PENGING...
test1: 1
beginning writing pipe...
Now we are in ERROR_IO_PENGING...
Now we are leaving ERROR_IO_PENGING...
test1: 1
beginning writing pipe...
Now we are in ERROR_IO_PENGING...
Now we are leaving ERROR_IO_PENGING...
test1: 1
beginning writing pipe...
Now we are in ERROR_IO_PENGING...
Now we are leaving ERROR_IO_PENGING...
test1: 1
beginning writing pipe...
Now we are in ERROR_IO_PENGING...
Now we are leaving ERROR_IO_PENGING...
test1: 1

So in my test, all the writing operation enters error_io_pending block and then after 5 seconds, it gets returned data and print out the result. I
did test with . and machine name. All have the same result.

If the above test has any different part from yours, could you please point it out? Or you can email me one repro sample and I am glad to test
it on my side. If we can reproduce it, I will contact product group on it. You can reach me by removing online from my email address here.

Also, I noticed that you added a (repost) in the post title. I not not sure of why you didn't get a response former since you didn't mention the title
of the former post. In order to receive response timely, I high recommend you post questions by using your MSDN subsciber no spam email
alias.

Thanks vey much. If you have any more concerns, please feel free to post here.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.



Relevant Pages