{"id":48,"date":"2005-10-23T17:45:26","date_gmt":"2005-10-24T00:45:26","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/?p=48"},"modified":"2005-10-23T20:53:52","modified_gmt":"2005-10-24T03:53:52","slug":"victory-over-process-completed","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/48\/victory-over-process-completed","title":{"rendered":"Victory over &#8220;[Process Completed]&#8221;!"},"content":{"rendered":"<p>I have been plagued by a bug I see often in Terminal. I know it&#8217;s been at least months, and it may have even been years. But it&#8217;s gotten worse lately. I call it the &#8220;phantom [Process Completed] bug.&#8221; The symptoms of this issue are that every so often an innocent attempt to open a new Terminal window instead yields an immediate &#8220;[Process Completed]&#8221;. What this has meant for me is that I&#8217;ve basically gotten used to &#8220;trying again&#8221; whenever I see this. It sucks though, because it limits my ability to automate with confidence when it comes to opening new shell windows. The thing about this bug is seems to &#8220;happen only every other time per pseudo-tty.&#8221; So if it fails once, it will probably not fail the very next time you try it! A dangerous recipe for &#8220;acceptance,&#8221; but this is a terrible concession to have to make to a computer!<\/p>\n<p>\nThings finally came to a head today and I decided to track down the root cause. I posted to the MacOSX-Dev mailing list for help, and Chad Leigh pointed me in the direction of some very helpful advice that, while it alleviates a separate problem I was experiencing (running out of processes, and getting fork failures), it doesn&#8217;t solve the &#8220;Process Completed&#8221; problem.\n<\/p>\n<p>\nI needed to collect some hard data, so I started out by rebooting my machine. On a freshly booted machine, I am able to create a dozen new Terminal windows without running into the problem a single time. Close those dozen windows and try again, and about half of them fail! So this has something to do with reusing pseudo-TTYs? I decided to switch over to &#8220;iTerm&#8221; for a little comparison. Sure enough, opening a dozen windows there yields about the same failure rate.\n<\/p>\n<p>\nI searched the web (again, I had tried this months ago), and came up with some interesting leads. Some people have noticed that the problem is worse with zsh. I run zsh, lucky me! Other people have noticed that the problem only happens when a Terminal window is closed without manually closing down the shell inside first. Sure enough, I almost always close my shells with Cmd-W, allowing Terminal to &#8220;take out the trash.&#8221;  I run some more tests, and what do you know? I can &#8220;cleanse&#8221; ttys by making sure that I run &#8220;exit&#8221; on the shell before closing the window.\n<\/p>\n<p>\nSince the rumor is that it happens more in zsh than other shells, I&#8217;m inclined to believe it&#8217;s a shell bug. I decide to test against a very simple shell: &#8220;bc&#8221; the built in UNIX calculator. Setting my default shell to &#8220;\/usr\/bin\/bc&#8221;, I go on a rampage of opening a dozen or more new shells, closing them all, and trying again. Sure enough, &#8220;bc&#8221; is completely clean. It can take a beating and happily reopen on the same old tty that got slammed last time-around. I switch back to zsh, and open a bunch of terminals again. They&#8217;re all clean! So bc left them clean enough for zsh&#8217;s liking. It must be something about the way zsh &#8220;takes out the trash&#8221; (or doesn&#8217;t).\n<\/p>\n<p>\nI switch the default shell to &#8220;\/bin\/sh&#8221;. In twelve opens, I witness the failures due to booby-traps set by zsh. Close twelve. Open twelve again: all clean. Sh (bash, actually) cleans up well! Try with tcsh: all clean, too!\n<\/p>\n<p>\nDamn you zsh! I love ya, but you&#8217;re a messy sucker!\n<\/p>\n<p>\nI&#8217;m not about to give up zsh, and I&#8217;m not quite ready to look into locating the bug in zsh, but I need to work around this. I know that if I type &#8220;exit&#8221; every time I need to close a shell window, I will never see the bug again, but I&#8217;m too lazy to remember to do that! FastScripts\/AppleScript tag team to the rescue. I wrote this simple script:\n<\/p>\n<p><div style=\"margin-left:20px; white-space:pre; line-height:0.7em;\"><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">tell<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">application<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> &quot;Terminal&quot;<br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">try<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">do script<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> &quot;exit&quot; <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">in<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">window<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> 1<br \/>\n\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">close<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">window<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> 1<br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">on<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">error<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">beep<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">end<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">try<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">end<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">tell<\/span><\/div>\n<\/p>\n<p>\nWith FastScripts I&#8217;m able to override just about any menu shortcut in just about any app, so I set it up as an Terminal-specific script, and assign it the shortcut Cmd-W. Now, whenever I type Cmd-W in Terminal, the same shortcut I&#8217;ve been using for years to close shell sessions, the Terminal first performs an explicit &#8220;exit&#8221; in the window, and then closes it. I get to keep my behavior, but the buggy zsh conditions are no longer produced.\n<\/p>\n<p>\nI&#8217;m so excited by this that I&#8217;ve spent a few minutes just holding Cmd-N down for several seconds, closing all the windows, and repeating. Ah, it&#8217;s going to be a nice autumn.<\/p>\n<p>\n<strong>Update:<\/strong> I thought I had observed some funkiness with the state of the pseudo tty files after zsh was closed on them, but I&#8217;ve come to find out that the behavior is not unique to zsh. I think it&#8217;s a red herring. Other shells sometimes leave the tty in this funky state and it doesn&#8217;t seem to affect their ability to reopen the tty as a healthy session.<\/p>\n<p>\n<strong>Update 2:<\/strong> It occurred to me that one of the downsides to replacing Cmd-W with a scripted keyboard shortcut is that I can&#8217;t take advantage of the &#8220;hold down the keys for repeated invocation&#8221; functionality that you normally get for free with menu items.\n<\/p>\n<p>\nI decided the only reason I was taking advantage of this in Terminal is because it lacks a proper &#8220;Close All&#8221; function. So, I wrote another script, this one mapped to Cmd-Opt-W in Terminal, which will first go through and call &#8220;exit&#8221; in all the open windows, and then close them all:<\/p>\n<div style=\"margin-left:20px; white-space:pre; line-height:0.7em;\"><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">tell<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">application<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> &quot;Terminal&quot;<br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">return<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">count<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">of<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">windows<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">if<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> ((<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">count<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">of<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">windows<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">) <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">is<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">equal to<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> 0) <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">then<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">beep<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">else<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">repeat<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">with<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#408000; font-family:'Verdana'; font-size:10.0pt;\">thisWindowNumber<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">from<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> 1 <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">to<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> (<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">count<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">of<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">windows<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">)<br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">do script<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> &quot;exit&quot; <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">in<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">window<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#408000; font-family:'Verdana'; font-size:10.0pt;\">thisWindowNumber<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">end<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">repeat<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><\/p>\n<p>\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">repeat<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">count<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">of<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">windows<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">times<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">close<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana'; font-size:10.0pt;\">window<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> 1<br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">end<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">repeat<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\">\t<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">end<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">if<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"><br \/>\n<\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">end<\/span><span style=\"color:#000000; font-family:'Verdana'; font-size:10.0pt;\"> <\/span><span style=\"color:#0000FF; font-family:'Verdana-Bold'; font-size:10.0pt;\">tell<\/span><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been plagued by a bug I see often in Terminal. I know it&#8217;s been at least months, and it may have even been years. But it&#8217;s gotten worse lately. I call it the &#8220;phantom [Process Completed] bug.&#8221; The symptoms of this issue are that every so often an innocent attempt to open a [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6,12],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-applescript","category-fastscripts","category-usability"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}