Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IOServerSocketChannel#accept cancelable #31

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class EpollAsyncServerSocketChannel private (fd: Int)
}
}

def close(): Unit = {
def close(): Unit = if (isOpen()) {
_isOpen = false
unmonitor.run()
if (posix.unistd.close(fd) == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class EpollAsyncSocketChannel private (fd: Int)
}
}

def close(): Unit = {
def close(): Unit = if (isOpen()) {
_isOpen = false
unmonitor.run()
if (posix.unistd.close(fd) == -1)
Expand Down
23 changes: 21 additions & 2 deletions tests/shared/src/test/scala/epollcat/TcpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.ClosedChannelException
import java.nio.channels.CompletionHandler
import java.nio.charset.StandardCharsets
import scala.concurrent.duration._

class TcpSuite extends EpollcatSuite {

Expand Down Expand Up @@ -79,8 +80,15 @@ class TcpSuite extends EpollcatSuite {

def accept: Resource[IO, IOSocketChannel] =
Resource
.make(IO.async_[AsynchronousSocketChannel](cb => ch.accept(null, toHandler(cb))))(ch =>
IO(ch.close()))
.makeFull[IO, AsynchronousSocketChannel] { poll =>
poll {
IO.async { cb =>
IO(ch.accept(null, toHandler(cb)))
// it seems the only way to cancel accept is to close the socket :(
.as(Some(IO(ch.close())))
}
Comment on lines +85 to +89
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fundamental issue is that there is no good way with the JDK APIs to cancel a pending accept (which is really dumb to be honest). Some googling suggested that closing the channel is the only way to do so.

This is very ugly, but it is good enough for our tests ... I think.

}
}(ch => IO(ch.close()))
.map(new IOSocketChannel(_))

def setOption[T](option: SocketOption[T], value: T): IO[Unit] =
Expand Down Expand Up @@ -270,4 +278,15 @@ class TcpSuite extends EpollcatSuite {
}
}

test("IOServerSocketChannel.accept is cancelable") {
// note that this test targets IOServerSocketChannel#accept,
// not the underlying AsynchronousSocketChannel#accept implementation
IOServerSocketChannel
.open
.evalTap(_.bind(new InetSocketAddress(0)))
.flatMap(_.accept)
.use_
.timeoutTo(100.millis, IO.unit)
}

}