Overview
| Artifact ID: | 6d4044c06be36cadc4b4d103f131b2510b4fc222 |
|---|---|
| Ticket: | 1958930fffffffffffffffffffffffffffffffff
exec with pipes and fileevents leaks ~2Mb everytime |
| User & Date: | mgammon on 2008-05-07 14:14:25 |
Changes
- icomment:
Logged In: YES user_id=2080734 Originator: YES David, my callback wasn't testing for EOF as a sign of the child's death and closing the pipe then. When I add that and change to a vwait on the death then the script hangs. The callback read chars from the child but never sees a EOF. From the debugger it looks like the Tcl event loop is spinning round and checking for events on the pipes, the pipe says its readable but the read thread is hung in a call to ReadFile. I found this article from MS about how to do this child spawning with pipes in C++: http://support.microsoft.com/kb/190351 Looking at this I think it explains why ReadFile is hanging. It says the parent process MUST close the unused child ends of the pipes after it has spawned the child. If not then the child can't close the pipe properly when it dies, and hence the pipe stays up and ReadFile gets hung on it. I added this to my script and bingo!, it all works perfectly now. Here's the current version of my script: ........................................................... proc callback {out_r} { global isdead echo "read [string length [read $out_r]] chars" ; flush stdout # MUST test for EOF condition and close pipe NOW if {[eof $out_r]} { echo "child has died!" ; flush stdout close $out_r set isdead 1 } } pipe in_r in_w pipe out_r out_w fconfigure $out_r -blocking 0 -buffering none set pid [exec ls <@$in_r >@$out_w &] # MUST close unused channels to child else child can't close pipe when it dies causing read to hang close $in_r close $out_w fileevent $out_r readable "callback $out_r" # wait for "ls" to die vwait isdead close $in_w ........................................................... I've instrumented tclWinPipe to tell me how the pipe read/write threads are stopped. Previously is was using the leaky TerminateThread call on the child reader threads, but now the threads are stopping properly in response to the event signal. Great! There is one thing in the MS article that I'm not doing. For some reason it duplicates the child read and write pipes and closes the original channels it got from CreatePipe. Not sure why its doing this. I tried reproducing this with the TclX "dup" command and after hacking it to allow me to duplicate a pipe (odd restriction?) I got my script doing exactly the same as the MS C++ code. However I found that my script worked just fine WITHOUT doing this extra duplication, so I've left it out. Alexandre: thanks for your help. Just seen your last comment. Looks like you are on to the same solution, i.e. closing the unused pipes after the exec. I think I've confirmed that this does cure the problem. BTW, we are using TclX8.2.0. I don't think I'm out of the woods yet though as when I replace the child process "ls.exe" with my real child process, then I'm still seeing the child reader threads killed by TerminateThread. My real child process does more than "ls" in that it sits and reads from stdin and sends responses down it's stdout and stderr. I'll see if I can pin down why this isn't behaving as cleanly as the simple "ls" example. Thanks a lot to you both for your excellent help! - login: "mgammon"
- mimetype: "text/plain"
- username: "mgammon"