Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker never ends if I use kable in it #567

Open
solvek opened this issue Sep 13, 2023 · 4 comments
Open

Worker never ends if I use kable in it #567

solvek opened this issue Sep 13, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@solvek
Copy link

solvek commented Sep 13, 2023

If I just create a periferal instance inside worker then this worker never ends.

I am enqueueing a unique work. For the first time it runs fine (I see in logs Finished worker), when I enqueue again the work it does not start. I assume this is because the previous work was not finished.

@HiltWorker
class DfuWorker @AssistedInject constructor(@Assisted private val context: Context,
                                            @Assisted workerParameters: WorkerParameters
) : CoroutineWorker(context, workerParameters) {
    override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
        Timber.tag(TAG).d("Started")

        // If I comment this line all works as expected
        val peripheral = peripheral(Biosensors.bluetoothDevice(deviceId))

        Timber.tag(TAG).d("Finished worker")
        Result.success()
    }

    companion object {
        private const val TAG = "DfuWorker"

        fun WorkManager.runDfuWorker(deviceId: String, deviceType: DeviceType){
            val data = Data
                .Builder()
                .putString(DATA_DEVICE_ID, deviceId)
                .putInt(DATA_DEVICE_TYPE, deviceType.code)
                .build()

            val workRequest = OneTimeWorkRequest
                .Builder(DfuWorker::class.java)
                .addTag("FIRMWARE_UPDATE")
                .setInputData(data)
                .build()

            enqueueUniqueWork(
                "update_firmware",
                ExistingWorkPolicy.KEEP,
                workRequest
            )

            Timber.tag(TAG).d("Requested enqueue worker for dfu.")
        }
    }
}
@solvek
Copy link
Author

solvek commented Sep 13, 2023

I have the most recent version 0.27.0

@twyatt
Copy link
Member

twyatt commented Sep 13, 2023

This is likely a duplicate of #378.

I've been slowly integrating changes in Kable to address the issue, it will be some time before it can be completely fixed.

In the mean time, one possible workaround is to create a parent Job for your peripheral that you explicitly control its lifecycle, for example:

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
    val scope = CoroutineScope(Job(coroutineContext.job))
    try {
        val peripheral = scope.peripheral(..)
    } finally {
        scope.cancel()
    }
}

@twyatt
Copy link
Member

twyatt commented Sep 25, 2023

I'm hoping to get changes into 0.29.0 that would remove the need for this workaround.

@twyatt twyatt added this to the 0.29.0 milestone Sep 25, 2023
@twyatt twyatt removed this from the 0.29.0 milestone Mar 12, 2024
@twyatt
Copy link
Member

twyatt commented Apr 18, 2024

I'm hoping to get changes into 0.29.0 that would remove the need for this workaround.

Unfortunately lots of other things came up and this didn't happen.
It'll likely be a bit more effort than originally anticipated, so it will be a number of version from now before this is fully resolved. #612 is a small step in the direction needed to fully resolve this, but I haven't had time to test it enough.

I'll try to get back to this when I can, but being that there is a workaround, it might not get prioritized very high vs. some other bugs. Thanks for being patient!

@twyatt twyatt added the bug Something isn't working label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants