Search code examples
c++multithreadingnamed-pipes

Multi-threaded named pipe in a Windows service locks up clients


I have a Windows service that has multiple named pipes. Each pipe is created in a separate thread within the service.

There are multiple clients sending/receiving information from these pipes.

If I don't use Overlapped I/O, I get a lockup when calling CallNamedPipe() from two different clients. I used this example: Multi-threaded MSDN Example

If I use Overlapped I/O, I don't get any lockups, but I can only have one pipe thread using Overlapped I/O. I used this example: Named Pipe using Overlapped I/O

Any idea why?

Here is some example muliti-threaded code without Overlapped I/O. When two clients go in a test loop and constantly call the pipe, PipeA, one of them locks up immediately when calling CallNamedPipe().

#include "windows.h"
#include <process.h>
#include <strsafe.h>

typedef struct
{
HANDLE hPipe;
}ThreadParams_hPipe;

const DWORD BUFSIZE = 2048;
void Thread_NamedPipeServer_PipeA(void *);

int main()
{
    // ... service stuff
    _beginthread(Thread_NamedPipeServer_PipeA, 0, 0);
    // ... code to wait for svc to end
    return 0;
}

void PipeA(LPVOID lpvParam)
{
    ThreadParams_hPipe *connect_params = (ThreadParams_hPipe *)lpvParam;
    DWORD dwBytesRead, dwReplyBytes, dwWritten;
    TCHAR chRead[64] = { 0 }, sReply[16] = { 0 };

    HANDLE hPipe = (HANDLE)connect_params->hPipe;
    if (hPipe)
        {
        while (1)
            {
            BOOL bSuccess = ReadFile(hPipe, chRead, BUFSIZE * sizeof(TCHAR), &dwBytesRead, NULL);
            if (!bSuccess || dwBytesRead == 0)
                break;
            chRead[dwBytesRead / sizeof(TCHAR)] = 0;    // If it received a value, if it's not null terminated, then put one here.

            StringCchCopy(sReply, 16, L"A");
            dwReplyBytes = (lstrlenW(sReply) + 1) * sizeof(TCHAR);
            bSuccess = WriteFile(hPipe, sReply, dwReplyBytes, &dwWritten, NULL);

            if ((!bSuccess) || (dwReplyBytes != dwWritten))
                break;
            }
        FlushFileBuffers(hPipe);
        DisconnectNamedPipe(hPipe);
        CloseHandle(hPipe);
        }

}


void Thread_NamedPipeServer_PipeA(void *)
{
    LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\{PipeA}");
    HANDLE hPipe;
    ThreadParams_hPipe connection_params;
    SECURITY_ATTRIBUTES sa; SECURITY_DESCRIPTOR sd;

    InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE);
    sa.nLength = (DWORD) sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = (LPVOID)&sd; sa.bInheritHandle = TRUE;

    while (1)
        {
        hPipe = CreateNamedPipeW(lpszPipename, PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, 0, &sa);
        if (hPipe != INVALID_HANDLE_VALUE)
            {
            if (ConnectNamedPipe(hPipe, NULL))
                {
                connection_params.hPipe = hPipe;
                _beginthread(PipeA, 0, (LPVOID)&connection_params);
                }
            else
                {
                CloseHandle(hPipe);
                break;
                }
            }
        } 

    return;
}

Here is the client code that is freezing on the line CallNamedPipe(), when run on the 2nd client:

#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>

DWORD NamedPipe_CallServerPipe(TCHAR *sServer)
{
    const DWORD BUFSIZE = 1024;
    DWORD cbRead;
    DWORD rv = 0;
    TCHAR sPipename[BUFSIZE] = {0};
    TCHAR sMsgToSend[BUFSIZE] = { 0 };
    TCHAR chReadBuf[BUFSIZE] = { 0 };

    StringCchPrintf(sPipename, BUFSIZE, L"\\\\%s\\pipe\\{PipeA}", sServer);
    StringCchCopy(sMsgToSend, BUFSIZE, L"msg from cilent");

    BOOL bSuccess = CallNamedPipe(sPipename, sMsgToSend, (DWORD)(_tcslen(sMsgToSend) + 1) * sizeof(TCHAR), chReadBuf, BUFSIZE * sizeof(TCHAR), &cbRead, 20000);
    if ((bSuccess) && (chReadBuf[0] == 'A'))
        rv = 1;

    return rv;
}
int main()
{
    DWORD rv;
    for (DWORD i=0; i< 1000000; i++)
        rv = NamedPipe_CallServerPipe(L"Server1");
    return 0;
}

Solution

  • So revisiting this...what actually fixed it was two pieces.

    #1) changing the line:

    from: BOOL bSuccess = ReadFile(hPipe, chRead, BUFSIZE * sizeof(WCHAR), &dwBytesRead, NULL); to: BOOL bSuccess = ReadFile(hPipe, chRead, 64, &dwBytesRead, NULL);

    (because chRead is only of array size 64)

    #2) Sending the hPipe instead of a structure to the thread:

    from: connection_params.hPipe = hPipe; _beginthread(PipeA, 0, (LPVOID)&connection_params); to just: _beginthread(PipeA, 0, hPipe);

    and from: ThreadParams_hPipe *connect_params = (ThreadParams_hPipe *)lpvParam; HANDLE hPipe = (HANDLE)connect_params->hPipe; to just: HANDLE hPipe = static_cast <HANDLE*> (lpvParam);