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

@Observes @BeforeDestroyed(?Scoped.class) does not work in the same bean and the same scope #46108

Closed
tandraschko opened this issue Feb 5, 2025 · 11 comments · Fixed by #46125
Assignees
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Milestone

Comments

@tandraschko
Copy link
Contributor

tandraschko commented Feb 5, 2025

Describe the bug

It seems when its observed by a application scoped bean, the @Observes works fine like here:

https://github.com/manovotn/quarkus/blob/ed08d10bcf5d6635a76574cf149c5e2a42d45ae9/extensions/undertow/deployment/src/test/java/io/quarkus/undertow/test/sessioncontext/ObservingBean.java

However, when its observed from a bean inside the same scope, it doesnt work:

@RequestScoped
public class Obsi {
    public void destroy(@Observes(notifyObserver = Reception.IF_EXISTS) @BeforeDestroyed(RequestScoped.class) Object event) {
        System.err.println("@BeforeDestroyed");
    }
}

Doesnt matter if Session or RequestScoped

Expected behavior

should be called

Actual behavior

is not called

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

No response

Quarkus version or git rev

No response

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

No response

Additional information

No response

@tandraschko tandraschko added the kind/bug Something isn't working label Feb 5, 2025
@michalvavrik michalvavrik added area/arc Issue related to ARC (dependency injection) and removed triage/needs-triage labels Feb 5, 2025
Copy link

quarkus-bot bot commented Feb 5, 2025

/cc @Ladicek (arc), @manovotn (arc), @mkouba (arc)

@manovotn
Copy link
Contributor

manovotn commented Feb 5, 2025

@tandraschko you are using notifyObserver = Reception.IF_EXISTS, are you sure the observing bean instance exists when the context destruction happens?

@tandraschko
Copy link
Contributor Author

Yep, but placed in a abstract superclass
I can provide a reproducer, too

@manovotn
Copy link
Contributor

manovotn commented Feb 5, 2025

Ok, this is easy to reproduce and the reception setup plays no part in it.

The culprit seems to be that we first invalidate context state and only then proceed to notify observers.
This is the code - https://github.com/quarkusio/quarkus/blob/main/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/CurrentManagedContext.java#L174-L175

The problem is, this code seems very deliberate, probably due to context propagation scenarios.
I need to take a closer look at it tomorrow...

@manovotn manovotn self-assigned this Feb 5, 2025
@manovotn
Copy link
Contributor

manovotn commented Feb 6, 2025

FTR, there is also a similar issue with @Initialized event where we fire the event from within ManagedContext#initializeState at which point the given context still appears to be inactive so any observer belonging to that context won't trigger.

@tandraschko tandraschko changed the title @Observes @BeforeDestroyed(?Scoped.class) does not work if the same bean exists in this scope? @Observes @BeforeDestroyed(?Scoped.class) does not work in the same bean and the same scope Feb 6, 2025
@melloware
Copy link
Contributor

@manovotn so confirmed bug?

@manovotn
Copy link
Contributor

manovotn commented Feb 6, 2025

@manovotn so confirmed bug?

Yes, I think this should work.

@quarkus-bot quarkus-bot bot added this to the 3.19 - main milestone Feb 7, 2025
@tandraschko
Copy link
Contributor Author

Thanks man! :)

@gsmet gsmet modified the milestones: 3.19 - main, 3.18.3 Feb 11, 2025
@tandraschko
Copy link
Contributor Author

@manovotn In which version is it available now?
Tried 3.19.0.CR1 but i still get similar errors

@manovotn
Copy link
Contributor

@manovotn In which version is it available now? Tried 3.19.0.CR1 but i still get similar errors

3.19.0.CR1 should have the commit (and it was backported to 3.18.3 as well).
The PR also had a test attached. If you are still seeing the problem, I'd need to see a reproducer because the setup in the test is now working for sure.
Feel free to create a follow up issue with it - mention me there and I can have a look.

@manovotn
Copy link
Contributor

@tandraschko FTR, I found out this is still an issue with session context from Undertow extension - #46363

This is because of the context implementation (at least I think so). I am looking into how to fix that properly.
Though I was unable to determine any further issues with req. context - if that is an issue for you, I am going to need more details.

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
5 participants