Search code examples
androidfilestorageinternal-storageandroid-internal-storage

android.system.ErrnoException: ioctl failed: EBADF (Bad file descriptor) with file from data/data folder


I install the same application from Android Studio onto different devices, it's installed successfully on all devices, but after launch I get the following error when I work with files in internal storage within this app on some devices:

E/java: [FileUploader] java.io.IOException: ioctl failed: EBADF (Bad file descriptor)
        at libcore.io.IoBridge.available(IoBridge.java:74)
        at java.io.FileInputStream.available(FileInputStream.java:108)
        at packagename.FileUploader$1.contentLength(FileUploader.java:530)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:67)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at com.android.tools.profiler.agent.okhttp.OkHttp3Interceptor.intercept(OkHttp3Interceptor.java:57)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
        at packagename.performFileUploadRequest(FileUploader.java:483)
        at packagename.FileUploader.performUploadRequestByType(FileUploader.java:429)
        at com


11-16 11:45:03.138 20253-21432/packagename E/java: .packagename.FileUploader.tryUploadFileToServer(FileUploader.java:393)
        at packagename.FileUploader.lambda$uploadFileToServer$2(FileUploader.java:302)
        at packagename.-$$Lambda$FileUploader$MZ_TG9wHD8CqIUeh0jeiS8COakc.call(lambda)
        at rx.Observable.unsafeSubscribe(Observable.java:10256)
        at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:248)
        at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:148)
        at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
        at rx.observers.Subscribers$5.onNext(Subscribers.java:235)
        at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276)
        at rx.Subscriber.setProducer(Subscriber.java:211)
        at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
        at rx.Subscriber.setProducer(Subscriber.java:205)
        at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138)
        at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129)
        at rx.Observable.unsafeSubscribe(Observable.java:10256)
        at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51)
        at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
        at rx.Observable.unsafeSubscribe(Observable.java:10256)
        at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
        at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
        at rx.Observable.unsafeSubscribe(Observable.java:10256)
        at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.

11-16 11:45:03.138 20253-21432/packagename E/java: java:41)
        at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30)
        at rx.Observable.unsafeSubscribe(Observable.java:10256)
        at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
        at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
        at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.ErrnoException: ioctl failed: EBADF (Bad file descriptor)
        at libcore.io.Posix.ioctlInt(Native Method)
        at libcore.io.ForwardingOs.ioctlInt(ForwardingOs.java:103)
        at libcore.io.IoBridge.available(IoBridge.java:58)
            ... 60 more

And at the same time the same application works on different device without this exception. I checked it on root device and even when this exception occurs zip file exists in data/data/packagename/files folder. The code on which exception points is the following (line with fileStream.available()):

RequestBody requestBody = new RequestBody() {
            @Override
            public MediaType contentType() {
                return MediaType.parse(file.getMimeType());
            }

            @Override
            public long contentLength() throws IOException {
                return fileStream.available();
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                Source source = null;
                try {
                    source = Okio.source(fileStream);
                    sink.writeAll(source);
                } finally {
                    Util.closeQuietly(source);
                }
            }
        };

where RequestBody is abstract class from okhttp3 library. What the reason of this error?

P.S. I also tried to install apk of this application which was built on other computer (I installed it using adb on my computer), which didn't have any significant changes in work with these files, and it runs without exception on the same devices where this exception occurs before... So it confuses me even more...

P.S.S. And when I'm building the same application in Android Studio using terminal and gradlew task, rather than Run button, this application works without this error. So it seems to be something with my Android Studio, but I couldn't even imagine what exactly, and it blows my mind...


Solution

  • It's impossible to believe, but the reason of this was the enabled checkbox in Run/Debug Configuration: "Enable advanced profiling (required for API level < 26 only)". And it explains why on some devices all works correctly - because it works correctly on device with Android 8.0 - i.e. API 26. But how this checkbox is connected with Android file system (or, in particular, internal storage), I still don't understand...