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

Added close handler on initial response for reactive SseEventSinkImpl #27707

Merged
merged 3 commits into from
Jan 11, 2024
Merged

Conversation

a29340
Copy link
Contributor

@a29340 a29340 commented Sep 4, 2022

This pr adds the necessary handling of the close event on SseEventSinkIpl. This addresses the scenario when it is the client that is closing the connection, and the sink was not registering itself as closed and most importantly the broadcaster was never notified of the sink close state.

@a29340 a29340 marked this pull request as ready for review September 4, 2022 09:16
@geoand geoand requested a review from FroMage September 5, 2022 06:10
@gsmet
Copy link
Member

gsmet commented Sep 19, 2022

@a29340 no need to rebase for now. @FroMage is on PTO for now. I will ping him when he's back so he can have a look to this one and the other SSE-related one.

@a29340
Copy link
Contributor Author

a29340 commented Sep 19, 2022

@a29340 no need to rebase for now. @FroMage is on PTO for now. I will ping him when he's back so he can have a look to this one and the other SSE-related one.

Oh ok, thank you!

@quarkus-bot

This comment has been minimized.

// // FIXME: notify of client closing
// System.err.println("Server connection closed");
// });
response.addCloseHandler(() -> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!

I checked out your branch and was testing the solution but I cannot understand why the close handler is only added when !response.headWritten(). Is this on purpose? For some reason, on my application this condition is always false and because of that the Close event firing never happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rjvq85, thanks for taking a look!
No, this was not placed there with a specific purpose, I just added the closeHandler where the previous comment was, but it makes sense to move it out of the if statement, to always add it to the response.

For me the condition !response.headWritten() was always true when testing, so it always execute the close handler, and I cannot reproduce a case where it gets evaluated to false... I used the reproducer provided by #23997, with the sse-server-reactive module and it worked fine, the issues on the reactive server seemed to be resolved.

I tested again after moving the changes out of the if statement, and I don't see any unexpected behaviour, so i guess it's fine like that?

}
response.addCloseHandler(() -> {
if (broadcaster != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should do this if block since it's a part of close() which you call anyway. Just call close() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works if the server is the one that closes the connection, the isClosed() returns false so the execution continues and broadcaster.fireClose(this) at line 59 is actually called. When the client is the one that drops, isClosed() method already returns true when the closeHandler is executed and the broadcaster is never notified.
At least this was the case using the reproducer here (using reactive-server)

@@ -72,7 +72,9 @@ public synchronized void close() {

synchronized void fireClose(SseEventSinkImpl sseEventSink) {
for (Consumer<SseEventSink> listener : onCloseListeners) {
listener.accept(sseEventSink);
if (sinks.remove(sseEventSink)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing the sink from the sinks? This will only match for the first listener, and subsequent listeners won't get called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get what do you mean, maybe I'm missing something. If we don't remove the sink (which is closing) from the broadcaster, that sink will continue to receive events even if it is closed, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have n listeners. On the first one, you remove the sink, then it's removed, and for all remaining listeners the sink is already removed so you don't notify them. That's a logic bug, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I see it, sorry! You're right, the removal should happen after all the listeners have been notified. I'll convert this pr to draft and work some more on it, since I'm already adding some tests.

@FroMage
Copy link
Member

FroMage commented Oct 5, 2022

Also, could you add a test for this fix?

@a29340 a29340 marked this pull request as draft October 10, 2022 14:31
@a29340 a29340 marked this pull request as ready for review October 28, 2022 10:16
@a29340
Copy link
Contributor Author

a29340 commented Nov 7, 2022

Hi @FroMage,

I added mockito as a dependency to do some tests, is it ok?

@FroMage
Copy link
Member

FroMage commented Nov 22, 2022

Thanks. I'm generally in favour of real HTTP tests rather than Mockito. Do you think you could write a RestAssured test that exhibits the issue you're fixing?

@quarkus-bot

This comment has been minimized.

@gsmet
Copy link
Member

gsmet commented Feb 1, 2023

@a29340 any chance you could rebase and finalize this?

I don't think we have fixed this issue already?

@gsmet gsmet added the triage/needs-feedback We are waiting for feedback. label Feb 1, 2023
@a29340
Copy link
Contributor Author

a29340 commented Feb 2, 2023

@gsmet Yes, I will try to finalize this in the next few days. Sorry, I didn´t have a chance to work on it recently

@gsmet gsmet added the triage/needs-rebase This PR needs to be rebased first because it has merge conflicts label Feb 4, 2023
@github-actions
Copy link

github-actions bot commented Feb 5, 2023

🎊 PR Preview 89fcdc9 has been successfully built and deployed to https://quarkus-io-pr-main-27707-preview.surge.sh/version/main/guides/

@quarkus-bot

This comment has been minimized.

@a29340 a29340 requested a review from FroMage February 5, 2023 09:06
Copy link
Member

@FroMage FroMage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, could you rebase on the latest main?

@a29340 a29340 requested a review from FroMage February 7, 2023 15:03
@a29340
Copy link
Contributor Author

a29340 commented Feb 14, 2023

I'm moving this to draft again, some tests are still unstable (often they fail) when run on the CI pipeline on the forked branch.

@a29340 a29340 marked this pull request as draft February 14, 2023 07:56
@a29340 a29340 marked this pull request as ready for review July 20, 2023 23:46
@a29340
Copy link
Contributor Author

a29340 commented Jul 20, 2023

Hi @FroMage, sorry for the long wait. I finally did fix up the tests covering this PR, and rebased on the latest main and squashed in a single commit

@a29340
Copy link
Contributor Author

a29340 commented Aug 8, 2023

Hi @gsmet @FroMage, is this PR still desired? Should I rebase again waiting for a review?

@FroMage
Copy link
Member

FroMage commented Aug 14, 2023

Hi @gsmet @FroMage, is this PR still desired? Should I rebase again waiting for a review?

Yes please. Sorry about the delays :(

Copy link
Member

@FroMage FroMage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review. This is OK, except the byte-buddy dependency that should be aligned with the Quarkus BOM.

independent-projects/resteasy-reactive/pom.xml Outdated Show resolved Hide resolved
@FroMage FroMage removed the triage/needs-rebase This PR needs to be rebased first because it has merge conflicts label Oct 9, 2023
@a29340 a29340 requested a review from FroMage October 9, 2023 11:37
@quarkus-bot

This comment has been minimized.

@FroMage
Copy link
Member

FroMage commented Oct 10, 2023

The first test failure seems related to this fix, no?

@a29340
Copy link
Contributor Author

a29340 commented Oct 10, 2023

The first test failure seems related to this fix, no?

Yes, looks like the test didn't detect the expected call on close method on the jdk 20 build. I'm trying to replicate on a local environment, but the tests pass every time. On the forked repo the jdk 11 build failed instead, with the same reason as this build, but passed on the 2nd attempt. Looks like it's not related to the specific java version.
I'll try to make this test more stable and run again a couple of times on the fork before requesting a build on here too

Copy link
Member

@FroMage FroMage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporarily asking for changes, just to make sure we don't merge this before @gsmet and @geoand answer the dependency question. But my review remains OK otherwise.

<type>pom</type>
<scope>import</scope>
</dependency>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to double check with @geoand and @gsmet that they're OK with this dependency being added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the BOM? Isn't adding the mockito test dependency enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually no, let me remove it. I'll try tu run the ci on the fork and then rebase

This comment has been minimized.

@a29340
Copy link
Contributor Author

a29340 commented Jan 5, 2024

The first test failure seems related to this fix, no?

Yes, looks like the test didn't detect the expected call on close method on the jdk 20 build. I'm trying to replicate on a local environment, but the tests pass every time. On the forked repo the jdk 11 build failed instead, with the same reason as this build, but passed on the 2nd attempt. Looks like it's not related to the specific java version. I'll try to make this test more stable and run again a couple of times on the fork before requesting a build on here too

Hi @FroMage, I tried to replicate the same test failure on the fork ci for a while now, but couldn't find what was the issue. As of now the tests pass every time I run the ci on the fork. Can you please take a look at the tests in SseServerTestCase to double check?

This comment has been minimized.

removed bom for mockito test dependency

sorted imports

removed colors and added logger instead of system out println

formatted

added more logs

added more logs

added more logs, updated bytebuddy

formatted

adding details to test log
Copy link

quarkus-bot bot commented Jan 10, 2024

✔️ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

You can consult the Develocity build scans.

@geoand geoand requested a review from FroMage January 10, 2024 17:25
Copy link
Member

@FroMage FroMage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, all tests pass, you figured it out?

@FroMage FroMage merged commit 20eeab4 into quarkusio:main Jan 11, 2024
43 checks passed
@quarkus-bot quarkus-bot bot added this to the 3.7 - main milestone Jan 11, 2024
@a29340
Copy link
Contributor Author

a29340 commented Jan 11, 2024

So, all tests pass, you figured it out?

@FroMage No, the only thing I did was rebase and add logs, but for the last month or so I couldn't replicate the test failure. As of now the tests pass reliably, but I really don't know why they didn't before

@FroMage
Copy link
Member

FroMage commented Jan 12, 2024

Well, we'll see if this occurs again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants