Skip to content

Commit 9bb675f

Browse files
jaikiranAlan Bateman
and
Alan Bateman
committed
8334719: (se) Deferred close of SelectableChannel may result in a Selector doing the final close before concurrent I/O on channel has completed
Co-authored-by: Alan Bateman <[email protected]> Reviewed-by: alanb, dfuchs
1 parent 6682305 commit 9bb675f

File tree

7 files changed

+705
-0
lines changed

7 files changed

+705
-0
lines changed

src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,11 @@ protected void implCloseSelectableChannel() throws IOException {
19451945

19461946
@Override
19471947
public void kill() {
1948+
// wait for any read/write operations to complete before trying to close
1949+
readLock.lock();
1950+
readLock.unlock();
1951+
writeLock.lock();
1952+
writeLock.unlock();
19481953
synchronized (stateLock) {
19491954
if (state == ST_CLOSING) {
19501955
tryFinishClose();

src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ protected void implCloseSelectableChannel() throws IOException {
654654

655655
@Override
656656
public void kill() {
657+
// wait for any accept operation to complete before trying to close
658+
acceptLock.lock();
659+
acceptLock.unlock();
657660
synchronized (stateLock) {
658661
if (state == ST_CLOSING) {
659662
tryFinishClose();

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,11 @@ protected void implCloseSelectableChannel() throws IOException {
12161216

12171217
@Override
12181218
public void kill() {
1219+
// wait for any read/write operations to complete before trying to close
1220+
readLock.lock();
1221+
readLock.unlock();
1222+
writeLock.lock();
1223+
writeLock.unlock();
12191224
synchronized (stateLock) {
12201225
if (state == ST_CLOSING) {
12211226
tryFinishClose();

src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ protected void implCloseSelectableChannel() throws IOException {
201201

202202
@Override
203203
public void kill() {
204+
// wait for any write operation to complete before trying to close
205+
writeLock.lock();
206+
writeLock.unlock();
204207
synchronized (stateLock) {
205208
if (state == ST_CLOSING) {
206209
tryFinishClose();

src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ protected void implCloseSelectableChannel() throws IOException {
200200
}
201201
@Override
202202
public void kill() {
203+
// wait for any read operation to complete before trying to close
204+
readLock.lock();
205+
readLock.unlock();
203206
synchronized (stateLock) {
204207
assert !isOpen();
205208
if (state == ST_CLOSING) {

0 commit comments

Comments
 (0)