Skip to content

Commit 47ebea0

Browse files
committed
Fix bg starter bug, fix process output reading bug
1 parent 571eb20 commit 47ebea0

File tree

5 files changed

+59
-12
lines changed

5 files changed

+59
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This app includes bitcoind and bitcoin-cli compiled for Android. All command lin
44

55
Run it for fun, or to help me experiment with NAT traversal and other new capabilities. Please do not rely on this app to serve your wallet or you might lose all your funds.
66

7-
Download on PlayStore, or build yourself.
7+
Download on [PlayStore](https://play.google.com/store/apps/details?id=org.lndroid.bitcoincore), or build yourself.
88

99
To download the debug.log file from the app, use:
1010

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
77
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
8+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
89

910
<application
1011
android:allowBackup="true"

app/src/main/java/org/lndroid/bitcoincore/AutoStartWorker.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Intent;
77
import android.content.ServiceConnection;
88
import android.content.SharedPreferences;
9+
import android.os.Handler;
910
import android.os.IBinder;
1011
import android.os.Message;
1112
import android.os.Messenger;
@@ -31,12 +32,14 @@ public class AutoStartWorker extends androidx.work.ListenableWorker {
3132

3233
private static final long WORK_INTERVAL = 15 * 60 * 1000; // 15 min
3334

35+
private Handler handler_;
3436
private CallbackToFutureAdapter.Completer<Result> completer_;
3537

3638
private ServiceConnection connection_ = new ServiceConnection() {
3739
public void onServiceConnected(ComponentName className,
3840
IBinder service) {
3941

42+
Log.i(TAG, "sending start command");
4043
Messenger daemon = new Messenger(service);
4144
try {
4245
Message msg = Message.obtain(null, DaemonService.MSG_START);
@@ -45,11 +48,21 @@ public void onServiceConnected(ComponentName className,
4548
Log.e(TAG, "Failed to send to daemon: "+e.getLocalizedMessage());
4649
}
4750

48-
// we're done
49-
AutoStartWorker.this.getApplicationContext().unbindService(connection_);
50-
51-
// ok
52-
completer_.set(Result.success());
51+
// pause to let daemon turn into foreground service so that
52+
// OS wouldn't kill our process. we can't block the current thread
53+
// bcs service will be started on the same thread
54+
Log.i(TAG, "waiting for service to start");
55+
handler_.postDelayed(new Runnable() {
56+
@Override
57+
public void run() {
58+
// we're done
59+
Log.i(TAG, "done");
60+
AutoStartWorker.this.getApplicationContext().unbindService(connection_);
61+
62+
// ok
63+
completer_.set(Result.success());
64+
}
65+
}, 5000);
5366
}
5467

5568
public void onServiceDisconnected(ComponentName className) {
@@ -63,6 +76,7 @@ public void onServiceDisconnected(ComponentName className) {
6376

6477
public AutoStartWorker(@NonNull Context context, @NonNull WorkerParameters params) {
6578
super(context, params);
79+
handler_ = new Handler(getApplicationContext().getMainLooper());
6680
}
6781

6882
@NonNull

app/src/main/java/org/lndroid/bitcoincore/DaemonService.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
import org.json.JSONException;
3030
import org.json.JSONObject;
3131

32+
import java.io.BufferedInputStream;
33+
import java.io.BufferedReader;
34+
import java.io.ByteArrayOutputStream;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.io.InputStreamReader;
3238
import java.lang.ref.WeakReference;
3339
import java.util.LinkedList;
3440
import java.util.ListIterator;
@@ -310,6 +316,28 @@ private void onDone(String result) {
310316

311317
Log.i(TAG,"stopped");
312318

319+
// check if parent as killed and we have a detached child daemon now,
320+
// this is bad and we should terminate the child ASAP
321+
/* if (result.contains("Cannot obtain a lock on data directory")) {
322+
// get pid of orphaned bitcoid
323+
try {
324+
java.lang.Process p = new ProcessBuilder("ps").start();
325+
BufferedReader r = new BufferedReader(
326+
new InputStreamReader(p.getInputStream(), "UTF-8"));
327+
String line;
328+
while ((line = r.readLine()) != null) {
329+
if (line.contains("libbitcoind.so")) {
330+
331+
}
332+
}
333+
334+
335+
} catch (IOException e) {
336+
// do not loop/retry. just let
337+
// bg worker retry starting the daemon
338+
}
339+
}
340+
*/
313341
// notify clients about status change
314342
notifyClients(MSG_STATUS, "Stopped");
315343

app/src/main/java/org/lndroid/bitcoincore/ProcessWorker.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import android.widget.Toast;
66

77
import java.io.BufferedReader;
8+
import java.io.ByteArrayOutputStream;
89
import java.io.File;
910
import java.io.IOException;
1011
import java.io.InputStream;
1112
import java.io.InputStreamReader;
1213
import java.lang.reflect.Field;
14+
import java.nio.charset.StandardCharsets;
1315
import java.util.ArrayList;
1416
import java.util.List;
1517
import java.util.concurrent.Callable;
@@ -109,8 +111,8 @@ public void run() {
109111
InputStream stdout = p.getInputStream();
110112
InputStream stderr = p.getErrorStream();
111113

112-
StringBuilder out = new StringBuilder();
113-
StringBuilder err = new StringBuilder();
114+
ByteArrayOutputStream out = new ByteArrayOutputStream();
115+
ByteArrayOutputStream err = new ByteArrayOutputStream();
114116

115117
byte[] buf = new byte[256];
116118
boolean alive = true;
@@ -119,12 +121,12 @@ public void run() {
119121
final int max = Math.min(stdout.available(), buf.length);
120122
final int r = stdout.read(buf, 0, max);
121123
if (!stdoutDevnull_)
122-
out.append(new String(buf, 0, r, "UTF-8"));
124+
out.write(buf, 0, r);
123125
}
124126
if (stderr.available() > 0) {
125127
final int max = Math.min(stderr.available(), buf.length);
126128
final int r = stderr.read(buf, 0, max);
127-
err.append(new String(buf, 0, r, "UTF-8"));
129+
err.write(buf, 0, r);
128130
}
129131

130132
if (alive) {
@@ -140,10 +142,12 @@ public void run() {
140142
}
141143
}
142144

145+
byte[] data;
143146
if (p.exitValue() != 0)
144-
result_.set(err.toString());
147+
data = err.toByteArray();
145148
else
146-
result_.set(out.toString());
149+
data = out.toByteArray();
150+
result_.set(new String(data, 0, data.length, StandardCharsets.UTF_8));
147151

148152
Log.i(TAG, "process "+command_.get(0)+" done "+p.exitValue());
149153
Log.i(TAG, "result "+result_.get());

0 commit comments

Comments
 (0)