instacart / truetime-android

Android NTP time library. Get the true current time impervious to device clock time changes
https://tech.instacart.com/truetime/
Apache License 2.0
1.41k stars 194 forks source link

android.system.ErrnoException on SntpClient #119

Open cesarnorena opened 5 years ago

cesarnorena commented 5 years ago

Using the 3.4 Rx version of the lib, with initializeNtp I get this error android.system.ErrnoException sendto failed: EPERM (Operation not permitted). This happens with a lot of Android versions and device brands.

Caused by android.system.ErrnoException: sendto failed: EPERM (Operation not permitted)
       at libcore.io.Linux.sendtoBytes(Linux.java)
       at libcore.io.Linux.sendto(Linux.java:225)
       at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:304)
       at libcore.io.IoBridge.sendto(IoBridge.java:569)
       at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:124)
       at java.net.DatagramSocket.send(DatagramSocket.java:721)
       at com.instacart.library.truetime.SntpClient.doubleMillis(SntpClient.java:10)
       at com.instacart.library.truetime.SntpClient.requestTime(SntpClient.java:116)
       at com.instacart.library.truetime.TrueTime.requestTime(TrueTime.java:133)
       at com.instacart.library.truetime.TrueTimeRx$4$1$2.subscribe(TrueTimeRx.java:211)
       at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
       at io.reactivex.Flowable.subscribe(Flowable.java:14805)
       at io.reactivex.Flowable.subscribe(Flowable.java:14752)
       at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82)
       at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
       at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
       at java.lang.Thread.run(Thread.java:764)
dradding commented 5 years ago

We've been experiencing this too. Any suggestions?

calhouncole commented 5 years ago

Us too. Any advice would be much appreciated.

guyromb commented 5 years ago

Same

LosDanieloss commented 5 years ago

Based on stacktrace and TrueTimeRx source code it looks like Flowable created for a flatmap tries to emit onNext after chain was disposed. If that's the case you could try "sweeping it under the rug" by using RxJavaPlugins.onError Ref. https://github.com/ReactiveX/RxJava/issues/4880

LosDanieloss commented 5 years ago

I believe I've found the issue. Inside SntpClient.java and TrueTime.java there are methods requestTime() throws IOException which are responsible for whole connection magic. In the one from SntpClient.java you can see:

catch (Exception e) {
    TrueLog.d(TAG, "---- SNTP request failed for " + ntpHost);
    throw e;
}

After checking Android docs it turns out that ErrnoException doesn't extends IOException and that is causing the crashes because TrueTimeRx wraps call to requestTime() and only expects to catch IOExpcetions. Any other exception is pushed forward to RxJavaPlugins.onError() to be handled which by default crashes as of RxJava2. I think changing requestTime() throws IOException to requestTime() throws Exception ( or even Throwable) should solve issue. I'd try to submit PR with this shortly.

sixpi commented 5 years ago

I'm not sure the requestTime() is capable of throwing ErrnoExceptions - requestTime() is calling socket.send(), which only throws IOExceptions. Sockets already catch ErrnoException and rethrow as IOException.

LosDanieloss commented 5 years ago

I'm afraid you are right @sixpi :( Will take a look at it once again if I found some free time. All help would be appreciated. So if you have some information don't hesitate to share

radvansky-tomas commented 3 years ago

Any update on this ?