Commit 0d1a5c45 authored by Tom Evans's avatar Tom Evans

OF-881: Simplify connection close semantics

Avoid recursion and synchronization issues
parent 0cf8db1a
...@@ -144,29 +144,9 @@ public interface Connection { ...@@ -144,29 +144,9 @@ public interface Connection {
* <li>Call notifyEvent all listeners that the channel is shutting down. * <li>Call notifyEvent all listeners that the channel is shutting down.
* <li>Close the socket. * <li>Close the socket.
* </ul> * </ul>
*
* An invocation of this method is equal to invoking {@link #close(boolean)} with a parameter
* that is false.
*/ */
public void close(); public void close();
/**
* Close this session including associated socket connection. The order of
* events for closing the session is:
* <ul>
* <li>Set closing flag to prevent redundant shutdowns.
* <li>Call notifyEvent all listeners that the channel is shutting down.
* <li>Close the socket.
* </ul>
*
* This method takes into account the connection state of the peer. Specifically,
* when the peer is known to be in a disconnected state, no data will be sent
* (otherwise, this method can trigger the delivery of an end-of-stream signal).
*
* @param peerIsKnownToBeDisconnected should be set to true when the peer is known to no longer be available.
*/
public void close( boolean peerIsKnownToBeDisconnected );
/** /**
* Notification message indicating that the server is being shutdown. Implementors * Notification message indicating that the server is being shutdown. Implementors
* should send a stream error whose condition is system-shutdown before closing * should send a stream error whose condition is system-shutdown before closing
......
...@@ -468,55 +468,40 @@ public class SocketConnection implements Connection { ...@@ -468,55 +468,40 @@ public class SocketConnection implements Connection {
@Override @Override
public void close() { public void close() {
close( false );
}
@Override
public void close( boolean peerIsKnownToBeDisconnected ) {
boolean wasClosed = false;
synchronized (this) { synchronized (this) {
if (!isClosed()) { if (isClosed()) {
try { return;
if (session != null) { }
session.setStatus(Session.STATUS_CLOSED); if (session != null) {
} session.setStatus(Session.STATUS_CLOSED);
}
if ( !peerIsKnownToBeDisconnected ) { }
boolean allowedToWrite = false;
try { boolean allowedToWrite = false;
requestWriting(); try {
allowedToWrite = true; requestWriting();
// Register that we started sending data on the connection allowedToWrite = true;
writeStarted(); // Register that we started sending data on the connection
writer.write("</stream:stream>"); writeStarted();
if (flashClient) { writer.write("</stream:stream>");
writer.write('\0'); if (flashClient) {
} writer.write('\0');
writer.flush();
}
catch (IOException e) {
// Do nothing
}
finally {
// Register that we finished sending data on the connection
writeFinished();
if (allowedToWrite) {
releaseWriting();
}
}
}
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.close")
+ "\n" + this.toString(), e);
}
closeConnection();
wasClosed = true;
} }
writer.flush();
}
catch (Exception e) {
Log.error("Failed to deliver stream close tag: " + e.getMessage());
} }
if (wasClosed) {
notifyCloseListeners(); // Register that we finished sending data on the connection
writeFinished();
if (allowedToWrite) {
releaseWriting();
} }
closeConnection();
notifyCloseListeners();
} }
@Override @Override
......
...@@ -194,31 +194,25 @@ public abstract class VirtualConnection implements Connection { ...@@ -194,31 +194,25 @@ public abstract class VirtualConnection implements Connection {
*/ */
@Override @Override
public void close() { public void close() {
close( false );
}
@Override
public void close(boolean peerIsKnownToBeDisconnected) {
boolean wasClosed = false;
synchronized (this) { synchronized (this) {
if (!isClosed()) { if (isClosed()) {
try { return;
if (session != null) { }
session.setStatus(Session.STATUS_CLOSED); if (session != null) {
} session.setStatus(Session.STATUS_CLOSED);
closeVirtualConnection();
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.close")
+ "\n" + this.toString(), e);
}
closed = true;
wasClosed = true;
} }
} }
if (wasClosed) {
notifyCloseListeners(); try {
closeVirtualConnection();
} catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.close")
+ "\n" + this.toString(), e);
} }
closed = true;
notifyCloseListeners();
} }
@Override @Override
......
...@@ -228,60 +228,33 @@ public class NIOConnection implements Connection { ...@@ -228,60 +228,33 @@ public class NIOConnection implements Connection {
@Override @Override
public void close() { public void close() {
close( false ); synchronized ( this ) {
} // prevent recursion while closing
if ( state == State.CLOSED || state == State.CLOSING) {
@Override return;
public void close( boolean peerIsKnownToBeDisconnected ) }
{ state = State.CLOSING;
boolean notifyClose = false; }
synchronized ( this ) {
try
{
if ( state == State.CLOSED )
{
return;
}
// This prevents any action after the first invocation of close() on this connection.
if ( state != State.CLOSING )
{
state = State.CLOSING;
if ( !peerIsKnownToBeDisconnected )
{
try
{
deliverRawText( flashClient ? "</flash:stream>" : "</stream:stream>" );
}
catch ( Exception e )
{
// Ignore
}
}
}
// deliverRawText might already have forced the state from Closing to Closed. In that case, there's no need try {
// to invoke the CloseListeners again. deliverRawText( flashClient ? "</flash:stream>" : "</stream:stream>" );
if ( state == State.CLOSING ) } catch ( Exception e ) {
{ Log.error("Failed to deliver stream close tag: " + e.getMessage());
notifyClose = true; }
}
} // Ensure that the state of this connection, its session and the MINA context are eventually closed.
finally if ( session != null ) {
{ session.setStatus( Session.STATUS_CLOSED );
// Ensure that the state of this connection, its session and the MINA context are eventually closed.
state = State.CLOSED;
if ( session != null )
{
session.setStatus( Session.STATUS_CLOSED );
}
ioSession.close( true );
}
} }
if (notifyClose)
{ try {
notifyCloseListeners(); // clean up session, etc. ioSession.close( true );
} catch (Exception e) {
Log.error("Exception while closing MINA session", e);
} }
state = State.CLOSED;
notifyCloseListeners(); // clean up session, etc.
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment