-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Java] Safely call onError on Subjects #31779
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1413,14 +1413,14 @@ public void handleHandshake(ByteBuffer payload) { | |
| handshakeResponse = HandshakeProtocol.parseHandshakeResponse(handshakeResponseString); | ||
| } catch (RuntimeException ex) { | ||
| RuntimeException exception = new RuntimeException("An invalid handshake response was received from the server.", ex); | ||
| handshakeResponseSubject.onError(exception); | ||
| errorHandshake(exception); | ||
| throw exception; | ||
| } | ||
| if (handshakeResponse.getHandshakeError() != null) { | ||
| String errorMessage = "Error in handshake " + handshakeResponse.getHandshakeError(); | ||
| logger.error(errorMessage); | ||
| RuntimeException exception = new RuntimeException(errorMessage); | ||
| handshakeResponseSubject.onError(exception); | ||
| errorHandshake(exception); | ||
| throw exception; | ||
| } | ||
| handshakeReceived = true; | ||
|
|
@@ -1431,12 +1431,7 @@ public void handleHandshake(ByteBuffer payload) { | |
| public void timeoutHandshakeResponse(long timeout, TimeUnit unit) { | ||
| handshakeTimeout = Executors.newSingleThreadScheduledExecutor(); | ||
| handshakeTimeout.schedule(() -> { | ||
| // If onError is called on a completed subject the global error handler is called | ||
| if (!(handshakeResponseSubject.hasComplete() || handshakeResponseSubject.hasThrowable())) | ||
| { | ||
| handshakeResponseSubject.onError( | ||
| new TimeoutException("Timed out waiting for the server to respond to the handshake message.")); | ||
| } | ||
| errorHandshake(new TimeoutException("Timed out waiting for the server to respond to the handshake message.")); | ||
| }, timeout, unit); | ||
| } | ||
|
|
||
|
|
@@ -1476,6 +1471,18 @@ public List<Type> getParameterTypes(String methodName) { | |
|
|
||
| return handlers.get(0).getTypes(); | ||
| } | ||
|
|
||
| private void errorHandshake(Exception error) { | ||
| lock.lock(); | ||
| try { | ||
| // If onError is called on a completed subject the global error handler is called | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we could hook the global error handler with our tests and fail if it ever gets called? Would that have been able to catch this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could use something like this:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the pointer, adapted something similar for junit 5 |
||
| if (!(handshakeResponseSubject.hasComplete() || handshakeResponseSubject.hasThrowable())) { | ||
| handshakeResponseSubject.onError(error); | ||
| } | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // We don't have reconnect yet, but this helps align the Java client with the .NET client | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| package com.microsoft.signalr; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
|
|
||
| import org.junit.jupiter.api.extension.AfterAllCallback; | ||
| import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
|
||
| import io.reactivex.rxjava3.plugins.RxJavaPlugins; | ||
|
|
||
| // Use by adding "@ExtendWith({RxJavaUnhandledExceptionsExtensions.class})" to a test class | ||
| class RxJavaUnhandledExceptionsExtensions implements BeforeAllCallback, AfterAllCallback { | ||
| private final BlockingQueue<Throwable> errors = new LinkedBlockingQueue<Throwable>(); | ||
|
|
||
| @Override | ||
| public void beforeAll(final ExtensionContext context) { | ||
| RxJavaPlugins.setErrorHandler(error -> { | ||
| errors.put(error); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void afterAll(final ExtensionContext context) { | ||
| if (errors.size() != 0) { | ||
| String RxErrors = ""; | ||
| for (final Throwable throwable : errors) { | ||
| StringWriter stringWriter = new StringWriter(); | ||
| PrintWriter printWriter = new PrintWriter(stringWriter); | ||
| throwable.printStackTrace(printWriter); | ||
| RxErrors += String.format("%s\n", stringWriter.toString()); | ||
| } | ||
| throw new RuntimeException(RxErrors); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why this uses an executor to implement the timeout instead of using RxJava's timeout operator?
connectionState.transport.send(handshake).timeout(..., ...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably implemented before RxJava was used.
Keeping it as-is so it's easier to backport to 5.0