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
parent fafde4aeec
commit 1b06de76c9
6 changed files with 20 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@ -138,6 +138,11 @@ abstract class AbstractKQueueChannel extends AbstractChannel implements UnixChan
doClose(); doClose();
} }
void resetCachedAddresses() {
local = socket.localAddress();
remote = socket.remoteAddress();
}
@Override @Override
public boolean isOpen() { public boolean isOpen() {
return socket.isOpen(); return socket.isOpen();

View File

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

View File

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