Reusable Python context does not adapt injected Publishers for await
Expected behavior
An async def Python controller can directly await a Publisher returned by a constructor-injected Java bean, including when Micronaut runs with a reusable GraalPy context.
This is the documented async/await behavior: Java Publisher, Mono, and Flux values returned by injected members are adapted to Python awaitables.
Actual behavior
With PythonContextRuntime.setReuseContext(true), constructor arguments are passed into the primary Python object unchanged. The injected Java service therefore remains a raw foreign object, and its reactive method returns a raw Reactor value:
TypeError: object Java_reactor.core.publisher.MonoDeferContextual_generated
can't be used in 'await' expressionPooled event-loop contexts do not have this problem because newEventLoopInstance passes replayed constructor arguments through PythonCoercion.asyncConstructorArgument.
Reproducer
The failing Core regression uses a Java service whose value() method returns Mono<String>:
@Controller("/reuse-async")
class AsyncController:
def __init__(self, service: ContextualService):
self.service = service
@Get
async def value(self) -> str:
return await self.service.value()After enabling context reuse, an HTTP request to /reuse-async returns 500 with the TypeError above. The existing synchronous reuse-context controller test continues to pass.
Suggested fix
When creating a Python class with coroutine methods in reuse-context mode, adapt its constructor arguments with PythonCoercion.asyncConstructorArgument before calling the Python constructor. This reuses the existing async-member facade and keeps the behavior consistent with event-loop-context constructor replay.
Version
Micronaut Core 5.2.x / 5.2.5, GraalPy 25.4.4.1.1.
Source: micronaut-projects/micronaut-core