[kaffe] Pipes kept open after executing subprocesses
Ito Kazumitsu
ito.kazumitsu at mail.hidec.co.jp
Mon Jun 7 17:15:03 PDT 2004
In message "[kaffe] Pipes kept open after executing subprocesses"
on 04/06/07, Ito Kazumitsu <ito.kazumitsu at mail.hidec.co.jp> writes:
> The attached program shows that some pipes used for executing subprocesses
> are not closed when they are no longer needed. This will cause a
> "too many open files" error in a long run.
> I am afraid that FileDescriptor sync_fd in
> libraries/javalib/kaffe/lang/UNIXProcess.java has something to do
> with this problem.
I do not know how the FileDescriptor sync_fd is used, but I think
it should be closed when the subprocess is finished.
Not only sync_fd but also stdin, stdout and stderr should be
closed because the application that called the subprocess
may not have closed them.
So here is my suggested patch.
--- kaffe/lang/UNIXProcess.java.orig Tue Apr 13 00:18:30 2004
+++ kaffe/lang/UNIXProcess.java Tue Jun 8 08:57:30 2004
@@ -34,6 +34,7 @@
OutputStream stdin_stream;
InputStream raw_stdout;
InputStream raw_stderr;
+ FileOutputStream sync;
Throwable throwable; // saved to rethrow in correct thread
public UNIXProcess(final String argv[], final String arge[], File dir)
@@ -75,6 +76,10 @@
this.notifyAll();
}
synchronized(UNIXProcess.this) {
+ try_close(raw_stdout);
+ try_close(raw_stderr);
+ try_close(stdin_stream);
+ try_close(sync);
UNIXProcess.this.notifyAll();
}
}
@@ -116,7 +121,7 @@
raw_stderr = new FileInputStream(stderr_fd);
// now signal child to proceed
- FileOutputStream sync = new FileOutputStream(sync_fd);
+ sync = new FileOutputStream(sync_fd);
byte[] sbuf = new byte[1];
try {
sync.write(sbuf);
@@ -164,6 +169,7 @@
raw_stdout.close();
raw_stderr.close();
stdin_stream.close();
+ sync.close();
}
catch (IOException e) {
e.printStackTrace();
@@ -185,5 +191,23 @@
private native static void sendSignal0(int pid, int signum);
private native static int getKillSignal();
+private static void try_close(InputStream stream) {
+ if (stream != null) {
+ try {
+ stream.close();
+ }
+ catch (IOException e) {}
+ }
+}
+
+private static void try_close(OutputStream stream) {
+ if (stream != null) {
+ try {
+ stream.close();
+ }
+ catch (IOException e) {}
+ }
+}
+
}
More information about the kaffe
mailing list