Overview
| Artifact ID: | 8e3d8cb9374dda7d3199e8736775ae5e0b6b8c72 |
|---|---|
| Ticket: | 219188ffffffffffffffffffffffffffffffffff
"exec" not freeing all memory |
| User & Date: | nobody on 2000-10-26 05:03:11 |
Changes
- assignee changed to: "nobody"
- cmimetype changed to: "text/plain"
- comment changed to:
OriginalBugID: 2460 Bug Version: 8.1.1 SubmitDate: '1999-07-28' LastModified: '2000-09-07' Severity: CRIT Status: Assigned Submitter: techsupp ChangedBy: davidg RelatedBugIDs: 3903 4382 5353 6130 OS: Windows NT Machine: X86 FixedDate: '2000-10-25' ClosedDate: '2000-10-25' Name: Micah Jeremy Pullen Comments: Normally this wouldn't affect me, but I'm iterating through several hundred calls through "exec". After about 1 hour, "tclsh81.exe" hangs, and I have to kill the process. I'll eventually replace the "exec" calls with a loadable extension, but I don't have the time right now. ReproducibleScript: Platform: Windows NT 4.0 (Build 1381: Service Pack 4) 1. Run Windows NT's command line 2. "tclsh81" 3. Run Windows NT Task Manager and find "tclsh81.exe" in the process list. 4. Note the amount of memory used by "tclsh81.exe" 5. On the tcl command line: "cmd.exe /c dir" 6. Memory usage will NOT change (i.e. cmd.exe isn't grabbing memory) 7. On the tcl command line: "exec cmd.exe /c dir" 8. Memory usage WILL increase 9. Repeat step 7 multiple times. Memory usage increases in 4KB (or more) increments. This does seem to be the case, but when I run this through Purify, with the above example and several of the tests, I receive no mem leak errors. It is possible (likely) that this is some sort of system leak, in which case Purify may not report it, or it is assumed to be left to the system to clean it up. -- 10/27/1999 hobbs It's also reported that this same/related bug causes hangs (3903). From Don Bowman (who attached patch): There were two separate problems I've tried to address here (a search through deja news shows this problem has been lurking for years). First, a memory leak. The leak was caused by using CreateThread from C runtime library. "http://msdn.microsoft.com/library/psdk/winbase/prothred_4084.htm" "A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called. " The solution I've implemented is to use beginthread (and remove the explicit calls to CloseHandle, which is not needed). There was also some problems with deadlocks due to the use of TerminateThread. This caused troubles for the process space of the entire application. See http://msdn.microsoft.com/library/psdk/winbase/prothred_7h2c.htm for more information. In particular, if the OS were in the middle of a WaitFor... bad stuff happened, including more leaks. More deadlocks then arose due to the Mutex, which is no longer needed during the exit/termination phase since the reader/writer threads now exit under their own power. The only downside I think to my proposed patch is that the speed of terminating the exec'd thread is increased by ~200ms. I suppose this could be resolved by changing the WaitForSingleObjects to WaitForMultipleObjects, and then using an event instead of a flag to signal the termination of the reader/writer thread. An example program to illustrate these two bugs is below: set i 0 while { 1 } { catch {exec ls} incr i puts $i } and then run 'perfmon', graph the working set of the tcl.exe process. It increases linearly with each exec until sometime later it hangs due to the deadlocks present. Neither case should happen now, I've tested for >20000 iterations on 3 different speed of computers, Windows 2000 & Windows NT 4.0 SP5. Sorry, didn't try Win9x. Attached patch: *** tclWinPipe.c.originalTue Mar 21 20:59:33 2000 --- tclWinPipe.cWed Mar 22 00:06:55 2000 *************** *** 111,120 **** --- 111,124 ---- * This value is used by the reader/writer * threads. */ HANDLE writeThread;/* Handle to writer thread. */ + BOOLEAN writerExit; /* Would like the writer to exit */ + BOOLEAN writerExited; /* Writer has exited */ HANDLE readThread;/* Handle to reader thread. */ HANDLE writable;/* Manual-reset event to signal when the * writer thread has finished waiting for * the current buffer to be written. */ + BOOLEAN readerExit; /* Would like the reader to exit */ + BOOLEAN readerExited; /* Reader has exited */ HANDLE readable;/* Manual-reset event to signal when the * reader thread has finished waiting for * input. */ *************** *** 194,203 **** int toRead, int *errorCode); static intPipeOutputProc(ClientData instanceData, char *buf, int toWrite, int *errorCode); ! static DWORD WINAPIPipeReaderThread(LPVOID arg); static voidPipeSetupProc(ClientData clientData, int flags); static voidPipeWatchProc(ClientData instanceData, int mask); ! static DWORD WINAPIPipeWriterThread(LPVOID arg); static voidProcExitHandler(ClientData clientData); static intTempFileName(WCHAR name[MAX_PATH]); static intWaitForRead(PipeInfo *infoPtr, int blocking); --- 198,207 ---- int toRead, int *errorCode); static intPipeOutputProc(ClientData instanceData, char *buf, int toWrite, int *errorCode); ! static void PipeReaderThread(LPVOID arg); static voidPipeSetupProc(ClientData clientData, int flags); static voidPipeWatchProc(ClientData instanceData, int mask); ! static void PipeWriterThread(LPVOID arg); static voidProcExitHandler(ClientData clientData); static intTempFileName(WCHAR name[MAX_PATH]); static intWaitForRead(PipeInfo *infoPtr, int blocking); *************** *** 1594,1599 **** --- 1598,1607 ---- PipeInit(); + infoPtr->readerExit = FALSE; + infoPtr->writerExit = FALSE; + infoPtr->readerExited = TRUE; + infoPtr->writerExited = TRUE; infoPtr->watchMask = 0; infoPtr->flags = 0; infoPtr->readFlags = 0; *************** *** 1632,1639 **** infoPtr->readable = CreateEvent(NULL, TRUE, TRUE, NULL); infoPtr->startReader = CreateEvent(NULL, FALSE, FALSE, NULL); ! infoPtr->readThread = CreateThread(NULL, 8000, PipeReaderThread, ! infoPtr, 0, &id); SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST); infoPtr->validMask |= TCL_READABLE; } else { --- 1640,1646 ---- infoPtr->readable = CreateEvent(NULL, TRUE, TRUE, NULL); infoPtr->startReader = CreateEvent(NULL, FALSE, FALSE, NULL); ! infoPtr->readThread = _beginthread(PipeReaderThread, 8000, infoPtr); SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST); infoPtr->validMask |= TCL_READABLE; } else { *************** *** 1646,1653 **** infoPtr->writable = CreateEvent(NULL, TRUE, TRUE, NULL); infoPtr->startWriter = CreateEvent(NULL, FALSE, FALSE, NULL); ! infoPtr->writeThread = CreateThread(NULL, 8000, PipeWriterThread, ! infoPtr, 0, &id); SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST); infoPtr->validMask |= TCL_WRITABLE; } --- 1653,1659 ---- infoPtr->writable = CreateEvent(NULL, TRUE, TRUE, NULL); infoPtr->startWriter = CreateEvent(NULL, FALSE, FALSE, NULL); ! infoPtr->writeThread = _beginthread(PipeWriterThread, 8000, infoPtr); SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST); infoPtr->validMask |= TCL_WRITABLE; } *************** *** 1802,1829 **** */ if (pipePtr->readThread) { ! /* ! * Forcibly terminate the background thread. We cannot rely on the ! * thread to cleanly terminate itself because we have no way of ! * closing the pipe handle without blocking in the case where the ! * thread is in the middle of an I/O operation. Note that we need ! * to guard against terminating the thread while it is in the ! * middle of Tcl_ThreadAlert because it won't be able to release ! * the notifier lock. ! */ ! ! Tcl_MutexLock(&pipeMutex); ! TerminateThread(pipePtr->readThread, 0); /* * Wait for the thread to terminate. This ensures that we are * completely cleaned up before we leave this function. */ - WaitForSingleObject(pipePtr->readThread, INFINITE); - Tcl_MutexUnlock(&pipeMutex); - - CloseHandle(pipePtr->readThread); CloseHandle(pipePtr->readable); CloseHandle(pipePtr->startReader); pipePtr->readThread = NULL; --- 1808,1830 ---- */ if (pipePtr->readThread) { ! /* ! * Cause the background thread to exist, under its own ! * control. This allows the OS to cleanup its resources, ! * close its handle, etc. Since its exiting on its own ! * we need not be concerned about resources owned etc, ! * it will clean them up first ! */ ! pipePtr->readerExit = TRUE; ! SetEvent(pipePtr->startReader); /* Force it to start if not */ /* * Wait for the thread to terminate. This ensures that we are * completely cleaned up before we leave this function. */ + while (pipePtr->readerExited == FALSE) + SleepEx(10, FALSE); CloseHandle(pipePtr->readable); CloseHandle(pipePtr->startReader); pipePtr->readThread = NULL; *************** *** 1845,1873 **** if (pipePtr->writeThread) { WaitForSingleObject(pipePtr->writable, INFINITE); ! /* ! * Forcibly terminate the background thread. We cannot rely on the ! * thread to cleanly terminate itself because we have no way of ! * closing the pipe handle without blocking in the case where the ! * thread is in the middle of an I/O operation. Note that we need ! * to guard against terminating the thread while it is in the ! * middle of Tcl_ThreadAlert because it won't be able to release ! * the notifier lock. ! */ ! Tcl_MutexLock(&pipeMutex); ! TerminateThread(pipePtr->writeThread, 0); /* * Wait for the thread to terminate. This ensures that we are * completely cleaned up before we leave this function. */ ! WaitForSingleObject(pipePtr->writeThread, INFINITE); ! Tcl_MutexUnlock(&pipeMutex); ! - CloseHandle(pipePtr->writeThread); CloseHandle(pipePtr->writable); CloseHandle(pipePtr->startWriter); pipePtr->writeThread = NULL; --- 1846,1870 ---- if (pipePtr->writeThread) { WaitForSingleObject(pipePtr->writable, INFINITE); ! /* ! * Cause the background thread to exist, under its own ! * control. This allows the OS to cleanup its resources, ! * close its handle, etc. Since its exiting on its own ! * we need not be concerned about resources owned etc, ! * it will clean them up first ! */ ! pipePtr->writerExit = TRUE; ! SetEvent(pipePtr->startWriter); /* Force it to start if not */ /* * Wait for the thread to terminate. This ensures that we are * completely cleaned up before we leave this function. */ ! while (pipePtr->writerExited == FALSE) ! SleepEx(10, FALSE); CloseHandle(pipePtr->writable); CloseHandle(pipePtr->startWriter); pipePtr->writeThread = NULL; *************** *** 2063,2070 **** DWORD bytesWritten, timeout; *errorCode = 0; ! timeout = (infoPtr->flags & PIPE_ASYNC) ? 0 : INFINITE; if (WaitForSingleObject(infoPtr->writable, timeout) == WAIT_TIMEOUT) { /* * The writer thread is blocked waiting for a write to complete * and the channel is in non-blocking mode. --- 2060,2077 ---- DWORD bytesWritten, timeout; *errorCode = 0; ! ! /* ! * If we're intended to block permanently, allow the WaitFor... to ! * come up for air periodically to check the 'exit' command ! * If its set, return, else continue to block ! */ ! timeout = (infoPtr->flags & PIPE_ASYNC) ? 0 : 200; ! again: if (WaitForSingleObject(infoPtr->writable, timeout) == WAIT_TIMEOUT) { + if (infoPtr->flags & PIPE_ASYNC && !infoPtr->writerExit) { + goto again; + } /* * The writer thread is blocked waiting for a write to complete * and the channel is in non-blocking mode. *************** *** 2552,2560 **** /* * Synchronize with the reader thread. */ ! ! timeout = blocking ? INFINITE : 0; if (WaitForSingleObject(infoPtr->readable, timeout) == WAIT_TIMEOUT) { /* * The reader thread is blocked waiting for data and the channel * is in non-blocking mode. --- 2559,2576 ---- /* * Synchronize with the reader thread. */ ! /* ! * If we're intended to block permanently, allow the WaitFor... to ! * come up for air periodically to check the 'exit' command ! * If its set, return, else continue to block ! */ ! ! timeout = blocking ? 200 : 0; ! again: if (WaitForSingleObject(infoPtr->readable, timeout) == WAIT_TIMEOUT) { + if (blocking && !infoPtr->readerExit) { + goto again; + } /* * The reader thread is blocked waiting for data and the channel * is in non-blocking mode. *************** *** 2651,2665 **** *---------------------------------------------------------------------- */ ! static DWORD WINAPI PipeReaderThread(LPVOID arg) { PipeInfo *infoPtr = (PipeInfo *)arg; HANDLE *handle = ((WinFile *) infoPtr->readFile)->handle; DWORD count, err; int done = 0; ! while (!done) { /* * Wait for the main thread to signal before attempting to wait. */ --- 2667,2682 ---- *---------------------------------------------------------------------- */ ! static void PipeReaderThread(LPVOID arg) { PipeInfo *infoPtr = (PipeInfo *)arg; HANDLE *handle = ((WinFile *) infoPtr->readFile)->handle; DWORD count, err; int done = 0; + infoPtr->readerExited = FALSE; ! while (!done && !infoPtr->readerExit) { /* * Wait for the main thread to signal before attempting to wait. */ *************** *** 2731,2737 **** Tcl_ThreadAlert(infoPtr->threadId); Tcl_MutexUnlock(&pipeMutex); } ! return 0; } /* --- 2748,2754 ---- Tcl_ThreadAlert(infoPtr->threadId); Tcl_MutexUnlock(&pipeMutex); } ! infoPtr->readerExited = TRUE; } /* *************** *** 2752,2758 **** *---------------------------------------------------------------------- */ ! static DWORD WINAPI PipeWriterThread(LPVOID arg) { --- 2769,2775 ---- *---------------------------------------------------------------------- */ ! static void PipeWriterThread(LPVOID arg) { *************** *** 2761,2768 **** DWORD count, toWrite; char *buf; int done = 0; ! while (!done) { /* * Wait for the main thread to signal before attempting to write. */ --- 2778,2786 ---- DWORD count, toWrite; char *buf; int done = 0; + infoPtr->writerExited = FALSE; ! while (!done && !infoPtr->writerExit) { /* * Wait for the main thread to signal before attempting to write. */ *************** *** 2804,2809 **** Tcl_ThreadAlert(infoPtr->threadId); Tcl_MutexUnlock(&pipeMutex); } ! return 0; } --- 2822,2827 ---- Tcl_ThreadAlert(infoPtr->threadId); Tcl_MutexUnlock(&pipeMutex); } ! infoPtr->writerExited = TRUE; } -- 03/30/2000 hobbs Subject: Re: Resource leak causes a blocking exec-call Date: Thu, 02 Dec 1999 09:31:38 +0100 From: "Dipl. Ing. Matthias Jansen" I look in the runtime-sources from Microsoft for beginthread, not in documentation. Yesterday, i had a lot of trouble with that what you explain, but maybe i have a solution. Please try this out. I test it only on NT4 against the tcl-test-scripts and it run. Index: tclWinPipe.c =================================================================== RCS file: /home/cvs/external/tcl/win/tclWinPipe.c,v retrieving revision 1.11 diff -c -b -r1.11 tclWinPipe.c *** tclWinPipe.c2000/04/11 21:42:121.11 --- tclWinPipe.c2000/04/25 17:16:17 *************** *** 87,92 **** --- 87,93 ---- #define PIPE_EOF(1<<2)/* Pipe has reached EOF. */ #define PIPE_EXTRABYTE(1<<3)/* The reader thread has consumed one byte. */ + #define PIPE_SHUTDOWN(1<<4)/* read pipe is shutdown */ /* * This structure describes per-instance data for a pipe based channel. *************** *** 1826,1840 **** */ Tcl_MutexLock(&pipeMutex); ! TerminateThread(pipePtr->readThread, 0); /* * Wait for the thread to terminate. This ensures that we are * completely cleaned up before we leave this function. */ - WaitForSingleObject(pipePtr->readThread, INFINITE); Tcl_MutexUnlock(&pipeMutex); CloseHandle(pipePtr->readThread); CloseHandle(pipePtr->readable); --- 1827,1842 ---- */ Tcl_MutexLock(&pipeMutex); ! pipePtr->readFlags |= PIPE_SHUTDOWN; ! SetEvent(pipePtr->startReader); /* * Wait for the thread to terminate. This ensures that we are * completely cleaned up before we leave this function. */ Tcl_MutexUnlock(&pipeMutex); + WaitForSingleObject(pipePtr->readThread, INFINITE); CloseHandle(pipePtr->readThread); CloseHandle(pipePtr->readable); *************** *** 2671,2676 **** --- 2673,2679 ---- HANDLE *handle = ((WinFile *) infoPtr->readFile)->handle; DWORD count, err; int done = 0; + char buf[2]; while (!done) { /* *************** *** 2686,2707 **** * we can read a single byte off of the pipe. */ ! if ((ReadFile(handle, NULL, 0, &count, NULL) == FALSE) ! || (PeekNamedPipe(handle, NULL, 0, NULL, &count, ! NULL) == FALSE)) { ! /* ! * The error is a result of an EOF condition, so set the ! * EOF bit before signalling the main thread. ! */ ! err = GetLastError(); if (err == ERROR_BROKEN_PIPE) { infoPtr->readFlags |= PIPE_EOF; done = 1; - } else if (err == ERROR_INVALID_HANDLE) { break; } } else if (count == 0) { if (ReadFile(handle, &(infoPtr->extraByte), 1, &count, NULL) != FALSE) { /* --- 2689,2719 ---- * we can read a single byte off of the pipe. */ ! do { ! if (PeekNamedPipe(handle, buf, 1, NULL, &count, NULL) == FALSE) { err = GetLastError(); if (err == ERROR_BROKEN_PIPE) { infoPtr->readFlags |= PIPE_EOF; done = 1; break; + } else if (err == ERROR_INVALID_HANDLE) { + return 0; } + } else { + Tcl_MutexLock(&pipeMutex); + if (infoPtr->readFlags & PIPE_SHUTDOWN) { + Tcl_MutexUnlock(&pipeMutex); + return 0; } else if (count == 0) { + Tcl_MutexUnlock(&pipeMutex); + Sleep(100); + } else { + Tcl_MutexUnlock(&pipeMutex); + } + } + } while (count == 0); + + if (!done && (count == 0)) { if (ReadFile(handle, &(infoPtr->extraByte), 1, &count, NULL) != FALSE) { /* --- The patch from Bowman causes hangs in event.test 8.1 and the one from Jansen causes intermittent problems in io.test (Tcl_InputBlocked test) and intermittent hangs in socket.test (threaded and non-threaded). -- 04/25/2000 hobbs Stage-1 bug fix applied to mainline. This seems to fix 2/3 of it. I still consider this bug to be open. Closing down the pipe reader thread when blocked in ReadFile() while the child process is still active is backwards for windows. -- 09/07/2000 davidg - foundin changed to: "final: 8.1.1"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- status changed to: "Open"
- submitter changed to: "nobody"
- subsystem changed to: "60. NRE and coroutines"
- title changed to: ""exec" not freeing all memory"
- type changed to: "Bug"