Correctly reset cached local and remote address when disconnect() is called (#9545)

Motivation:

We should correctly reset the cached local and remote address when a Channel.disconnect() is called and the channel has a notion of disconnect vs close (for example DatagramChannel implementations).

Modifications:

- Correctly reset cached kicak abd remote address
- Update testcase to cover it and so ensure all transports work in a consistent way

Result:

Correctly handle disconnect()
This commit is contained in:
Norman Maurer 2019-09-19 08:51:10 +02:00 committed by GitHub
parent 2fe2a15593
commit 3ad037470e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 0 deletions

View File

@ -298,9 +298,14 @@ public class DatagramUnicastTest extends AbstractDatagramTest {
}
assertTrue(cc.isConnected());
assertNotNull(cc.localAddress());
assertNotNull(cc.remoteAddress());
// Test what happens when we call disconnect()
cc.disconnect().syncUninterruptibly();
assertFalse(cc.isConnected());
assertNotNull(cc.localAddress());
assertNull(cc.remoteAddress());
ChannelFuture future = cc.writeAndFlush(
buf.retain().duplicate()).awaitUninterruptibly();

View File

@ -191,6 +191,11 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann
}
}
void resetCachedAddresses() {
local = socket.localAddress();
remote = socket.remoteAddress();
}
@Override
protected void doDisconnect() throws Exception {
doClose();

View File

@ -453,6 +453,7 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements
protected void doDisconnect() throws Exception {
socket.disconnect();
connected = active = false;
resetCachedAddresses();
}
@Override

View File

@ -132,6 +132,11 @@ abstract class AbstractKQueueChannel extends AbstractChannel implements UnixChan
doClose();
}
void resetCachedAddresses() {
local = socket.localAddress();
remote = socket.remoteAddress();
}
@Override
protected boolean isCompatible(EventLoop loop) {
return loop instanceof KQueueEventLoop;

View File

@ -386,6 +386,7 @@ public final class KQueueDatagramChannel extends AbstractKQueueChannel implement
protected void doDisconnect() throws Exception {
socket.disconnect();
connected = active = false;
resetCachedAddresses();
}
@Override

View File

@ -578,6 +578,9 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
boolean wasActive = isActive();
try {
doDisconnect();
// Reset remoteAddress and localAddress
remoteAddress = null;
localAddress = null;
} catch (Throwable t) {
safeSetFailure(promise, t);
closeIfClosed();