micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.05k stars 1.06k forks source link

IllegalStateException: No lock present for object #3334

Closed theHacker closed 2 years ago

theHacker commented 4 years ago

Task List

Steps to Reproduce

  1. Open the demo project
  2. Run the MyServiceTest inside your IDE to see the full Exception.

Expected Behaviour

Test finishes ordinarily.

Actual Behaviour

Micronaut will throw an exception

No lock present for object: MyClient(#1)
java.lang.IllegalStateException: No lock present for object: MyClient(#1)
    at io.micronaut.runtime.context.scope.refresh.RefreshScope.getLock(RefreshScope.java:158)
    at io.micronaut.runtime.context.scope.refresh.RefreshInterceptor.intercept(RefreshInterceptor.java:46)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
    at micronaut.issue.$MyServiceTest$MyClientMock0Definition$Intercepted.getSomeFoos(Unknown Source)
    at micronaut.issue.MyService.getFoos(MyService.kt:9)
    at micronaut.issue.MyServiceTest$1.invokeSuspend(MyServiceTest.kt:24)
    at micronaut.issue.MyServiceTest$1.invoke(MyServiceTest.kt)
    at io.kotlintest.runner.jvm.TestCaseExecutor$executeTest$supervisorJob$1$invokeSuspend$$inlined$map$lambda$1.invokeSuspend(TestCaseExecutor.kt:121)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

Environment Information

Example Application

Additional information

You can see the problem inside the debugger, when you hold in MyService. Evaluate the commented out line, manually resolving the proxy, and it works. But this is in production code, so this cannot be the way.

jameskleeh commented 4 years ago

@theHacker Can you verify the issue persists on 1.3.5?

theHacker commented 4 years ago

Yes, the issue exists also in 1.3.3 and 1.3.5. I even tried latest dev-version 2.0.0.M3. Issue remains.

antondelpiero commented 4 years ago

just want to say that this problem also persist in 2.0.0.RC1

graemerocher commented 4 years ago

Seems to be some weird issue where the kotlin mocking library is introducing coroutines into the picture which introduces a concurrency issue where the lock is not visible from the couroutine thread. Why a mocking library needs to introduce concurrency and coroutines a Kotlin expert would need to explain.

ikovalyov commented 4 years ago

I had this issue with mockk tests, when mockk was setting stubs in the test body, not during initialisation. Happened only once in few years though. Hope it'll help.

mparlee commented 4 years ago

Anyone know of a workaround for this?

ikovalyov commented 4 years ago

Anyone know of a workaround for this?

I stopped changing mock answers in the test body and it helped.

mparlee commented 4 years ago

Thanks, @ikovalyov. I ended up switching from mockk to kluent mocks for the affected tests. I was already using kluent for assertions so no new library or wholesale change required.

beedub commented 3 years ago

I'm actually seeing this with junit5 and mockk. Any workarounds?

graemerocher commented 2 years ago

code under question re-written in 3.x