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

Unnecessary caught ClassCastException due to incorrect instance check #1992

Closed
fluidsonic opened this issue May 6, 2020 · 0 comments
Closed
Labels

Comments

@fluidsonic
Copy link

fluidsonic commented May 6, 2020

While debugging with exception breakpoints I get a lot of internally caught exceptions like this:

ClassCastException: class io.ktor.server.netty.EventLoopGroupProxy cannot be cast
to class java.util.concurrent.ScheduledThreadPoolExecutor

In the following code the executor is cast to ScheduledExecutorService but the reflected Method requires a ScheduledThreadPoolExecutor.

Looks like a bug to me.

private val REMOVE_FUTURE_ON_CANCEL: Method? = try {
ScheduledThreadPoolExecutor::class.java.getMethod("setRemoveOnCancelPolicy", Boolean::class.java)
} catch (e: Throwable) {
null
}
@Suppress("NAME_SHADOWING")
internal fun removeFutureOnCancel(executor: Executor): Boolean {
try {
val executor = executor as? ScheduledExecutorService ?: return false
(REMOVE_FUTURE_ON_CANCEL ?: return false).invoke(executor, true)
return true
} catch (e: Throwable) {
return true
}
}

@elizarov elizarov added the bug label May 6, 2020
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
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