Skip to content

Commit b9afd9c

Browse files
authored
Merge pull request #342 from funtax/Handle_Exceptions_during_shutdown
Handle some Exceptions caused eg. by missing network during shutdown
2 parents 802ecf8 + 183a030 commit b9afd9c

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

lib/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private void authenticate(@NotNull Authentication.LoginCredentials credentials)
388388
* {@code true} for {@link Session#reconnect()}.
389389
*/
390390
private void authenticatePartial(@NotNull Authentication.LoginCredentials credentials, boolean removeLock) throws IOException, GeneralSecurityException, SpotifyAuthenticationException {
391-
if (cipherPair == null) throw new IllegalStateException("Connection not established!");
391+
if (conn == null || cipherPair == null) throw new IllegalStateException("Connection not established!");
392392

393393
Authentication.ClientResponseEncrypted clientResponseEncrypted = Authentication.ClientResponseEncrypted.newBuilder()
394394
.setLoginCredentials(credentials)
@@ -409,7 +409,6 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden
409409

410410
receiver = new Receiver();
411411

412-
413412
byte[] bytes0x0f = new byte[20];
414413
random().nextBytes(bytes0x0f);
415414
sendUnchecked(Packet.Type.Unknown_0x0f, bytes0x0f);
@@ -452,6 +451,8 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden
452451
public void close() throws IOException {
453452
LOGGER.info("Closing session. {deviceId: {}}", inner.deviceId);
454453

454+
if (scheduledReconnect != null) scheduledReconnect.cancel(true);
455+
455456
closing = true;
456457

457458
scheduler.shutdownNow();
@@ -513,6 +514,9 @@ public void close() throws IOException {
513514
}
514515

515516
private void sendUnchecked(Packet.Type cmd, byte[] payload) throws IOException {
517+
if (conn == null)
518+
throw new IOException("Cannot write to missing connection.");
519+
516520
cipherPair.sendEncoded(conn.out, cmd.val, payload);
517521
}
518522

@@ -692,6 +696,9 @@ public Configuration configuration() {
692696
}
693697

694698
private void reconnect() {
699+
if (closing)
700+
return;
701+
695702
synchronized (reconnectionListeners) {
696703
reconnectionListeners.forEach(ReconnectionListener::onConnectionDropped);
697704
}
@@ -716,6 +723,9 @@ private void reconnect() {
716723
reconnectionListeners.forEach(ReconnectionListener::onConnectionEstablished);
717724
}
718725
} catch (IOException | GeneralSecurityException | SpotifyAuthenticationException ex) {
726+
if (closing)
727+
return;
728+
719729
conn = null;
720730
LOGGER.error("Failed reconnecting, retrying in 10 seconds...", ex);
721731

@@ -1310,7 +1320,7 @@ public void run() {
13101320
continue;
13111321
}
13121322
} catch (IOException | GeneralSecurityException ex) {
1313-
if (running) {
1323+
if (running && !closing) {
13141324
LOGGER.error("Failed reading packet!", ex);
13151325
reconnect();
13161326
}

player/src/main/java/xyz/gianlu/librespot/player/state/DeviceStateHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.io.UnsupportedEncodingException;
4545
import java.net.URLDecoder;
4646
import java.util.*;
47+
import java.util.concurrent.RejectedExecutionException;
4748

4849
/**
4950
* @author Gianlu
@@ -65,6 +66,7 @@ public final class DeviceStateHandler implements Closeable, DealerClient.Message
6566
private final Connect.PutStateRequest.Builder putState;
6667
private final AsyncWorker<Connect.PutStateRequest> putStateWorker;
6768
private volatile String connectionId = null;
69+
private volatile boolean closing = false;
6870

6971
public DeviceStateHandler(@NotNull Session session, @NotNull PlayerConfiguration conf) {
7072
this.session = session;
@@ -226,7 +228,11 @@ public synchronized void updateState(@NotNull Connect.PutStateReason reason, int
226228
.setClientSideTimestamp(TimeProvider.currentTimeMillis())
227229
.getDeviceBuilder().setDeviceInfo(deviceInfo).setPlayerState(state);
228230

229-
putStateWorker.submit(putState.build());
231+
try {
232+
putStateWorker.submit(putState.build());
233+
} catch (RejectedExecutionException ex) {
234+
if (!closing) LOGGER.error("Failed to submit update state task.", ex);
235+
}
230236
}
231237

232238
public synchronized int getVolume() {
@@ -244,6 +250,8 @@ public void setVolume(int val) {
244250

245251
@Override
246252
public void close() {
253+
closing = true;
254+
247255
session.dealer().removeMessageListener(this);
248256
session.dealer().removeRequestListener(this);
249257

0 commit comments

Comments
 (0)