Skip to content

Commit

Permalink
Merge pull request #170 from HoneyryderChuck/cancelled_key
Browse files Browse the repository at this point in the history
preventing CancelledKeyException
  • Loading branch information
tarcieri authored Nov 14, 2017
2 parents 2ba5f79 + 01ab970 commit 4e41e89
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ext/nio4r/org/nio4r/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ public IRubyObject removeInterest(ThreadContext context, IRubyObject interest) {

@JRubyMethod
public IRubyObject readiness(ThreadContext context) {
if(!key.isValid())
return this.interests;
return Nio4r.interestOpsToSymbol(context.getRuntime(), key.readyOps());
}

@JRubyMethod(name = "readable?")
public IRubyObject isReadable(ThreadContext context) {
Ruby runtime = context.getRuntime();
if (!this.key.isValid())
return runtime.getTrue();
int readyOps = this.key.readyOps();

if((readyOps & SelectionKey.OP_READ) != 0 || (readyOps & SelectionKey.OP_ACCEPT) != 0) {
Expand All @@ -120,6 +124,8 @@ public IRubyObject isReadable(ThreadContext context) {
@JRubyMethod(name = {"writable?", "writeable?"})
public IRubyObject writable(ThreadContext context) {
Ruby runtime = context.getRuntime();
if (!this.key.isValid())
return runtime.getTrue();
int readyOps = this.key.readyOps();

if((readyOps & SelectionKey.OP_WRITE) != 0 || (readyOps & SelectionKey.OP_CONNECT) != 0) {
Expand Down
4 changes: 3 additions & 1 deletion ext/nio4r/org/nio4r/Selector.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.channels.Channel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.CancelledKeyException;

import org.jruby.Ruby;
import org.jruby.RubyArray;
Expand Down Expand Up @@ -208,6 +209,7 @@ public synchronized IRubyObject select(ThreadContext context, IRubyObject timeou
while(selectedKeys.hasNext()) {
SelectionKey key = (SelectionKey)selectedKeys.next();
processKey(key);

selectedKeys.remove();

if(block.isGiven()) {
Expand Down Expand Up @@ -269,7 +271,7 @@ private void cancelKeys() {
// Remove connect interest from connected sockets
// See: http://stackoverflow.com/questions/204186/java-nio-select-returns-without-selected-keys-why
private void processKey(SelectionKey key) {
if((key.readyOps() & SelectionKey.OP_CONNECT) != 0) {
if(key.isValid() && (key.readyOps() & SelectionKey.OP_CONNECT) != 0) {
int interestOps = key.interestOps();

interestOps &= ~SelectionKey.OP_CONNECT;
Expand Down
8 changes: 8 additions & 0 deletions spec/support/selectable_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@
expect(m.readiness).to eq(:rw)
end
end
it "keeps readiness after the selectable has been closed" do
selector.register(readable_subject, :rw)
selector.select(0) do |m|
expect(m.readiness).to eq(:rw)
readable_subject.close
expect(m.readiness).to eq(:rw)
end
end
end

0 comments on commit 4e41e89

Please sign in to comment.