title: Why do I get CancelledKeyException when going through the keys?
body: <p>Why do I get the <code>CancelledKeyException</code> few times a day? Should I do something about it? Is my code wrong?</p>
  
  <pre><code>        Iterator&lt;SelectionKey&gt; keys = selector.selectedKeys().iterator();
          while (keys.hasNext()) {
  
              SelectionKey key = (SelectionKey) keys.next();
              keys.remove();
  
              try {
                  if (key.isValid()) {
                      if (key.isReadable()) {
                          readHandler.handle((Connection) key.attachment());
                      }
                      if (key.isWritable()) {
                          writeHandler.handle((Connection) key.attachment());
                      }
                      if (key.isAcceptable()) {
                          acceptHandler.handle(key);
                      }
                  }
              } catch (CancelledKeyException e) {
                  _logger.error("CanceledKeyException in while loop:", e);
              }
          }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>java.nio.channels.CancelledKeyException: null
      at sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:55) ~[na:1.6.0_12]
      at sun.nio.ch.SelectionKeyImpl.readyOps(SelectionKeyImpl.java:69) ~[na:1.6.0_12]
      at java.nio.channels.SelectionKey.isWritable(SelectionKey.java:294) ~[na:1.6.0_12]
      at project.engine.io.SimpleReactor.work(SimpleReactor.java:194) ~[engine-02.06.11.jar:na]
      at project.server.work.AbstractWorker$1.run(AbstractWorker.java:20) [server-21.05.11.jar:na]
      at java.lang.Thread.run(Thread.java:619) [na:1.6.0_12]
  </code></pre>