Discussion:
End thread waiting on ConnectNamedPipe?
(too old to reply)
Vincent Fatica
2010-01-27 03:48:59 UTC
Permalink
I have a thread doing this.

DWORD WINAPI ReadPipeThread(LPVOID lpv)
{
WCHAR Buffer[32768];//, szResult[32];
DWORD dwRead, dwWritten;

while ( bListen )
{
if ( ConnectNamedPipe(hPipe, NULL) ) // blocks
{
if ( ReadFile(hPipe, Buffer, 32768 * sizeof(WCHAR),
&dwRead, NULL) && dwRead )
...

If from another thread I call

VOID ShutDownServer(VOID)
{
Printf(L"Stopping server\r\n");
bListen = FALSE;
if ( hPipe != INVALID_HANDLE_VALUE )
{
CloseHandle(hPipe);
hPipe = INVALID_HANDLE_VALUE;
}
Printf(L"Stopping server\r\n");
//if ( hThread )
//{
// TerminateThread(hThread, 0);
// CloseHandle(hThread);
// hThread = NULL;
//}
}

ShutDownServer() never returns; the thread that calls it hangs! In fact I don't
even see the second message. I'm very surprised, not seeing any reason for that
to happen. Any ideas? Thanks.
--
- Vince
Alexander Grigoriev
2010-01-27 15:08:57 UTC
Permalink
Use overlapped mode.
Post by Vincent Fatica
I have a thread doing this.
DWORD WINAPI ReadPipeThread(LPVOID lpv)
{
WCHAR Buffer[32768];//, szResult[32];
DWORD dwRead, dwWritten;
while ( bListen )
{
if ( ConnectNamedPipe(hPipe, NULL) ) // blocks
{
if ( ReadFile(hPipe, Buffer, 32768 * sizeof(WCHAR),
&dwRead, NULL) && dwRead )
...
If from another thread I call
VOID ShutDownServer(VOID)
{
Printf(L"Stopping server\r\n");
bListen = FALSE;
if ( hPipe != INVALID_HANDLE_VALUE )
{
CloseHandle(hPipe);
hPipe = INVALID_HANDLE_VALUE;
}
Printf(L"Stopping server\r\n");
//if ( hThread )
//{
// TerminateThread(hThread, 0);
// CloseHandle(hThread);
// hThread = NULL;
//}
}
ShutDownServer() never returns; the thread that calls it hangs! In fact I don't
even see the second message. I'm very surprised, not seeing any reason for that
to happen. Any ideas? Thanks.
--
- Vince
Vincent Fatica
2010-01-27 17:09:54 UTC
Permalink
Can you explain? It was CloseHandle() that wasn't returning. I would not have
thought that possible.

On Wed, 27 Jan 2010 07:08:57 -0800, "Alexander Grigoriev" <***@earthlink.net>
wrote:

|Use overlapped mode.
|
|"Vincent Fatica" <***@blackholespam.net> wrote in message
|news:4b5fb7ab$***@news.vefatica.net...
|>I have a thread doing this.
|>
|> DWORD WINAPI ReadPipeThread(LPVOID lpv)
|> {
|> WCHAR Buffer[32768];//, szResult[32];
|> DWORD dwRead, dwWritten;
|>
|> while ( bListen )
|> {
|> if ( ConnectNamedPipe(hPipe, NULL) ) // blocks
|> {
|> if ( ReadFile(hPipe, Buffer, 32768 * sizeof(WCHAR),
|> &dwRead, NULL) && dwRead )
|> ...
|>
|> If from another thread I call
|>
|> VOID ShutDownServer(VOID)
|> {
|> Printf(L"Stopping server\r\n");
|> bListen = FALSE;
|> if ( hPipe != INVALID_HANDLE_VALUE )
|> {
|> CloseHandle(hPipe);
|> hPipe = INVALID_HANDLE_VALUE;
|> }
|> Printf(L"Stopping server\r\n");
|> //if ( hThread )
|> //{
|> // TerminateThread(hThread, 0);
|> // CloseHandle(hThread);
|> // hThread = NULL;
|> //}
|> }
|>
|> ShutDownServer() never returns; the thread that calls it hangs! In fact I
|> don't
|> even see the second message. I'm very surprised, not seeing any reason
|> for that
|> to happen. Any ideas? Thanks.
|> --
|> - Vince
|
--
- Vince
Alexander Grigoriev
2010-01-28 02:54:52 UTC
Permalink
If a handle is opened in non-overlapped mode, all IO requests (including
CLEANUP/CLOSE) are serialized, submitted one by one. Since the connection
request didn't return, CLOSE request doesn't have a chance to even proceed.

In overlapped mode, any pending requests (CONNECT_NAMED_PIPE) will be
canceled when CLEANUP/CLOSE is received.
Post by Vincent Fatica
Can you explain? It was CloseHandle() that wasn't returning. I would not have
thought that possible.
On Wed, 27 Jan 2010 07:08:57 -0800, "Alexander Grigoriev"
|Use overlapped mode.
|
|>I have a thread doing this.
|>
|> DWORD WINAPI ReadPipeThread(LPVOID lpv)
|> {
|> WCHAR Buffer[32768];//, szResult[32];
|> DWORD dwRead, dwWritten;
|>
|> while ( bListen )
|> {
|> if ( ConnectNamedPipe(hPipe, NULL) ) // blocks
|> {
|> if ( ReadFile(hPipe, Buffer, 32768 * sizeof(WCHAR),
|> &dwRead, NULL) && dwRead )
|> ...
|>
|> If from another thread I call
|>
|> VOID ShutDownServer(VOID)
|> {
|> Printf(L"Stopping server\r\n");
|> bListen = FALSE;
|> if ( hPipe != INVALID_HANDLE_VALUE )
|> {
|> CloseHandle(hPipe);
|> hPipe = INVALID_HANDLE_VALUE;
|> }
|> Printf(L"Stopping server\r\n");
|> //if ( hThread )
|> //{
|> // TerminateThread(hThread, 0);
|> // CloseHandle(hThread);
|> // hThread = NULL;
|> //}
|> }
|>
|> ShutDownServer() never returns; the thread that calls it hangs! In fact I
|> don't
|> even see the second message. I'm very surprised, not seeing any reason
|> for that
|> to happen. Any ideas? Thanks.
|> --
|> - Vince
|
--
- Vince
Vincent Fatica
2010-01-28 03:45:19 UTC
Permalink
On Wed, 27 Jan 2010 18:54:52 -0800, "Alexander Grigoriev" <***@earthlink.net>
wrote:

|If a handle is opened in non-overlapped mode, all IO requests (including
|CLEANUP/CLOSE) are serialized, submitted one by one. Since the connection
|request didn't return, CLOSE request doesn't have a chance to even proceed.
|
|In overlapped mode, any pending requests (CONNECT_NAMED_PIPE) will be
|canceled when CLEANUP/CLOSE is received.

Thanks. I see.

I got around it another way. My ShutDownServer() function now uses the normal
client method to write a special message to the pipe. On processing that
message the server won't be in a wait state and will exit its loop (and thread).
--
- Vince
Loading...