Skip to content

Commit

Permalink
close InputStream and OutputStream in finally block
Browse files Browse the repository at this point in the history
This should get run most of the time, but not always, since Android could
send `kill -9`.  But this might be better handled in jtorctl's
TorControlConnection, since it wraps those streams.

guardianproject#57
  • Loading branch information
eighthave committed Nov 26, 2021
1 parent 7b0af96 commit 22a9987
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ public void onEvent(String keyword, String data) {
@Override
public void run() {
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
InputStream is = null;
OutputStream os = null;
try {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final String observeDir = getAppTorServiceDataDir(TorService.this).getAbsolutePath();
Expand All @@ -257,8 +259,8 @@ public void onEvent(int event, @Nullable String name) {
}

FileDescriptor controlSocketFd = prepareFileDescriptor(getControlSocket(TorService.this).getAbsolutePath());
InputStream is = new FileInputStream(controlSocketFd);
OutputStream os = new FileOutputStream(controlSocketFd);
is = new FileInputStream(controlSocketFd);
os = new FileOutputStream(controlSocketFd);
torControlConnection = new TorControlConnection(is, os);
torControlConnection.launchThread(true);
torControlConnection.authenticate(new byte[0]);
Expand All @@ -272,6 +274,13 @@ public void onEvent(int event, @Nullable String name) {
e.printStackTrace();
broadcastStatus(TorService.this, STATUS_STOPPING);
TorService.this.stopSelf();
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
}
};
Expand Down

0 comments on commit 22a9987

Please sign in to comment.