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

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

Closed
laststem opened this issue Dec 12, 2024 · 1 comment · Fixed by #5742
Closed

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

laststem opened this issue Dec 12, 2024 · 1 comment · Fixed by #5742
Labels
bug A general bug module: micrometer-core An issue that is related to our core module
Milestone

Comments

@laststem
Copy link
Contributor

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.

@shakuzen shakuzen added bug A general bug module: micrometer-core An issue that is related to our core module and removed waiting-for-triage labels Dec 12, 2024
@shakuzen shakuzen added this to the 1.13.x milestone Dec 12, 2024
@shakuzen
Copy link
Member

Thanks for the detailed report. Indeed it sounds like a bug. We'd be happy to review a pull request for a fix. It looks like CountedAspect and ObservedAspect also suffer from the same bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A general bug module: micrometer-core An issue that is related to our core module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants