这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 3c1a6be

Browse files
Changed: Do not hang indefinitely when exceptions are thrown in TermuxApiReceiver and ResultReturner
1 parent 6112bf6 commit 3c1a6be

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

app/src/main/java/com/termux/api/TermuxApiReceiver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.termux.api.apis.WallpaperAPI;
4747
import com.termux.api.apis.WifiAPI;
4848
import com.termux.api.activities.TermuxApiPermissionActivity;
49+
import com.termux.api.util.ResultReturner;
4950
import com.termux.shared.data.IntentUtils;
5051
import com.termux.shared.logger.Logger;
5152
import com.termux.shared.termux.TermuxConstants;
@@ -70,6 +71,8 @@ public void onReceive(Context context, Intent intent) {
7071

7172
TermuxCrashUtils.sendPluginCrashReportNotification(context, LOG_TAG,
7273
TermuxConstants.TERMUX_API_APP_NAME + " Error", message, e);
74+
75+
ResultReturner.noteDone(this, intent);
7376
}
7477
}
7578

app/src/main/java/com/termux/api/util/ResultReturner.java

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,38 +148,41 @@ public static void returnData(Object context, final Intent intent, final ResultW
148148
final Activity activity = (Activity) ((context instanceof Activity) ? context : null);
149149

150150
final Runnable runnable = () -> {
151+
PrintWriter writer = null;
152+
LocalSocket outputSocket = null;
151153
try {
152154
final ParcelFileDescriptor[] pfds = { null };
153-
try (LocalSocket outputSocket = new LocalSocket()) {
154-
String outputSocketAdress = intent.getStringExtra(SOCKET_OUTPUT_EXTRA);
155-
outputSocket.connect(new LocalSocketAddress(outputSocketAdress));
156-
try (PrintWriter writer = new PrintWriter(outputSocket.getOutputStream())) {
157-
if (resultWriter != null) {
158-
if (resultWriter instanceof BinaryOutput) {
159-
BinaryOutput bout = (BinaryOutput) resultWriter;
160-
bout.setOutput(outputSocket.getOutputStream());
161-
}
162-
if (resultWriter instanceof WithInput) {
163-
try (LocalSocket inputSocket = new LocalSocket()) {
164-
String inputSocketAdress = intent.getStringExtra(SOCKET_INPUT_EXTRA);
165-
inputSocket.connect(new LocalSocketAddress(inputSocketAdress));
166-
((WithInput) resultWriter).setInput(inputSocket.getInputStream());
167-
resultWriter.writeResult(writer);
168-
}
169-
} else {
170-
resultWriter.writeResult(writer);
171-
}
172-
if(resultWriter instanceof WithAncillaryFd) {
173-
int fd = ((WithAncillaryFd) resultWriter).getFd();
174-
if (fd >= 0) {
175-
pfds[0] = ParcelFileDescriptor.adoptFd(fd);
176-
FileDescriptor[] fds = { pfds[0].getFileDescriptor() };
177-
outputSocket.setFileDescriptorsForSend(fds);
178-
}
179-
}
155+
outputSocket = new LocalSocket();
156+
String outputSocketAdress = intent.getStringExtra(SOCKET_OUTPUT_EXTRA);
157+
Logger.logDebug(LOG_TAG, "Connecting to output socket \"" + outputSocketAdress + "\"");
158+
outputSocket.connect(new LocalSocketAddress(outputSocketAdress));
159+
writer = new PrintWriter(outputSocket.getOutputStream());
160+
161+
if (resultWriter != null) {
162+
if (resultWriter instanceof BinaryOutput) {
163+
BinaryOutput bout = (BinaryOutput) resultWriter;
164+
bout.setOutput(outputSocket.getOutputStream());
165+
}
166+
if (resultWriter instanceof WithInput) {
167+
try (LocalSocket inputSocket = new LocalSocket()) {
168+
String inputSocketAdress = intent.getStringExtra(SOCKET_INPUT_EXTRA);
169+
inputSocket.connect(new LocalSocketAddress(inputSocketAdress));
170+
((WithInput) resultWriter).setInput(inputSocket.getInputStream());
171+
resultWriter.writeResult(writer);
172+
}
173+
} else {
174+
resultWriter.writeResult(writer);
175+
}
176+
if(resultWriter instanceof WithAncillaryFd) {
177+
int fd = ((WithAncillaryFd) resultWriter).getFd();
178+
if (fd >= 0) {
179+
pfds[0] = ParcelFileDescriptor.adoptFd(fd);
180+
FileDescriptor[] fds = { pfds[0].getFileDescriptor() };
181+
outputSocket.setFileDescriptorsForSend(fds);
180182
}
181183
}
182184
}
185+
183186
if(pfds[0] != null) {
184187
pfds[0].close();
185188
}
@@ -202,14 +205,23 @@ public static void returnData(Object context, final Intent intent, final ResultW
202205
activity.setResult(1);
203206
}
204207
} finally {
208+
try {
209+
if (writer != null)
210+
writer.close();
211+
if (outputSocket != null)
212+
outputSocket.close();
213+
} catch (Exception e) {
214+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to close", e);
215+
}
216+
205217
try {
206218
if (asyncResult != null) {
207219
asyncResult.finish();
208220
} else if (activity != null) {
209221
activity.finish();
210222
}
211223
} catch (Exception e) {
212-
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to cleanup", e);
224+
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to finish", e);
213225
}
214226
}
215227
};

0 commit comments

Comments
 (0)