Я работаю над приложением, которое записывает файлы каждый раз, когда поступают новые данные от датчика (я получаю около 266 чисел с плавающей запятой в секунду), и я должен хранить данные каждую минуту, поэтому каждую минуту я 'Я создаю новый файл и пишу на нем, иногда, после нескольких часов, я получаю плохой файловый дескриптор, и я точно не знаю, что происходит.Обычно это происходит через 2:30, но это может длиться дольше
fun startMonitoring() {
createNewFhirPackageForCurrentMonitoring {
currentFhirPackage = it
createNewFileManagers(it)
shouldWriteLogFile = true
startFhirPackageChangerRoutine()
}
}
@Synchronized
private fun createNewFileManagers(fhirPackage: FhirPackage) {
val currentMonitoring = currentMonitoring ?: return
val folder = filesDir
.resolve(currentMonitoring.monitoringName)
.resolve(fhirPackage.packageIdentifier.toString())
dataFilesManager = BraincareDataLogFilesManager(folder)
}
// здесь я меняю файлы каждую минуту
private fun startFhirPackageChangerRoutine() {
fhirPackageChangerSubscription = Observable.interval(FHIR_DURATION, FHIR_DURATION, TimeUnit.SECONDS).flatMap {
Observable.fromCallable {
changeFhirPackage()
}
}.subscribe({
}, {
BleLogHelper.writeError("Error on Monitoring Package Change", it)
})
}
private fun changeFhirPackage() {
val closingFhirPackage = currentFhirPackage ?: return
val closingDataFilesManager = dataFilesManager
createNewFhirPackageForCurrentMonitoring {
currentFhirPackage = it
createNewFileManagers(it)
closingDataFilesManager?.closeOutputStreams()
}
}
// Я продолжаю писать в файл, используяэта функция
private var differentialBridgeSignalOutputStream: ObjectOutputStream? = null
fun logDifferentialBridgeSignal(signal: BraincareBluetoothDataFrame.DifferentialBridgeSignal) {
try {
if (differentialBridgeSignalOutputStream == null) {
val file = File(folder, differentialBridgeSignalFileName)
file.parentFile.mkdirs()
file.createNewFile()
differentialBridgeSignalOutputStream = ObjectOutputStream(FileOutputStream(file))
}
differentialBridgeSignalOutputStream?.writeObject(signal)
} catch (e: IOException) {
BleLogHelper.writeError("Error writing DifferentialBridgeSignal data $signal to $differentialBridgeSignalOutputStream $e", e)
}
}
Журнал ошибок
java.io.IOException: write failed: EBADF (Bad file descriptor)
at libcore.io.IoBridge.write(IoBridge.java:558)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1959)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1999)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1475)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at io.b4c.brain4care.modules.monitoringservice.filesmanagers.BraincareStatusLogFilesManager.logSystemInformation(BraincareStatusLogFilesManager.kt:32)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.handleStatusFrame(BraincareMonitoringService.kt:257)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.access$handleStatusFrame(BraincareMonitoringService.kt:45)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:158)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:102)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.processFrame(BraincareBluetoothManager.kt:797)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.handleBluetoothNotification(BraincareBluetoothManager.kt:724)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.access$handleBluetoothNotification(BraincareBluetoothManager.kt:40)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:632)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:40)
at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:201)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:255)
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)
Caused by: android.system.ErrnoException: write failed: EBADF (Bad file descriptor)
at libcore.io.Linux.writeBytes(Native Method)
at libcore.io.Linux.write(Linux.java:286)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:345)
at libcore.io.IoBridge.write(IoBridge.java:553)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1959)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1999)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1475)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at io.b4c.brain4care.modules.monitoringservice.filesmanagers.BraincareStatusLogFilesManager.logSystemInformation(BraincareStatusLogFilesManager.kt:32)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.handleStatusFrame(BraincareMonitoringService.kt:257)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService.access$handleStatusFrame(BraincareMonitoringService.kt:45)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:158)
at io.b4c.brain4care.modules.monitoringservice.BraincareMonitoringService$LocalBinder$clearAndPrepareMonitoringService$2.invoke(BraincareMonitoringService.kt:102)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.processFrame(BraincareBluetoothManager.kt:797)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.handleBluetoothNotification(BraincareBluetoothManager.kt:724)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager.access$handleBluetoothNotification(BraincareBluetoothManager.kt:40)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:632)
at io.b4c.brain4care.modules.monitoringservice.BraincareBluetoothManager$startReadingSensorNotification$3.accept(BraincareBluetoothManager.kt:40)
at io.reactivex.internal.observers.LambdaObserver.onNext(LambdaObserver.java:63)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:201)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:255)
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)
Что может вызвать эту ошибку, условия гонки?Это вызывает у меня сильную реакцию, потому что довольно случайно, когда это произойдет