Skip to content

Commit

Permalink
[JVM] Support overriding RPCWatchdog termination behavior on Android …
Browse files Browse the repository at this point in the history
…and other platforms (apache#6216)

* Instead of performing a system exit and leaving unhandled items on
the activity stack, finish the RPCActivity and return cleanly to the
MainActivity where the RPCActivity can be restarted automatically.

* Update doc. string for checkstyle.
  • Loading branch information
csullivan authored and Trevor Morris committed Sep 2, 2020
1 parent 9dd3613 commit 34aad1a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onClick(View v) {
int port = intent.getIntExtra("port", 9090);
String key = intent.getStringExtra("key");

tvmServerWorker = new RPCProcessor();
tvmServerWorker = new RPCProcessor(this);
tvmServerWorker.setDaemon(true);
tvmServerWorker.start();
tvmServerWorker.connect(host, port, key);
Expand All @@ -62,5 +62,6 @@ protected void onDestroy() {
System.err.println("rpc activity onDestroy");
tvmServerWorker.disconnect();
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.tvm.tvmrpc;

import android.app.Activity;
import org.apache.tvm.rpc.RPCWatchdog;

/**
* Watchdog for Android RPC.
*/
public class RPCAndroidWatchdog extends RPCWatchdog {
public Activity rpc_activity = null;
public RPCAndroidWatchdog(Activity activity) {
super();
rpc_activity = activity;
}

/**
* Method to non-destructively terminate the running thread on Android
*/
@Override protected void terminate() {
rpc_activity.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.tvm.tvmrpc;

import android.app.Activity;
import android.os.ParcelFileDescriptor;
import java.net.Socket;
import org.apache.tvm.rpc.ConnectTrackerServerProcessor;
import org.apache.tvm.rpc.RPCWatchdog;

/**
* Connect to RPC proxy and deal with requests.
Expand All @@ -33,9 +33,15 @@ class RPCProcessor extends Thread {
private long startTime;
private ConnectTrackerServerProcessor currProcessor;
private boolean first = true;
private Activity rpc_activity = null;

public RPCProcessor(Activity activity) {
super();
rpc_activity = activity;
}

@Override public void run() {
RPCWatchdog watchdog = new RPCWatchdog();
RPCAndroidWatchdog watchdog = new RPCAndroidWatchdog(rpc_activity);
watchdog.start();
while (true) {
synchronized (this) {
Expand Down
9 changes: 8 additions & 1 deletion jvm/core/src/main/java/org/apache/tvm/rpc/RPCWatchdog.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public synchronized void finishTimeout() {
} else {
System.err.println("watchdog woke up!");
System.err.println("terminating...");
System.exit(0);
terminate();
}
} catch (InterruptedException e) {
System.err.println("watchdog interrupted...");
Expand All @@ -80,4 +80,11 @@ public synchronized void finishTimeout() {
}
}
}

/**
* Default method to terminate the running RPCActivity process.
*/
protected void terminate() {
System.exit(0);
}
}

0 comments on commit 34aad1a

Please sign in to comment.