3

I'm writing a server that runs in a loop and terminates in case of SIGINT(ctrl-c) or terminate button pressed in the console window of Eclipse IDE. I want it to shutdown gracefully printing out termination logs. But the problem is that println doesn't seem to work while shutdown sequence triggered by pressing the terminate button in Eclipse IDE. Look at the simple code below:

object Test extends App {
  println("start")
  Runtime.getRuntime().addShutdownHook(new Thread {
    override def run = println("shutdown")
  })
  synchronized { wait }
}

It works well with the command line scala tool. Both messages "start" and "shutdown" are printed when I hit ctrl-c. But the "shutdown" message isn't printed when I run it in Eclipse IDE and hit the terminate button of the console window. It just terminates silently. I've verified that everything else in the shutdown hook runs correctly while termination. It's only println that doesn't work.

Any idea about this? I need to print out termination messages for logging.
Thanks for your help in advance!

K J
  • 4,505
  • 6
  • 27
  • 45

3 Answers3

4

Shutdown hooks don't get run by Eclipse when shutting down a process: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38016

That bug report is resolved as Won't fix

I realise that its an old bug and so maybe things have changed since then.

Channing Walton
  • 3,977
  • 1
  • 30
  • 59
  • Thanks for the link, Channing. (+1 vote :) It is good to know the history of the problem and also that it is not only me who have had the problem. Things seem to have changed a bit now. As I stated, shutdown hooks get called correctly now. It is only println that is not working while shutdown. – K J Jul 19 '12 at 04:47
  • You could try flushing System.out too – Channing Walton Jul 19 '12 at 07:09
  • FYI this is not fixed as of Sept 2014, and it also makes scala's `sys addShutdownHook` not work. Externally sending a SIGTERM to the java process Eclipse launches causes everything to work as expected (including the console log) – Hamy Sep 22 '14 at 13:17
1

You can try the shutDownHook which scala provides

sys addShutdownHook(println("shutdown")) 
FUD
  • 5,114
  • 7
  • 39
  • 61
  • Thanks for the heads-up, FUD, though it didn't solve the problem. :) – K J Jul 19 '12 at 04:48
  • Doesn't work for Eclipse as of Sept 2014, due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=38016. You have to send SIGTERM to the java process yourself – Hamy Sep 22 '14 at 13:18
1

Try logging the shutdown messages to a file instead. Eclipse probably closes its end of the stdout pipe before the program is really terminated.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • Thanks for the answer, Kim. That's a possible workaround and it actually works, though I'm still trying to find a way to get the console work. If it turns out that there's no alternative at this point, I'll mark your answer as accepted soon. ;) – K J Jul 19 '12 at 04:53