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

newFixedThreadPoolContext/newSingleThreadContext can OOM on long timeouts #571

Closed
elizarov opened this issue Sep 17, 2018 · 0 comments
Closed
Assignees
Labels

Comments

@elizarov
Copy link
Contributor

elizarov commented Sep 17, 2018

Consider the following code:

fun main(args: Array<String>) = runBlocking {
    val scope = CoroutineScope(newSingleThreadContext("test"))
    while (true) {
        val job = scope.launch(start = CoroutineStart.ATOMIC) {
            delay(Long.MAX_VALUE)
        }
        job.cancelAndJoin()
    }
}

It creates newSingleThreadContext, then launches and cancels lots of coroutines that do delay(Long.MAX_VALUE) there. This code will end with OutOfMemoryError, because newSingleThreadContext uses ScheduleTheadPoolExecutor but cancelling scheduled tasks in it does not really work (memory is not really released until the trigger time). We'll need to rewrite newSingleThreadContext on top of a better thread-pool or use system-wide (default) delay implementation (aka DefaultExecutor) that does not suffer from this problem. We shall consider this problem in scope of #261.

@elizarov elizarov added the bug label Sep 17, 2018
@qwwdfsad qwwdfsad self-assigned this Sep 25, 2018
qwwdfsad added a commit that referenced this issue Sep 25, 2018
…emoveFutureOnCancel on executor instance and use default dispatcher if attempt failed

Fixes #571
elizarov pushed a commit that referenced this issue Sep 28, 2018
…emoveFutureOnCancel on executor instance and use default dispatcher if attempt failed

Fixes #571
elizarov added a commit that referenced this issue May 6, 2020
I've failed to write a reliable test here. See also #571

Fixes #1992
elizarov added a commit that referenced this issue May 7, 2020
I've failed to write a reliable test here. See also #571

Fixes #1992
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants