Share via

PeekNamedPipe unexpected behaviour (bug?)

x33 1 Reputation point
2021-08-22T15:45:41.73+00:00

I was recently scratching my head for some time while i was working with named pipes.

So in my setup, i had the "reader" and the "writer" ends of a pipe created in "PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE" mode. The writer called twice the WriteFile api to send some data to the reader. After the second call to WriteFile by the writer, the reader used the PeekNamedPipe to peek at the data written by the writer. To my surprise, i was always getting only the data of the first WriteFile call even though the peek length included both writes.

After reading more carefully the documentation, i noticed the following paragraph in the remarks: "The data is read in the mode specified with CreateNamedPipe. For example, create a pipe with PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE. If you change the mode to PIPE_READMODE_BYTE with SetNamedPipeHandleState, ReadFile will read in byte mode, but PeekNamedPipe will continue to read in message mode."

So this appears to be related with my problem, it looks like the peek operation tries to read the data in message mode. The only issue is that i had the pipe opened with "PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE" which based on the documentation, should have allowed the peek operation to read all the data of the pipe.

Am i missing something? Is this a bug of the implementation, documentation or do i have a flaw in my thinking process?

I am also curious to hear the logic behind this design decision of peek operation, as it doesn't look like it makes much sense.

It is noted that i was using Windows 21H1, but the same behaviour was observed in all the versions i have experimented with.

Thanks

Windows development | Windows API - Win32
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,736 Reputation points Microsoft External Staff
    2021-08-27T07:23:30.763+00:00

    Hello, we have opened a feedback at https://aka.ms/AAdn3v1 about PeekNamedPipe unexpected behaviour. You can track the feedback progress.

    1 person found this answer helpful.

  2. Xiaopo Yang - MSFT 12,736 Reputation points Microsoft External Staff
    2021-08-23T03:15:27.213+00:00

    I have tested the condition based on The Pipe Sample. After WriteFile twice, There is no problem on ReadFile, but on PeekNamedPipe although lpTotalBytesAvail of PeekNamedPipe indicates there is more data.
    Edit: It seems that PeekNamedPipe is related to PIPE_TYPE_MESSAGE and PIPE_TYPE_BYTE.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.