Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Jan Burse Newsgroups: comp.lang.java.programmer Subject: Re: [Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Date: Mon, 12 Mar 2012 20:16:12 +0100 Organization: albasani.net Lines: 57 Message-ID: References: <4f5d18ef$0$291$14726298@news.sunsite.dk> <4f5d239d$0$293$14726298@news.sunsite.dk> <4f5e3743$0$293$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net KMVDgnrOyE5XZfcoJjzI48knx41woSEhDhbjIkStv+vMTywdV3hglSTNCziVLJNCSaW82xjg4S6povsku/iQINbhIQIAvJQ0eQ7Zdxjk3gqdLpYcFrbgPJwOgjUPvCw1 NNTP-Posting-Date: Mon, 12 Mar 2012 19:16:12 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="hVRzu/U7HQBeU1K7eugWCQpOgFVOUA9ahqYatmNza9GWgs0NjhaowkpX+vYK8yM9wLEqtCmCBgT1RwAb0qpCvkLYVoJqdQPEwqI4k1jPrVLh1X4li5Z+tiN7j38NNOfZ"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Firefox/10.0.2 SeaMonkey/2.7.2 In-Reply-To: <4f5e3743$0$293$14726298@news.sunsite.dk> Cancel-Lock: sha1:V+p9wkz00Hvb1WR5FztAAnQhTwk= Xref: csiph.com comp.lang.java.programmer:12909 Arne Vajhøj schrieb: > If a developer chose Java for a task that Java was not designed > for and has never been prioritized since, then it is the > developers faults not Java's fault. Here is an example (although quite an artifical one) which shows how this flaw can harm the end-user. Consider this naive code: System.out.println("Do you want commit/rollback? (^D or ^Z/Enter)"); String str=br.readLine(); if (str=null) { commitTransaction(); } else if ("".equals(str)) { rollbackTransaction(); } Now what do you expect a program to do after the prompt: Do you want commit/rollback? (^D or ^Z/Enter) When the end-user presses ^C. Here is what happens on the different platforms because of this flaw: Mac or Linux: Upon pressing ^C the standard interrupt handler will be invoked, which calls System.exit(), neither commit nor rollback is issued, depending on the database most likely the System.exit() will do a rollback. Windows: A race condition is induced. Upon pressing ^C two things will happen: The readLine() will return null and in the same time the standard interrupt handler will be invoked. If the System.exit() and its shutdown handlers are slow it might be very well that a commit happens. So this is something I called fucked up. If I have time I will try to file a bug, if there isn't yet one. Filing a bug should be easy since "GetLastError returns ERROR_OPERATION_ABORTED" is well documented by MS. Best Regards P.S.: I did some measurements. It appears that usually the readLine() returns before that the INT handler gets invoiked. The delay is about ~1ms. One can easily measure it with the new System.nanoTime(). P.S.S.: Therefore I adopetd the workaround of Gabriel Genellina with a timeout of around 10ms. But this design is also flawed, it might provoke an unrecognized EOF. But it is better than nothing...