Skip to content

Commit 4b88a70

Browse files
committed
Handle synchronous startup failures
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
1 parent 1d2c90f commit 4b88a70

3 files changed

Lines changed: 103 additions & 6 deletions

File tree

‎CHANGES.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# HAP-Java 2.0.10
22
* Expose asynchronous HomeKit startup failures through `HomekitRoot.startAsync()` and clean up failed starts
3+
* Synchronous startup exceptions are also reported through the returned future; duplicate starts fail without stopping an existing startup or listener
4+
* Failed startup stops the HTTP listener even when only mDNS registration failed, including when using the existing `start()` method; already-paired controllers can no longer use that listener
5+
* Recovery is not automatic: integrations must create a new server and root for another attempt because the default HTTP handler cannot be restarted
36
* Add constructors with custom min/max to the window covering tilt angle characteristics [#162](https://github.com/hap-java/HAP-Java/issues/162)
47
* Add optional Mute characteristic to Doorbell service
58
* Fix leak of mDNS instances and shutdown hooks when stopping `HomekitRoot`

‎src/main/java/io/github/hapjava/server/impl/HomekitRoot.java‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ public void removeAccessory(HomekitAccessory accessory) {
161161
* initial accessories you plan on advertising, as any later additions will cause the HomeKit
162162
* clients to reconnect.
163163
*
164-
* <p>Startup failures are logged. Use {@link #startAsync()} to handle them in the caller.
164+
* <p>Startup failures are logged. Use {@link #startAsync()} to handle them in the caller. An
165+
* advertising failure also stops the HTTP listener. Recovery is not automatic and requires a new
166+
* server and root.
165167
*/
166168
public void start() {
167169
startAsync()
@@ -176,16 +178,37 @@ public void start() {
176178
/**
177179
* Starts handling and advertising the previously added HomeKit accessories.
178180
*
181+
* <p>Startup failures, including exceptions thrown before the HTTP handler returns a future,
182+
* complete the returned future exceptionally after cleanup. If advertising fails after the HTTP
183+
* listener has started, the listener is stopped as well. Recovery requires a new server and root;
184+
* the default HTTP handler cannot be restarted.
185+
*
186+
* <p>A start requested while this root is starting or running fails without stopping the existing
187+
* startup or listener.
188+
*
179189
* @return a future that completes after the HTTP listener has started and the advertiser has
180190
* registered the service, or exceptionally after a failed start has been cleaned up
181191
*/
182192
public CompletableFuture<Void> startAsync() {
193+
if (started) {
194+
CompletableFuture<Void> failure = new CompletableFuture<>();
195+
failure.completeExceptionally(new IllegalStateException("HomeKit root is already started"));
196+
return failure;
197+
}
183198
started = true;
184-
madeChanges = false;
185-
registry.reset();
186-
return webHandler
187-
.start(
188-
new HomekitClientConnectionFactoryImpl(authInfo, registry, subscriptions, advertiser))
199+
CompletableFuture<Integer> portFuture;
200+
try {
201+
madeChanges = false;
202+
registry.reset();
203+
portFuture =
204+
webHandler.start(
205+
new HomekitClientConnectionFactoryImpl(
206+
authInfo, registry, subscriptions, advertiser));
207+
} catch (RuntimeException e) {
208+
portFuture = new CompletableFuture<>();
209+
portFuture.completeExceptionally(e);
210+
}
211+
return portFuture
189212
.thenAccept(
190213
port -> {
191214
try {

‎src/test/java/io/github/hapjava/server/impl/HomekitRootTest.java‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.mockito.Mockito.doThrow;
77
import static org.mockito.Mockito.mock;
88
import static org.mockito.Mockito.never;
9+
import static org.mockito.Mockito.times;
910
import static org.mockito.Mockito.verify;
1011
import static org.mockito.Mockito.when;
1112

@@ -120,6 +121,76 @@ public void testStartAsyncPropagatesBindFailure() throws Exception {
120121
verify(advertiser, never()).setConfigurationIndex(2);
121122
}
122123

124+
@Test
125+
public void testStartAsyncPropagatesSynchronousFailure() throws Exception {
126+
RuntimeException failure = new IllegalStateException("HTTP startup failed");
127+
when(webHandler.start(any())).thenThrow(failure);
128+
129+
CompletableFuture<Void> start = root.startAsync();
130+
131+
Assert.assertTrue(start.isCompletedExceptionally());
132+
Assert.assertSame(
133+
failure, Assert.assertThrows(CompletionException.class, start::join).getCause());
134+
verify(webHandler).stop();
135+
verify(advertiser).stop();
136+
verify(advertiser, never()).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any());
137+
root.setConfigurationIndex(2);
138+
verify(advertiser, never()).setConfigurationIndex(2);
139+
}
140+
141+
@Test
142+
public void testStartCleansUpSynchronousFailure() throws Exception {
143+
when(webHandler.start(any())).thenThrow(new IllegalStateException("HTTP startup failed"));
144+
145+
root.start();
146+
147+
verify(webHandler).stop();
148+
verify(advertiser).stop();
149+
root.setConfigurationIndex(2);
150+
verify(advertiser, never()).setConfigurationIndex(2);
151+
}
152+
153+
@Test
154+
public void testDuplicateStartDoesNotStopPendingStartup() throws Exception {
155+
CompletableFuture<Integer> bind = new CompletableFuture<>();
156+
when(webHandler.start(any())).thenReturn(bind);
157+
158+
CompletableFuture<Void> first = root.startAsync();
159+
CompletableFuture<Void> duplicate = root.startAsync();
160+
161+
Assert.assertTrue(duplicate.isCompletedExceptionally());
162+
Assert.assertTrue(
163+
Assert.assertThrows(CompletionException.class, duplicate::join).getCause()
164+
instanceof IllegalStateException);
165+
Assert.assertFalse(first.isDone());
166+
verify(webHandler, times(1)).start(any());
167+
verify(webHandler, never()).stop();
168+
verify(advertiser, never()).stop();
169+
170+
bind.complete(PORT);
171+
172+
Assert.assertTrue(first.isDone());
173+
first.join();
174+
verify(advertiser).advertise(eq(LABEL), eq(1), any(), eq(PORT), eq(1), any());
175+
}
176+
177+
@Test
178+
public void testDuplicateStartDoesNotStopRunningRoot() throws Exception {
179+
root.startAsync().join();
180+
181+
CompletableFuture<Void> duplicate = root.startAsync();
182+
183+
Assert.assertTrue(duplicate.isCompletedExceptionally());
184+
Assert.assertTrue(
185+
Assert.assertThrows(CompletionException.class, duplicate::join).getCause()
186+
instanceof IllegalStateException);
187+
verify(webHandler, times(1)).start(any());
188+
verify(webHandler, never()).stop();
189+
verify(advertiser, never()).stop();
190+
root.setConfigurationIndex(2);
191+
verify(advertiser).setConfigurationIndex(2);
192+
}
193+
123194
@Test
124195
public void testStartAsyncPropagatesAdvertisingFailure() throws Exception {
125196
IOException failure = new IOException("mDNS registration failed");

0 commit comments

Comments
 (0)