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

fix: complete client websocket future on open #20587

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

mcollovati
Copy link
Collaborator

Description

Completing the websocket future in onOpen event prevents the connection to hang indefintely when application run on low resources.

Fixes #20586

Type of change

  • Bugfix
  • Feature

Checklist

  • I have read the contribution guide: https://vaadin.com/docs/latest/guide/contributing/overview/
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

Additional for Feature type of change

  • Enhancement / new feature was discussed in a corresponding GitHub issue and Acceptance Criteria were created.

Copy link

github-actions bot commented Dec 1, 2024

Test Results

1 158 files  ±0  1 158 suites  ±0   1h 35m 26s ⏱️ + 3m 55s
7 520 tests ±0  7 467 ✅ ±0  53 💤 ±0  0 ❌ ±0 
7 886 runs   - 4  7 824 ✅  - 3  62 💤  - 1  0 ❌ ±0 

Results for commit 91d5db9. ± Comparison against base commit 5b95429.

♻️ This comment has been updated with latest results.

@mshabarov mshabarov self-requested a review December 2, 2024 12:34
mshabarov
mshabarov previously approved these changes Dec 10, 2024
Copy link
Contributor

@mshabarov mshabarov left a comment

Choose a reason for hiding this comment

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

Looks good to me.
Tested with the provided reproducible steps and noticed the cases when onOpen happens before whenCompleted, with this fix the connection and message sending seems to run without any deadlocks or errors, see my log below:

2024-12-10 10:56:58,385 DEBUG [com.vaa.bas.dev.vit.ViteWebsocketConnection] (Thread-171) Connected using the vite-hmr protocol
2024-12-10 10:56:58,387 DEBUG [com.vaa.bas.dev.vit.ViteWebsocketProxy] (vert.x-eventloop-thread-0) Sent message to Vite: {"type":"custom","event":"vite-plugin-checker","data":{"event":"runtime-loaded"}}
2024-12-10 10:56:58,387 DEBUG [com.vaa.bas.dev.vit.ViteWebsocketConnection] (Thread-171) Connection to ws://127.0.0.1:51220/VAADIN/ using the vite-hmr protocol established
2024-12-10 10:56:58,387 DEBUG [com.vaa.bas.dev.vit.ViteWebsocketConnection] (HttpClient-24-Worker-0) Message from Vite: {"type":"connected"}
2024-12-10 10:56:58,388 DEBUG [com.vaa.bas.dev.vit.ViteWebsocketProxy] (HttpClient-24-Worker-0) Message sent to browser: {"type":"connected"}

@@ -72,15 +72,21 @@ public ViteWebsocketConnection(int port, String path, String subProtocol,
this.onClose = onClose;
String wsHost = ViteHandler.DEV_SERVER_HOST.replace("http://", "ws://");
URI uri = URI.create(wsHost + ":" + port + path);
clientWebsocket = HttpClient.newHttpClient().newWebSocketBuilder()
clientWebsocket = new CompletableFuture<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: clientWebsocket could be instantiated in place when declared, IMO it's a little bit clearer for code reading.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@mshabarov
Copy link
Contributor

One more remark, not blocking though, can we replace clientWebsocket.get() calls with the get() variant with a timeout?

@mcollovati
Copy link
Collaborator Author

One more remark, not blocking though, can we replace clientWebsocket.get() calls with the get() variant with a timeout?

Probably yes, but we should decide what timeouts to apply.
We have two blocking get() operations: one on the connection future, and the other one on the send call.

Completing the websocket future in onOpen event prevents the connection to hang
indefintely when application run on low resources.

Fixes #20586
@mcollovati mcollovati force-pushed the issues/20586-vitewebsocketconnect-locks branch from 8a95b97 to 91d5db9 Compare December 10, 2024 11:54
@mcollovati
Copy link
Collaborator Author

I wonder if we can proceed without timeouts, and change it if issues will be reported.

@mshabarov
Copy link
Contributor

Sure, it was just a though for future.

@mshabarov mshabarov enabled auto-merge (squash) December 10, 2024 11:58
Copy link

sonarcloud bot commented Dec 10, 2024

@mshabarov mshabarov merged commit 39e640c into main Dec 10, 2024
25 of 26 checks passed
@mshabarov mshabarov deleted the issues/20586-vitewebsocketconnect-locks branch December 10, 2024 12:11
vaadin-bot pushed a commit that referenced this pull request Dec 10, 2024
* fix: complete client websocket future on open

Completing the websocket future in onOpen event prevents the connection to hang
indefintely when application run on low resources.

Fixes #20586

* apply review suggestions
vaadin-bot added a commit that referenced this pull request Dec 10, 2024
* fix: complete client websocket future on open

Completing the websocket future in onOpen event prevents the connection to hang
indefintely when application run on low resources.

Fixes #20586

* apply review suggestions

Co-authored-by: Marco Collovati <[email protected]>
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.

ViteWebsocketConnection hangs when running with low resource
3 participants