이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: kotlin-security/no-finalizers-on-exit

Language: Kotlin

Severity: Error

Category: Security

CWE: 833

Description

This ensures the proper termination of Kotlin programs. It is generally considered unsafe to use the System.runFinalizersOnExit(true) method because it can lead to unpredictable program behavior. This method forces all objects undergoing finalization to be finalized when the JVM exits, which can cause problems if an object is in the middle of a critical operation.

Instead of System.runFinalizersOnExit(true), you can use the Java Runtime API’s addShutdownHook method. This method registers a new virtual-machine shutdown hook, meaning it adds a thread to run when the JVM begins its shutdown sequence. This allows you to handle any cleanup actions yourself, providing a safer and more predictable termination process.

Non-Compliant Code Examples

fun foo() {
  System.runFinalizersOnExit(true)
}

Compliant Code Examples

fun main() {
    Runtime.getRuntime().addShutdownHook(object : Thread() {
        override fun run() {
            handleShutdown()
        }
    })
}
PREVIEWING: aliciascott/DOCS-9725-Cloudcraft