From b45528203d75eee81df0070ba10bea289a4b4224 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 31 Jan 2012 09:48:05 -0700 Subject: [PATCH] avoid crashes due to calling select or wakeup on a closed Selector --- .../nio/channels/ClosedSelectorException.java | 13 +++++++++++++ classpath/java/nio/channels/SocketSelector.java | 16 +++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 classpath/java/nio/channels/ClosedSelectorException.java diff --git a/classpath/java/nio/channels/ClosedSelectorException.java b/classpath/java/nio/channels/ClosedSelectorException.java new file mode 100644 index 0000000000..046fc37f4f --- /dev/null +++ b/classpath/java/nio/channels/ClosedSelectorException.java @@ -0,0 +1,13 @@ +/* Copyright (c) 2012, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.nio.channels; + +public class ClosedSelectorException extends IllegalStateException { } diff --git a/classpath/java/nio/channels/SocketSelector.java b/classpath/java/nio/channels/SocketSelector.java index 2d5cdbd699..a19d608918 100644 --- a/classpath/java/nio/channels/SocketSelector.java +++ b/classpath/java/nio/channels/SocketSelector.java @@ -15,7 +15,7 @@ import java.util.Iterator; import java.net.Socket; class SocketSelector extends Selector { - protected long state; + protected volatile long state; protected final Object lock = new Object(); protected boolean woken = false; @@ -31,7 +31,7 @@ class SocketSelector extends Selector { public Selector wakeup() { synchronized (lock) { - if (! woken) { + if (isOpen() && (! woken)) { woken = true; natWakeup(state); @@ -66,6 +66,10 @@ class SocketSelector extends Selector { } public int doSelect(long interval) throws IOException { + if (! isOpen()) { + throw new ClosedSelectorException(); + } + selectedKeys.clear(); if (clearWoken()) interval = -1; @@ -107,9 +111,11 @@ class SocketSelector extends Selector { } public synchronized void close() { - if (isOpen()) { - natClose(state); - state = 0; + synchronized (lock) { + if (isOpen()) { + natClose(state); + state = 0; + } } }