1

I have a named pipe server with a main loop where I am reading data from a pipe:

while True:
        bytes_available = DWORD()
        ret_code = windll.kernel32.PeekNamedPipe(pipe, None, 0, None, byref(bytes_available))
        if ret_code == 0 and windll.kernel32.GetLastError() == ERROR_BROKEN_PIPE:
            reconnect()
        buf = create_string_buffer(bytes_available.value)
        ret_code = windll.kernel32.ReadFile(pipe, byref(buf), bytes_available.value, None, overlapped_struct_ptr)
        if ret_code != 0 and bytes_available.value > 0:
            # process(buf.raw)
            pass
        elif ret_code == 0 and windll.kernel32.GetLastError() == ERROR_BROKEN_PIPE:
            reconnect()
        windll.kernel32.GetQueuedCompletionStatus(iocp, byref(bytes_transferred), byref(completion_key),
                                                  byref(overlapped_struct_ptr), INFINITE)

Sometimes it crashes on the create_string_buffer() step with the following message:

Process finished with exit code -1073741819 (0xC0000005)

This seems strange to me, because if I replace create_string_buffer(bytes_available.value) with (c_char*bytes_available.value)() everything works fine. And if we look at the create_string_buffer() implementation, we will see, that it allocates the buffer the same way:

def create_string_buffer(init, size=None):
    """create_string_buffer(aString) -> character array
    create_string_buffer(anInteger) -> character array
    create_string_buffer(aString, anInteger) -> character array
    """
    if isinstance(init, (str, unicode)):
        if size is None:
            size = len(init)+1
        buftype = c_char * size
        buf = buftype()
        buf.value = init
        return buf
    elif isinstance(init, (int, long)):
        buftype = c_char * init
        buf = buftype()
        return buf
    raise TypeError(init)

So why does it fail?

2
  • 1
    windll.kernel32.PeekNamedPipe() call in your code seems to be missing an argument, there should be six, msdn Commented Jun 28, 2016 at 13:14
  • Bingo! I decided to omit the last argument because it was the last one and marked as optional. And I ignored the notice "This parameter can be NULL if no data is to be read." The funny thing is, it worked until I had lots of print statements in the code for debugging puproses. Python has started to crash after I removed print statements for the buffer. Could you add an answer? I will accept it. Commented Jun 28, 2016 at 19:38

1 Answer 1

1

windll.kernel32.PeekNamedPipe() call in your code seems to be missing an argument, there should be six; see msdn

Omitting an argument essentially caused undefined behaviour.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.