Skip to content

Commit b002ba6

Browse files
committed
8334719: (se) Deferred close of SelectableChannel may result in a Selector doing the final close before concurrent I/O on channel has completed
Reviewed-by: lucy Backport-of: 9bb675f89dd1eeec423ca96cb3f96d29f5de477c
1 parent 978075f commit b002ba6

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
@@ -1927,6 +1927,11 @@ protected void implCloseSelectableChannel() throws IOException {
19271927

19281928
@Override
19291929
public void kill() {
1930+
// wait for any read/write operations to complete before trying to close
1931+
readLock.lock();
1932+
readLock.unlock();
1933+
writeLock.lock();
1934+
writeLock.unlock();
19301935
synchronized (stateLock) {
19311936
if (state == ST_CLOSING) {
19321937
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
@@ -653,6 +653,9 @@ protected void implCloseSelectableChannel() throws IOException {
653653

654654
@Override
655655
public void kill() {
656+
// wait for any accept operation to complete before trying to close
657+
acceptLock.lock();
658+
acceptLock.unlock();
656659
synchronized (stateLock) {
657660
if (state == ST_CLOSING) {
658661
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
@@ -1127,6 +1127,11 @@ protected void implCloseSelectableChannel() throws IOException {
11271127

11281128
@Override
11291129
public void kill() {
1130+
// wait for any read/write operations to complete before trying to close
1131+
readLock.lock();
1132+
readLock.unlock();
1133+
writeLock.lock();
1134+
writeLock.unlock();
11301135
synchronized (stateLock) {
11311136
if (state == ST_CLOSING) {
11321137
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)