Skip to content

NPE occurs when AOP is applied to a method that returns CompletableFuture #5741

@laststem

Description

@laststem

Describe the bug
Returning null from a method that returns CompletableFuture will result in an NPE in AOP.

@Timed
fun doSomething(): CompletableFuture<Int>? {
    return if (condition) {
        CompletableFuture.completedFuture(1)
    } else { 
        null
    }
}

Because AOP considers it to be non-null if it is of type CompletionStage. (code)

As a result, the original method did not cause an NPE, but the AOP caused an NPE, so the calling thread was affected.

If you agree, please let me know if I can create a PR.

Environment

  • Micrometer version 1.14.1
  • OS: macOS
  • Java version: 17

To Reproduce

@SpringBootApplication
class SpringPlaygroundApplication(
    private val testComponent: TestComponent
) : ApplicationRunner {
    override fun run(args: ApplicationArguments?) {
        val result = testComponent.test()
        println("result: $result") // No output is produced as NPE is thrown.
        Thread.sleep(10000)
    }
}

@Configuration
class TestCfg(
    private val meterRegistry: MeterRegistry,
) {
    @Bean
    fun timedAop() = TimedAspect(meterRegistry)
}

fun main(args: Array<String>) {
    runApplication<SpringPlaygroundApplication>(*args)
}

@Component
class TestComponent {
    @Timed
    fun test(): CompletableFuture<Int>? {
         return null
    }
}

Expected behavior
AOP needs to be aware that values ​​of type CompletionStage can be null.

Also, exceptions raised in AOP should not affect the original method.

Additional context
Add any other context about the problem here, e.g. related issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA general bugmodule: micrometer-coreAn issue that is related to our core module

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions