Skip to content

Prevent termination races of Worker #3584

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

Closed
wants to merge 1 commit into from
Closed

Conversation

qwwdfsad
Copy link
Member

Fixes #3578

// We deliberately ignore ISE here as they are expected
// due to peculiarities of Workers API.
// Unfortunately, race-free termination is unachievable by its current state,
// see https://github.com/Kotlin/kotlinx.coroutines/issues/3578
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we decide to use #3585, let's still use this change. It prevents IllegalStateException in the case when the worker is terminated before ever entering runBlocking.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think about it, it doesn't look like IllegalStateException can really happen anymore.

    @Test
    fun testRunBlockingInAlreadyTerminatedWorker() {
        val finished = Channel<Unit>()
        Worker.start().apply {
            executeAfter(1_000_000) {
                runBlocking {
                    finished.send(Unit)
                }
            }
        }.requestTermination()
        runBlocking {
            finished.receive()
        }
    }

It passes with no problems. When the worker starts runBlocking, it's obviously doing some work, and that's enough to prevent it from terminating. So, it looks like the issue has been mitigated already.

@qwwdfsad qwwdfsad closed this Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants