tcl hangs on parallel execution of commands using open
Closed / Invalid3536564fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 14 years ago
Descriptionplain2012-06-20 11:28:05
Hi,
I am looking to run a couple of processes in parallel. But a strange hang occurs randomly. I am invoking two tcl commands using open in parallel. Now after a while one of the process would just hang indefinitely which I am not able to understand why. The issue is easily reproducible on win 2008 server and other multi core machines. Am I doing something wrongly? What is the best way to do this?
To reproduce, you just have to create the files and run loop.bat. You should see the hang within 15-20 minutes.
Following are the files I am using
********
loop.bat
******
@echo off
:loop
tclsh 1.tcl
goto loop
*****************
Contents of file 1.tcl
*****************
#!/bin/sh
#DO NOT CHANGE THOSE 3 FIRST LINES \
exec tclsh $0 "$@"
set status [catch { exec tclsh 2.tcl } outcmd]
puts $outcmd
exit 0
*******
Contents of file 2.tcl
*****
#!/bin/sh
#DO NOT CHANGE THOSE 3 FIRST LINES \
exec tclsh $0 "$@"
set cmd1 "tclsh 6.tcl &"
set cmd2 "tclsh 5.tcl &"
set out1 ""
set out2 ""
proc isProcessDone { channel logText } {
gets $channel currentline
while {[eof $channel] == 0} {
set logText "$logText\n $currentline"
gets $channel currentline
}
return $logText
}
puts "hi"
set imChannel1 [open "| $cmd1"]
puts "hi1 $imChannel1"
set imChannel2 [open "| $cmd2"]
puts "hi2 $imChannel2"
#set imChannel3 [open "| $cmd"]
if { ${imChannel1} != "" } {
puts 1
fileevent $imChannel1 readable [set out1 [isProcessDone $imChannel1 $out1]]
puts 11
puts $out1
}
if { ${imChannel2} != "" } {
puts 2
fileevent $imChannel2 readable [set out2 [isProcessDone $imChannel2 $out2]]
puts 22
puts $out2
}
puts "bye"
exit 0
****
Contents of 5.tcl and 6.tcl
******
#!/bin/sh
#DO NOT CHANGE THOSE 3 FIRST LINES \
exec tclsh $0 "$@"
puts "\ncallexec 6 started"
puts "\ncallexec 6 terminated"
Please help.
TIA.
Deepanshu
anonymous Ticket [3536564fff]: 1 change
[History diff]2012-06-22 11:40:44
andreas_kupriesplain2012-07-18 00:33:04
Hello Deepanshu.
Thank you for providing test scripts. Reviewing them I find fairly big problems in 2.tcl, and the attempted use of fileevents. First, the lines
fileevent $imChannel1 readable [set out1 [isProcessDone $imChannel1 $out1]]
(and equivalent for channel 2) have the problem that 'isProcessDone' is executed immediately, and not in response to fileevents. Because you are using [...]-quoting which evaluates the command directly. This also means that while you are opening 2 pipes, you are then looking at them sequentially, not in parallel.
Further, you are not reconfiguring your channel to be non-blocking, which is strongly recommended when working with fileevents. Otherwise your read/gets in a fileevent handler procedure can block. Making things look like a hang.
Then 'isProcessDone' itself accesses the channel in a loop waiting for EOF. With fileevents such a loop would not be necessary, as your handler would get called when EOF is reached, allowing you to test and close, without an explicit loop.
Lastly, as you are using tclsh no standard event loop is started (like wish would do), and you are not starting an event loop either (I see no 'vwait' command).
With all these problems in the code I currently believe that the source of your problem (hang) is to be found in there, and not in the Tcl core itself.
Examples of proper fileevent usage can be found at
http://www.activestate.com/blog/2010/05/concurrency-tcl-events-without-getting-twisted
http://wiki.tcl.tk/1757
http://www.tcl.tk/man/tcl8.5/tutorial/Tcl40.html
plain2012-07-19 00:01:33
Thanks for the update Andreas. I debugged the problem further and nailed it down to exec hanging on exit when invoked in a nested code. Say I have 1.tcl which contains exec 2.tcl now 2.tcl says exec tclsh 3.tcl and 3.tcl says puts "Hello" I have noticed this problem on 64-bit Windows multi-core machines with 32-bit Tcl 8.4 and also 8.6. Please let me know if you need more info.
plain2012-08-02 01:02:25
Hi Andreas, Can you please see if this nesting exec problem on 64-bit machines can be looked into? I have seen this problem with ActiveTcl 8.6 also. Regards, Deepanshu
dkfplain2012-08-02 04:51:13
If the problems are due to Tcl scripts that simply cannot work, that's not a bug in Tcl's implementation but rather in the scripts. This is not a general Tcl script writing support forum (that would be the comp.lang.tcl newsgroup, or possibly the Stack Overflow website).
ferrieuxplain2012-08-02 18:25:30
allow_comments - 1 Non-dev comments closed now. Hint: go to comp.lang.tcl for further assistance.
Log in (anonymous is fine) to add a comment.