@@ -184,7 +184,7 @@ public long applyAsLong(Object value) {
184
184
private final Map <String , String > connectionProperties ;
185
185
private final Duration rpcTimeout ;
186
186
private final List <String > saslMechanisms ;
187
- private volatile ShutdownReason shutdownReason = null ;
187
+ private AtomicReference < ShutdownReason > shutdownReason = new AtomicReference <>() ;
188
188
private final Runnable streamStatsCommandVersionsCheck ;
189
189
private final boolean filteringSupported ;
190
190
private final Runnable superStreamManagementCommandVersionsCheck ;
@@ -1435,17 +1435,21 @@ public Response unsubscribe(byte subscriptionId) {
1435
1435
1436
1436
public void close () {
1437
1437
if (closing .compareAndSet (false , true )) {
1438
- LOGGER .debug ("Closing client" );
1439
-
1440
- sendClose (RESPONSE_CODE_OK , "OK" );
1441
-
1442
- closingSequence (ShutdownContext .ShutdownReason .CLIENT_CLOSE );
1443
-
1438
+ LOGGER .debug ("Closing client, channel still active? {}" , this .channel .isActive ());
1439
+ ShutdownReason reason ;
1440
+ if (this .channel .isActive ()) {
1441
+ sendClose (RESPONSE_CODE_OK , "OK" );
1442
+ reason = ShutdownReason .CLIENT_CLOSE ;
1443
+ } else {
1444
+ reason = ShutdownReason .UNKNOWN ;
1445
+ }
1446
+ closingSequence (reason );
1444
1447
LOGGER .debug ("Client closed" );
1445
1448
}
1446
1449
}
1447
1450
1448
1451
void closingSequence (ShutdownContext .ShutdownReason reason ) {
1452
+ this .shutdownReason (reason );
1449
1453
if (reason != null ) {
1450
1454
this .shutdownListenerCallback .accept (reason );
1451
1455
}
@@ -1713,7 +1717,7 @@ public void consumerUpdateResponse(
1713
1717
}
1714
1718
1715
1719
void shutdownReason (ShutdownReason reason ) {
1716
- this .shutdownReason = reason ;
1720
+ this .shutdownReason . compareAndSet ( null , reason ) ;
1717
1721
}
1718
1722
1719
1723
public SocketAddress localAddress () {
@@ -2858,16 +2862,14 @@ public void channelInactive(ChannelHandlerContext ctx) {
2858
2862
// the event is actually dispatched to the listener, emitting
2859
2863
// an UNKNOWN reason instead of SERVER_CLOSE. So we skip the closing here
2860
2864
// because it will be handled later anyway.
2861
- if (shutdownReason == null ) {
2865
+ if (shutdownReason .get () == null ) {
2866
+ LOGGER .debug ("No shutdown reason" );
2862
2867
if (closing .compareAndSet (false , true )) {
2868
+ LOGGER .debug ("Closing with 'unknown' shutdown reason" );
2863
2869
if (executorService == null ) {
2864
2870
// the TCP connection is closed before the state is initialized
2865
2871
// we do our best the execute the closing sequence
2866
- new Thread (
2867
- () -> {
2868
- closingSequence (ShutdownReason .UNKNOWN );
2869
- })
2870
- .start ();
2872
+ new Thread (() -> closingSequence (ShutdownReason .UNKNOWN )).start ();
2871
2873
} else {
2872
2874
executorService .submit (() -> closingSequence (ShutdownReason .UNKNOWN ));
2873
2875
}
0 commit comments