diff --git a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/StreamingDataSource.java b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/StreamingDataSource.java index 2ca8cadb..055af34c 100644 --- a/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/StreamingDataSource.java +++ b/launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/StreamingDataSource.java @@ -133,9 +133,11 @@ public void onError(Throwable t) { if (code >= 400 && code < 500) { logger.error("Encountered non-retriable error: {}. Aborting connection to stream. Verify correct Mobile Key and Stream URI", code); running = false; - resultCallback.onError(new LDInvalidResponseCodeFailure("Unexpected Response Code From Stream Connection", t, code, false)); if (code == 401) { connection401Error = true; + } + resultCallback.onError(new LDInvalidResponseCodeFailure("Unexpected Response Code From Stream Connection", t, code, false)); + if (code == 401) { dataSourceUpdateSink.shutDown(); } stop(null); diff --git a/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/StreamingDataSourceTest.java b/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/StreamingDataSourceTest.java index c56dd394..bf54c0fd 100644 --- a/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/StreamingDataSourceTest.java +++ b/launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/StreamingDataSourceTest.java @@ -617,9 +617,8 @@ public void startWithHttp401ShutsDownSink() throws Exception { LDInvalidResponseCodeFailure failure = (LDInvalidResponseCodeFailure) error; assertEquals(401, failure.getResponseCode()); assertFalse(failure.isRetryable()); - // The background thread calls resultCallback.onError() before - // dataSourceUpdateSink.shutDown(), so shutDownCalled may not be true yet. - // Poll briefly to allow the background thread to complete. + // shutDown() is called after the error callback, so we need to + // wait briefly for the background thread to complete the call. long deadline = System.currentTimeMillis() + 1000; while (!dataSourceUpdateSink.shutDownCalled && System.currentTimeMillis() < deadline) { Thread.sleep(10);