/**
  * Execute an operation and wait until the connection is closed. This is only useful for :reload
  * and :shutdown operations.
  *
  * @param operation the operation to execute
  * @return the operation result
  * @throws IOException for any error
  */
 public ModelNode executeAwaitConnectionClosed(final ModelNode operation) throws IOException {
   final DomainTestClient client = internalGetOrCreateClient();
   final Channel channel = client.getChannel();
   final Connection ref = channel.getConnection();
   ModelNode result = new ModelNode();
   try {
     result = client.execute(operation);
     // IN case the operation wasn't successful, don't bother waiting
     if (!"success".equals(result.get("outcome").asString())) {
       return result;
     }
   } catch (Exception e) {
     if (e instanceof IOException) {
       final Throwable cause = e.getCause();
       if (cause instanceof ExecutionException) {
         // ignore, this might happen if the channel gets closed before we got the response
       } else {
         throw (IOException) e;
       }
     } else {
       throw new RuntimeException(e);
     }
   }
   try {
     if (channel != null) {
       // Wait for the channel to close
       channel.awaitClosed();
     }
     // Wait for the connection to be closed
     connection.awaitConnectionClosed(ref);
   } catch (InterruptedException e) {
     Thread.currentThread().interrupt();
   }
   return result;
 }
 @Override
 public void handleClose(final Channel closed, final IOException exception) {
   if (CLIModelControllerClient.this.closed) {
     return;
   }
   synchronized (lock) {
     if (strategy != null) {
       if (strategy != originalStrategy) {
         new Exception("Channel close handler " + strategy + " " + originalStrategy)
             .printStackTrace();
       }
       strategy = null;
       closeHandler.handleClose();
     }
     channelAssociation.handleChannelClosed(closed, exception);
     lock.notifyAll();
   }
   // Closing the strategy in this handler may result in race conditions
   // with connection closing and then deadlocks in remoting
   // it's safer to close the strategy from the connection close handler
   closed
       .getConnection()
       .addCloseHandler(
           new CloseHandler<Connection>() {
             @Override
             public void handleClose(Connection closed, IOException exception) {
               StreamUtils.safeClose(originalStrategy);
             }
           });
 }