Skip to content

Commit 5b31592

Browse files
committed
fix: possible issue with concurrency for activation dynamic event source registration
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 51a8fe5 commit 5b31592

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractEventSourceHolderDependentResource.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ protected AbstractEventSourceHolderDependentResource(Class<R> resourceType) {
3434
this.resourceType = resourceType;
3535
}
3636

37-
public Optional<T> eventSource(EventSourceContext<P> context) {
37+
/**
38+
* Method is synchronized since when used in case of dynamic registration (thus for activation
39+
* conditions) can be called concurrently to create the target event source. In that case only one
40+
* instance should be created, since this also sets the event source, and dynamic registration
41+
* will just start one with the same name. So if this would not be synchronized it could happen
42+
* that multiple event sources would be created and only one started and registered. Note that
43+
* this method does not start the event source, so no blocking IO is involved.
44+
*/
45+
public synchronized Optional<T> eventSource(EventSourceContext<P> context) {
3846
// some sub-classes (e.g. KubernetesDependentResource) can have their event source created
3947
// before this method is called in the managed case, so only create the event source if it
4048
// hasn't already been set.
4149
// The filters are applied automatically only if event source is created automatically. So if an
4250
// event source
4351
// is shared between dependent resources this does not override the existing filters.
52+
4453
if (eventSource == null && eventSourceNameToUse == null) {
4554
setEventSource(createEventSource(context));
4655
applyFilters();
@@ -83,9 +92,10 @@ public Class<R> resourceType() {
8392

8493
protected abstract T createEventSource(EventSourceContext<P> context);
8594

86-
protected void setEventSource(T eventSource) {
95+
public void setEventSource(T eventSource) {
8796
isCacheFillerEventSource = eventSource instanceof RecentOperationCacheFiller;
8897
this.eventSource = eventSource;
98+
applyFilters();
8999
}
90100

91101
protected void applyFilters() {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowReconcileExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ private <R> void registerOrDeregisterEventSourceBasedOnActivation(boolean activa
8888
var es = eventSource.orElseThrow();
8989
context.eventSourceRetriever()
9090
.dynamicallyRegisterEventSource(dependentResourceNode.getName(), es);
91-
9291
} else {
9392
context.eventSourceRetriever()
9493
.dynamicallyDeRegisterEventSource(dependentResourceNode.getName());

0 commit comments

Comments
 (0)