Skip to content

Commit 757f514

Browse files
author
Martin Lehmann
committed
Handle some Exceptions caused eg. by missing network during shutdown
1 parent 802ecf8 commit 757f514

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ private void authenticatePartial(@NotNull Authentication.LoginCredentials creden
452452
public void close() throws IOException {
453453
LOGGER.info("Closing session. {deviceId: {}}", inner.deviceId);
454454

455+
if (scheduledReconnect != null) scheduledReconnect.cancel(true);
456+
455457
closing = true;
456458

457459
scheduler.shutdownNow();
@@ -513,7 +515,11 @@ public void close() throws IOException {
513515
}
514516

515517
private void sendUnchecked(Packet.Type cmd, byte[] payload) throws IOException {
516-
cipherPair.sendEncoded(conn.out, cmd.val, payload);
518+
try {
519+
cipherPair.sendEncoded(conn.out, cmd.val, payload);
520+
} catch (NullPointerException e) {
521+
throw new IOException(e);
522+
}
517523
}
518524

519525
private void waitAuthLock() {
@@ -692,6 +698,7 @@ public Configuration configuration() {
692698
}
693699

694700
private void reconnect() {
701+
if (!this.closing) {
695702
synchronized (reconnectionListeners) {
696703
reconnectionListeners.forEach(ReconnectionListener::onConnectionDropped);
697704
}
@@ -715,15 +722,18 @@ private void reconnect() {
715722
synchronized (reconnectionListeners) {
716723
reconnectionListeners.forEach(ReconnectionListener::onConnectionEstablished);
717724
}
718-
} catch (IOException | GeneralSecurityException | SpotifyAuthenticationException ex) {
719-
conn = null;
720-
LOGGER.error("Failed reconnecting, retrying in 10 seconds...", ex);
721-
722-
try {
723-
scheduler.schedule(this::reconnect, 10, TimeUnit.SECONDS);
724-
} catch (RejectedExecutionException exx) {
725-
LOGGER.info("Scheduler already shutdown, stopping reconnection", exx);
726-
}
725+
} catch (NullPointerException | IOException | GeneralSecurityException | SpotifyAuthenticationException ex) {
726+
if (!this.closing) {
727+
conn = null;
728+
LOGGER.error("Failed reconnecting, retrying in 10 seconds...", ex);
729+
730+
try {
731+
scheduler.schedule(this::reconnect, 10, TimeUnit.SECONDS);
732+
} catch (RejectedExecutionException exx) {
733+
LOGGER.info("Scheduler already shutdown, stopping reconnection", exx);
734+
}
735+
}
736+
}
727737
}
728738
}
729739

@@ -1309,16 +1319,16 @@ public void run() {
13091319
LOGGER.info("Skipping unknown command {cmd: 0x{}, payload: {}}", Integer.toHexString(packet.cmd), Utils.bytesToHex(packet.payload));
13101320
continue;
13111321
}
1312-
} catch (IOException | GeneralSecurityException ex) {
1313-
if (running) {
1322+
} catch (IOException | GeneralSecurityException | NullPointerException ex) {
1323+
if (running && !closing) {
13141324
LOGGER.error("Failed reading packet!", ex);
13151325
reconnect();
13161326
}
13171327

13181328
break;
13191329
}
13201330

1321-
if (!running) break;
1331+
if (!running || closing) break;
13221332

13231333
switch (cmd) {
13241334
case Ping:

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

Lines changed: 6 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
@@ -226,7 +227,11 @@ public synchronized void updateState(@NotNull Connect.PutStateReason reason, int
226227
.setClientSideTimestamp(TimeProvider.currentTimeMillis())
227228
.getDeviceBuilder().setDeviceInfo(deviceInfo).setPlayerState(state);
228229

229-
putStateWorker.submit(putState.build());
230+
try {
231+
putStateWorker.submit(putState.build());
232+
} catch (RejectedExecutionException e){
233+
LOGGER.debug("Failed to update state, ignoring.", e);
234+
}
230235
}
231236

232237
public synchronized int getVolume() {

0 commit comments

Comments
 (0)