-
Notifications
You must be signed in to change notification settings - Fork 42
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: Move event emitting off the main thread to avoid deadlocks #1314
Merged
toddbaert
merged 6 commits into
open-feature:main
from
sideshowcoder:sideshowcoder/issue/1299
Feb 13, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
558c615
Move event emitting off the main thread to avoid deadlocks
2825dda
Test fixes
sideshowcoder f60e7d8
Add timeout to EventProviderTest
sideshowcoder eb10367
Don't reuse the JVM Process
sideshowcoder 8363aba
Merge branch 'main' into sideshowcoder/issue/1299
beeme1mr 69e056f
Merge branch 'main' into sideshowcoder/issue/1299
toddbaert 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
103 changes: 103 additions & 0 deletions
103
src/test/java/dev/openfeature/sdk/testutils/TestStackedEmitCallsProvider.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,103 @@ | ||
package dev.openfeature.sdk.testutils; | ||
|
||
import dev.openfeature.sdk.EvaluationContext; | ||
import dev.openfeature.sdk.EventProvider; | ||
import dev.openfeature.sdk.Metadata; | ||
import dev.openfeature.sdk.ProviderEvaluation; | ||
import dev.openfeature.sdk.ProviderEvent; | ||
import dev.openfeature.sdk.ProviderEventDetails; | ||
import dev.openfeature.sdk.Value; | ||
import java.util.function.Consumer; | ||
|
||
public class TestStackedEmitCallsProvider extends EventProvider { | ||
private final NestedBlockingEmitter nestedBlockingEmitter = new NestedBlockingEmitter(this::onProviderEvent); | ||
|
||
@Override | ||
public Metadata getMetadata() { | ||
return () -> getClass().getSimpleName(); | ||
} | ||
|
||
@Override | ||
public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
synchronized (nestedBlockingEmitter) { | ||
nestedBlockingEmitter.init(); | ||
while (!nestedBlockingEmitter.isReady()) { | ||
try { | ||
nestedBlockingEmitter.wait(); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void onProviderEvent(ProviderEvent providerEvent) { | ||
synchronized (nestedBlockingEmitter) { | ||
if (providerEvent == ProviderEvent.PROVIDER_READY) { | ||
nestedBlockingEmitter.setReady(); | ||
/* | ||
* This line deadlocked in the original implementation without the emitterExecutor see | ||
* https://github.com/open-feature/java-sdk/issues/1299 | ||
*/ | ||
emitProviderReady(ProviderEventDetails.builder().build()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { | ||
throw new UnsupportedOperationException("Unimplemented method 'getBooleanEvaluation'"); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { | ||
throw new UnsupportedOperationException("Unimplemented method 'getStringEvaluation'"); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { | ||
throw new UnsupportedOperationException("Unimplemented method 'getIntegerEvaluation'"); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { | ||
throw new UnsupportedOperationException("Unimplemented method 'getDoubleEvaluation'"); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { | ||
throw new UnsupportedOperationException("Unimplemented method 'getObjectEvaluation'"); | ||
} | ||
|
||
static class NestedBlockingEmitter { | ||
|
||
private final Consumer<ProviderEvent> emitProviderEvent; | ||
private volatile boolean isReady; | ||
|
||
public NestedBlockingEmitter(Consumer<ProviderEvent> emitProviderEvent) { | ||
this.emitProviderEvent = emitProviderEvent; | ||
} | ||
|
||
public void init() { | ||
// run init outside monitored thread | ||
new Thread(() -> { | ||
try { | ||
Thread.sleep(500); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
emitProviderEvent.accept(ProviderEvent.PROVIDER_READY); | ||
}) | ||
.start(); | ||
} | ||
|
||
public boolean isReady() { | ||
return isReady; | ||
} | ||
|
||
public synchronized void setReady() { | ||
isReady = true; | ||
this.notifyAll(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Ok this is obviously not a solution, but I want to know what the reason for the test failures are, are they actual issues ore is it cross test interactions? It seems we are suffering from global setup issues due to the Singleton that is OpenFeatureApi. @toddbaert any suggestion how to get around this?
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.
Which tests are failing? Today we merged something which increases the stability of the spectests
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 think there's a test annotation to run some tests in isolation, which may be the best solution here. If you cannot narrow down the exact suite I'd support you adding forkCount 1 for now, and I can look into it later (or somebody else).
If you want to keep digging at it yourself, you're more than welcome, but it's not 100% related to your improvement, so I don't want to force you to do it if you are sure it's a bad test interaction.
Up to you!
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.
@ Execution(SAME_THREAD) annotation
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.
Ah thanks! Yes it is always the same tests so I'm gonna annotate them and remove this global one. And then this should be done.
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.
Ok so I tried this out locally and got some fixes but still have some failing tests, with annotations it is not possible to get the same isolation as with no reuse for forks. I am gonna spent more time tomorrow on this and if I can't resolve it I would suggest to move this to a followup PR?
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 in favor of merging as is and following up with improvements in a separate PR. @toddbaert @liran2000 any concerns about that?
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.
No objections.