Skip to content

Commit 01b307e

Browse files
authored
fix: avoid deadlock when changeNamespaces joins a populated shared informer (#3655)
Fabric8 replays an already populated informer's cache to a newly added handler synchronously, on the registering thread. changeNamespaces registered the handler on an informer starter thread while the event source held its monitor waiting for that thread, so the replay blocked on the monitor and deadlocked. Register the handlers on the calling thread and only start the informers in parallel. Fixes #3650
1 parent cdf56f2 commit 01b307e

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

‎operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerManager.java‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,31 @@ public void changeNamespaces(Set<String> namespaces) {
120120
return;
121121
}
122122

123+
// Registering the handler on an already populated shared informer replays its cache to the
124+
// handler synchronously, on the registering thread. The event source holds its monitor while
125+
// waiting for the informers below to start, so the handler has to be registered on this thread:
126+
// a replay from an informer starter thread would block on that monitor and deadlock.
127+
var newSources = newNamespaces.stream().map(this::getEventSourceForNamespace).toList();
123128
controllerConfiguration
124129
.getConfigurationService()
125130
.getExecutorServiceManager()
126131
.boundedExecuteAndWaitForAllToComplete(
127-
newNamespaces.stream(),
128-
ns -> {
129-
final var source = getEventSourceForNamespace(ns);
132+
newSources.stream(),
133+
source -> {
130134
// block until the informer's cache is synced (or the sync timeout elapses)
131135
start(source);
132-
log.debug("Registered new {} -> {} for namespace: {}", this, source, ns);
136+
log.debug(
137+
"Registered new {} -> {} for namespace: {}",
138+
this,
139+
source,
140+
source.getTargetNamespace());
133141
return null;
134142
},
135-
ns -> "InformerStarter-" + ns + "-" + configuration.getResourceClass().getSimpleName());
143+
source ->
144+
"InformerStarter-"
145+
+ source.getTargetNamespace()
146+
+ "-"
147+
+ configuration.getResourceClass().getSimpleName());
136148
}
137149

138150
private void start(InformerWrapper<R> informerWrapper) {

‎operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/ManagedInformerEventSourceNamespaceChangeTest.java‎

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
*/
1616
package io.javaoperatorsdk.operator.processing.event.source.informer;
1717

18+
import java.time.Duration;
1819
import java.util.Set;
1920

2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
2223

24+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
2325
import io.fabric8.kubernetes.api.model.apps.Deployment;
2426
import io.fabric8.kubernetes.client.KubernetesClient;
27+
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
2528
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
2629
import io.fabric8.kubernetes.client.informers.cache.Cache;
2730
import io.javaoperatorsdk.operator.MockKubernetesClient;
@@ -37,8 +40,11 @@
3740
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
3841

3942
import static org.assertj.core.api.Assertions.assertThat;
43+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
4044
import static org.mockito.ArgumentMatchers.any;
45+
import static org.mockito.Mockito.doAnswer;
4146
import static org.mockito.Mockito.mock;
47+
import static org.mockito.Mockito.verify;
4248
import static org.mockito.Mockito.when;
4349

4450
/**
@@ -100,22 +106,71 @@ void namespaceChangeIsStillAppliedWhileRunning() {
100106
assertThat(pool.numberOfInformersForResource(Deployment.class)).isZero();
101107
}
102108

109+
/**
110+
* Fabric8 replays the cache of an already populated informer to a newly added handler
111+
* synchronously, on the thread registering it. That replay enters the event source's monitor,
112+
* which the namespace change holds while waiting for the informers to start, so the handler must
113+
* be registered on the thread holding the monitor rather than on an informer starter thread.
114+
*/
115+
@Test
116+
void namespaceChangeJoiningAPopulatedInformerDoesNotDeadlock() {
117+
var eventHandler = mock(EventHandler.class);
118+
var eventSource = buildEventSource(eventHandler);
119+
eventSource.start();
120+
replayCacheOnHandlerRegistration();
121+
122+
assertTimeoutPreemptively(
123+
Duration.ofSeconds(10), () -> eventSource.changeNamespaces(Set.of("ns1", "ns2")));
124+
125+
assertThat(eventSource.manager().isWatchingNamespace("ns2")).isTrue();
126+
verify(eventHandler).handleEvent(any());
127+
eventSource.stop();
128+
}
129+
130+
private void replayCacheOnHandlerRegistration() {
131+
var informer = informer();
132+
var existing = new Deployment();
133+
existing.setMetadata(
134+
new ObjectMetaBuilder()
135+
.withName("existing")
136+
.withNamespace("ns2")
137+
.withResourceVersion("1")
138+
.build());
139+
doAnswer(
140+
invocation -> {
141+
ResourceEventHandler<Deployment> handler = invocation.getArgument(0);
142+
handler.onAdd(existing);
143+
return null;
144+
})
145+
.when(informer)
146+
.addEventHandler(any());
147+
}
148+
103149
/**
104150
* A successfully started {@link InformerEventSource} lists its cache to populate the
105151
* primary-to-secondary index, and the mock client leaves the informer's store unstubbed.
106152
*/
107153
private void stubInformerStore() {
108-
SharedIndexInformer informer =
109-
clientMock
110-
.resources(Deployment.class)
111-
.inNamespace("ns1")
112-
.withLabelSelector((String) null)
113-
.withShardSelector(null)
114-
.runnableInformer(0);
154+
var informer = informer();
115155
when(informer.getStore()).thenReturn(mock(Cache.class));
116156
}
117157

158+
/** The mock client hands out the very same informer for every namespace. */
159+
private SharedIndexInformer informer() {
160+
return clientMock
161+
.resources(Deployment.class)
162+
.inNamespace("ns1")
163+
.withLabelSelector((String) null)
164+
.withShardSelector(null)
165+
.runnableInformer(0);
166+
}
167+
118168
private InformerEventSource<Deployment, TestCustomResource> buildEventSource() {
169+
return buildEventSource(mock(EventHandler.class));
170+
}
171+
172+
private InformerEventSource<Deployment, TestCustomResource> buildEventSource(
173+
EventHandler eventHandler) {
119174
ConfigurationService configurationService =
120175
ConfigurationService.newOverriddenConfigurationService(
121176
new BaseConfigurationService(),
@@ -125,7 +180,7 @@ private InformerEventSource<Deployment, TestCustomResource> buildEventSource() {
125180
when(controllerConfiguration.getName()).thenReturn("controller");
126181

127182
var eventSource = new InformerEventSource<Deployment, TestCustomResource>(configuration);
128-
eventSource.setEventHandler(mock(EventHandler.class));
183+
eventSource.setEventHandler(eventHandler);
129184
eventSource.setControllerConfiguration(controllerConfiguration);
130185
return eventSource;
131186
}

0 commit comments

Comments
 (0)