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

@Blocking does not work as expected with @PostConstruct #30675

Closed
jie-huang opened this issue Jan 28, 2023 · 3 comments
Closed

@Blocking does not work as expected with @PostConstruct #30675

jie-huang opened this issue Jan 28, 2023 · 3 comments
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working

Comments

@jie-huang
Copy link

jie-huang commented Jan 28, 2023

Describe the bug

When using @Blocking on @PostConstruct method, it is expected that should work.
But, it does not work when the class is used in a route of Uni<>.

@ApplicationScoped
public class BlockingInitResource {
  static class Data {
    public Data(String s) {
      this.s = s;
    }

    String s;
  }

  Uni<Data> initData() {
    return Uni.createFrom().item(new Data("hello"));
  }

  Data data = null;

  public Data getData() {
    return data;
  }

  @PostConstruct
  @Blocking
  public void init() {
    initData().onItem().transform(new Function<Data, Void>() {
      @Override
      public Void apply(Data d) {
        data = d;
        return null;
      }
    }).await().indefinitely();
  }
}

Expected behavior

Quarkus allow the blocking operation and can handle it smartly.

Actual behavior

Request failed: java.lang.IllegalStateException: The current thread cannot be blocked: vert.x-eventloop-thread-0

How to Reproduce?

https://github.com/jie-huang/quarkus-blocking

Run quarkus dev, then run curl http://localhost:8080/hello2

If you run curl http://localhost:8080/hello1 first, it works.
If you run curl http://localhost:8080/hello2 first, it fails.

Output of uname -a or ver

Darwin sd-lmc-1a8902 21.6.0 Darwin Kernel Version 21.6.0: Mon Dec 19 20:46:01 PST 2022; root:xnu-8020.240.18~2/RELEASE_ARM64_T8101 arm64

Output of java -version

openjdk 18 2022-03-22 OpenJDK Runtime Environment (build 18+36-2087) OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.12.0.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Gradle 7.5

Additional information

No response

@jie-huang jie-huang added the kind/bug Something isn't working label Jan 28, 2023
@geoand geoand added area/arc Issue related to ARC (dependency injection) and removed triage/needs-triage labels Jan 30, 2023
@geoand
Copy link
Contributor

geoand commented Jan 30, 2023

I would personally close this as won't fix as the @Blocking annotation is not a magic tool that can be used everywhere, but I'll let @cescoffier and @mkouba decide

@mkouba
Copy link
Contributor

mkouba commented Jan 30, 2023

A @PostConstruct callback is always invoked on the thread where the bean instance is created (BlockingInitResource in this particular case is created on an event loop thread). And it may not return an asynchronous type, such as Uni. In fact, the CDI "bean construction API" is not asynchronous and so we may not easily integrate it with reactive libraries.

If you really need to do something similar you would need to @Inject Vertx and offload the logic manually via Vertx.executeBlocking(), but keep in mind that you might not block inside the callback anyway (no Uni.await().indefinitely() etc.). In other words, you'd need some kind of synchronization to ensure the data is initialized and ready for use.

@mkouba
Copy link
Contributor

mkouba commented Jan 30, 2023

#27591 is related.

@mkouba mkouba closed this as completed Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants