开发者

Need to call System.err.close() or JVM won't exit. Why?

开发者 https://www.devze.com 2023-03-15 08:06 出处:网络
We have a (very) multithreaded application that would pass all unit tests, but would not exit when run from the IDE or command line. This app is not only multi-th开发者_如何转开发readed, it executes n

We have a (very) multithreaded application that would pass all unit tests, but would not exit when run from the IDE or command line. This app is not only multi-th开发者_如何转开发readed, it executes native processes, and writes to standard error and standard out. The problem was that the app would hang on exit. Eventually, I reduced the app until it was identical to the unit tests, and it would still hang, so, I figured that JUnit was doing something that a command-line launch was not.

When I called System.exit() at the end of main(args), the app would exit, which finally lead me to System.err.close().

Of course, the app never "opens" System.err or System.out. It just writes to them, and calls flush() when finished. I've only tested on 64 bit Windows, I'll test later on Linux. The JVM is Java(TM) SE Runtime Environment (build 1.6.0_24-b07)

Any ideas why the JVM won't exit?


I seem to remember that when you are calling native processes, you have to be careful to fully read the native processes out and/or err streams, or things can lock up. I think you may need to spawn off a thread to handle this. Just a thought.


All programs on Linux face this problem to some extent. A process with an open filehandle (and you obviously have one, even though you didn't specifically open one, it came from one of your system command operations) won't fully exit until all the handles are closed. The best example I can think of at the moment is a problem I had at work where a startup script (bash) which starte a java process never exited. The problem was, we were doing 2>&1 > /path/to/log & which doesn't close stdin (file handle 0). Chainging it to &> /path/to/log fixed the problem, because &> means all filehandles, while the other just meant stdin and stderr.

Your problem is similar, and the fact that it is multithreaded exacerbates the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号