@@ -115,7 +115,21 @@ public WebSocketClient(JDAImpl api)
115
115
this .shardInfo = api .getShardInfo ();
116
116
this .shouldReconnect = api .isAutoReconnect ();
117
117
this .connectNode = new StartingNode ();
118
- api .getSessionController ().appendSession (connectNode );
118
+ try
119
+ {
120
+ api .getSessionController ().appendSession (connectNode );
121
+ }
122
+ catch (RuntimeException | Error e )
123
+ {
124
+ LOG .error ("Failed to append new session to session controller queue. Shutting down!" , e );
125
+ this .api .setStatus (JDA .Status .SHUTDOWN );
126
+ this .api .getEventManager ().handle (
127
+ new ShutdownEvent (api , OffsetDateTime .now (), 1006 ));
128
+ if (e instanceof RuntimeException )
129
+ throw (RuntimeException ) e ;
130
+ else
131
+ throw (Error ) e ;
132
+ }
119
133
}
120
134
121
135
public JDA getJDA ()
@@ -396,17 +410,20 @@ private JSONObject newVoiceOpen(AudioManager manager, VoiceChannel channel)
396
410
397
411
public void close ()
398
412
{
399
- socket .sendClose (1000 );
413
+ if (socket != null )
414
+ socket .sendClose (1000 );
400
415
}
401
416
402
417
public void close (int code )
403
418
{
404
- socket .sendClose (code );
419
+ if (socket != null )
420
+ socket .sendClose (code );
405
421
}
406
422
407
423
public void close (int code , String reason )
408
424
{
409
- socket .sendClose (code , reason );
425
+ if (socket != null )
426
+ socket .sendClose (code , reason );
410
427
}
411
428
412
429
public synchronized void shutdown ()
@@ -552,10 +569,19 @@ else if (closeCode != null)
552
569
if (isInvalidate )
553
570
invalidate (); // 1000 means our session is dropped so we cannot resume
554
571
api .getEventManager ().handle (new DisconnectEvent (api , serverCloseFrame , clientCloseFrame , closedByServer , OffsetDateTime .now ()));
555
- if (sessionId == null )
572
+ try
573
+ {
574
+ if (sessionId == null )
575
+ queueReconnect ();
576
+ else // if resume is possible
577
+ reconnect ();
578
+ }
579
+ catch (InterruptedException e )
580
+ {
581
+ LOG .error ("Failed to resume due to interrupted thread" , e );
582
+ invalidate ();
556
583
queueReconnect ();
557
- else // if resume is possible
558
- reconnect ();
584
+ }
559
585
}
560
586
}
561
587
@@ -578,7 +604,7 @@ protected void queueReconnect()
578
604
}
579
605
}
580
606
581
- protected void reconnect ()
607
+ protected void reconnect () throws InterruptedException
582
608
{
583
609
reconnect (false , true );
584
610
}
@@ -592,7 +618,7 @@ protected void reconnect()
592
618
* @param shouldHandleIdentify
593
619
* whether SessionReconnectQueue already handled an IDENTIFY rate limit for this session
594
620
*/
595
- public void reconnect (boolean callFromQueue , boolean shouldHandleIdentify )
621
+ public void reconnect (boolean callFromQueue , boolean shouldHandleIdentify ) throws InterruptedException
596
622
{
597
623
if (callFromQueue && api .getContextMap () != null )
598
624
api .getContextMap ().forEach (MDC ::put );
@@ -613,23 +639,15 @@ public void reconnect(boolean callFromQueue, boolean shouldHandleIdentify)
613
639
}
614
640
while (shouldReconnect )
615
641
{
616
- try
617
- {
618
- api .setStatus (JDA .Status .WAITING_TO_RECONNECT );
619
- if (handleIdentifyRateLimit && shouldHandleIdentify )
620
- {
621
- LOG .error ("Encountered IDENTIFY (OP {}) Rate Limit! Waiting {} seconds before trying again!" ,
622
- WebSocketCode .IDENTIFY , IDENTIFY_DELAY );
623
- Thread .sleep (IDENTIFY_DELAY * 1000 );
624
- }
625
- else
626
- {
627
- Thread .sleep (reconnectTimeoutS * 1000 );
628
- }
629
- handleIdentifyRateLimit = false ;
630
- api .setStatus (JDA .Status .ATTEMPTING_TO_RECONNECT );
631
- }
632
- catch (InterruptedException ignored ) {}
642
+ api .setStatus (JDA .Status .WAITING_TO_RECONNECT );
643
+ int delay = IDENTIFY_DELAY ;
644
+ if (handleIdentifyRateLimit && shouldHandleIdentify )
645
+ LOG .error ("Encountered IDENTIFY (OP {}) Rate Limit! Waiting {} seconds before trying again!" , WebSocketCode .IDENTIFY , IDENTIFY_DELAY );
646
+ else
647
+ delay = reconnectTimeoutS ;
648
+ Thread .sleep (delay * 1000 );
649
+ handleIdentifyRateLimit = false ;
650
+ api .setStatus (JDA .Status .ATTEMPTING_TO_RECONNECT );
633
651
LOG .warn ("Attempting to reconnect!" );
634
652
try
635
653
{
0 commit comments