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