-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
1.x: Change the workers to capture the stack trace #3937
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/rx/internal/schedulers/SchedulerContextException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx.internal.schedulers; | ||
|
||
/** | ||
* Used only for providing context around where work was scheduled should an error occur in a different thread. | ||
*/ | ||
public class SchedulerContextException extends Exception { | ||
/** | ||
* Constant to use when disabled | ||
*/ | ||
private static final Throwable CONTEXT_MISSING = new SchedulerContextException("Missing context. Enable by setting the system property \"rxjava.captureSchedulerContext=true\""); | ||
|
||
static { | ||
CONTEXT_MISSING.setStackTrace(new StackTraceElement[0]); | ||
} | ||
|
||
/** | ||
* @return a {@link Throwable} that captures the stack trace or a {@link Throwable} that documents how to enable the feature if needed. | ||
*/ | ||
public static Throwable create() { | ||
String def = "false"; | ||
String setTo = System.getProperty("rxjava.captureSchedulerContext", def); | ||
return setTo != def && "true".equals(setTo) ? new SchedulerContextException("Asynchronous work scheduled at") : CONTEXT_MISSING; | ||
} | ||
|
||
private SchedulerContextException(String message) { | ||
super(message); | ||
} | ||
|
||
private static final long serialVersionUID = 1L; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ | |
import rx.functions.Action0; | ||
import rx.functions.Action1; | ||
import rx.functions.Func1; | ||
import rx.plugins.RxJavaErrorHandler; | ||
import rx.plugins.RxJavaPlugins; | ||
|
||
/** | ||
* Base tests for all schedulers including Immediate/Current. | ||
|
@@ -502,4 +504,54 @@ public void onNext(T args) { | |
|
||
} | ||
|
||
@Test | ||
public final void testStackTraceAcrossThreads() throws Throwable { | ||
final AtomicReference<Throwable> exceptionRef = new AtomicReference<Throwable>(); | ||
final CountDownLatch done = new CountDownLatch(1); | ||
System.setProperty("rxjava.captureSchedulerContext", "true"); | ||
|
||
try { | ||
|
||
RxJavaPlugins.getInstance().reset(); | ||
RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() { | ||
@Override | ||
public void handleError(Throwable e) { | ||
exceptionRef.set(e); | ||
done.countDown(); | ||
} | ||
}); | ||
|
||
try { | ||
getScheduler().createWorker().schedule(new Action0() { | ||
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. This leaks the worker. |
||
@Override | ||
public void call() { | ||
throw new RuntimeException(); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
exceptionRef.set(e); | ||
done.countDown(); | ||
} | ||
|
||
done.await(); | ||
|
||
Throwable exception = exceptionRef.get(); | ||
Throwable e = exception; | ||
while (e.getCause() != null) { | ||
e = e.getCause(); | ||
} | ||
|
||
StackTraceElement[] st = e.getStackTrace(); | ||
for (StackTraceElement stackTraceElement : st) { | ||
if (stackTraceElement.getMethodName().equals("testStackTraceAcrossThreads")) { | ||
// pass we found this class in the stack trace. | ||
return; | ||
} | ||
} | ||
|
||
throw exception; | ||
} finally { | ||
System.setProperty("rxjava.captureSchedulerContext", "false"); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 not experienced with it, but
AccessController.doPrivileged
could be used for defending the setting of the volatile boolean field.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.
Is this about setting up a public API to toggle the features? I am considering adding the method to the plugin class or the execution hooks.
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.
doPreveleged
only penalizes the mode change and not thiscreate()
method.