@@ -232,8 +232,12 @@ public void exception(final IOSession protocolSession, final Exception cause) {
232
232
}
233
233
final IOEventHandler handler = session .getHandler ();
234
234
if (handshakeStateRef .get () != TLSHandShakeState .COMPLETE ) {
235
- session .close (CloseMode .GRACEFUL );
236
- close (CloseMode .IMMEDIATE );
235
+ if (cause instanceof SSLHandshakeException ) {
236
+ close (CloseMode .GRACEFUL );
237
+ } else {
238
+ session .close (CloseMode .GRACEFUL );
239
+ close (CloseMode .IMMEDIATE );
240
+ }
237
241
}
238
242
if (handler != null ) {
239
243
handler .exception (protocolSession , cause );
@@ -467,13 +471,17 @@ private void updateEventMask() {
467
471
this .sslEngine .closeOutbound ();
468
472
this .outboundClosedCount .incrementAndGet ();
469
473
}
470
- if (this .status == Status .CLOSING && this .sslEngine .isOutboundDone ()
474
+ final HandshakeStatus handshakeStatus = this .sslEngine .getHandshakeStatus ();
475
+ if (this .status == Status .CLOSING
476
+ && (handshakeStatus == HandshakeStatus .NOT_HANDSHAKING || handshakeStatus == HandshakeStatus .FINISHED )
477
+ && !this .outEncrypted .hasData ()
478
+ && this .sslEngine .isOutboundDone ()
471
479
&& (this .endOfStream || this .sslEngine .isInboundDone ())) {
472
480
this .status = Status .CLOSED ;
473
481
}
474
482
// Abnormal session termination
475
483
if (this .status .compareTo (Status .CLOSING ) <= 0 && this .endOfStream
476
- && this . sslEngine . getHandshakeStatus () == HandshakeStatus .NEED_UNWRAP ) {
484
+ && handshakeStatus == HandshakeStatus .NEED_UNWRAP ) {
477
485
this .status = Status .CLOSED ;
478
486
}
479
487
if (this .status == Status .CLOSED ) {
@@ -484,7 +492,7 @@ private void updateEventMask() {
484
492
return ;
485
493
}
486
494
// Is there a task pending?
487
- if (this . sslEngine . getHandshakeStatus () == HandshakeStatus .NEED_TASK ) {
495
+ if (handshakeStatus == HandshakeStatus .NEED_TASK ) {
488
496
doRunTask ();
489
497
}
490
498
// Need to toggle the event mask for this channel?
@@ -527,7 +535,7 @@ private void updateEventMask() {
527
535
private int sendEncryptedData () throws IOException {
528
536
this .session .getLock ().lock ();
529
537
try {
530
- if (!this .outEncrypted .hasData ()) {
538
+ if (this . status == Status . ACTIVE && !this .outEncrypted .hasData ()) {
531
539
// If the buffer isn't acquired or is empty, call write() with an empty buffer.
532
540
// This will ensure that tests performed by write() still take place without
533
541
// having to acquire and release an empty buffer (e.g. connection closed,
@@ -719,6 +727,8 @@ public void close(final CloseMode closeMode) {
719
727
// in the JSSE provider. For instance
720
728
// com.android.org.conscrypt.NativeCrypto#SSL_get_shutdown can
721
729
// throw NPE at this point
730
+ doHandshake (this );
731
+ sendEncryptedData ();
722
732
updateEventMask ();
723
733
} catch (final CancelledKeyException ex ) {
724
734
this .session .close (CloseMode .GRACEFUL );
0 commit comments