@@ -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 :
0 commit comments