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

Ignore ENOTCONN on socket shutdown #157

Merged
merged 2 commits into from
Aug 27, 2023
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 @@ -38,6 +38,7 @@ import scala.scalanative.annotation.stub
import scala.scalanative.libc.errno
import scala.scalanative.meta.LinktimeInfo
import scala.scalanative.posix
import scala.scalanative.posix.errno._
import scala.scalanative.posix.netdbOps._
import scala.scalanative.unsafe._
import scala.scalanative.unsigned._
Expand Down Expand Up @@ -79,14 +80,14 @@ final class EpollAsyncSocketChannel private (
def isOpen = _isOpen

def shutdownInput(): AsynchronousSocketChannel = {
if (posix.sys.socket.shutdown(fd, 0) == -1)
if (posix.sys.socket.shutdown(fd, 0) == -1 && errno.errno != ENOTCONN)
throw new IOException(s"shutdown: ${errno.errno}")
this
}

def shutdownOutput(): AsynchronousSocketChannel = {
outputShutdown = true
if (posix.sys.socket.shutdown(fd, 1) == -1)
if (posix.sys.socket.shutdown(fd, 1) == -1 && errno.errno != ENOTCONN)
throw new IOException(s"shutdown: ${errno.errno}")
this
}
Expand Down
18 changes: 18 additions & 0 deletions tests/shared/src/test/scala/epollcat/TcpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,22 @@ class TcpSuite extends EpollcatSuite {
.flatMap(address => IOServerSocketChannel.open.evalTap(_.bind(address)).use_)
}

test("shutdown ignores ENOTCONN") {
IOServerSocketChannel
.open
.evalTap(_.bind(new InetSocketAddress("localhost", 0)))
.use { server =>
server.localAddress.flatTap { address =>
val connect =
IOSocketChannel.open.evalTap(_.connect(address)).surround(IO.sleep(100.millis))
val accept = server.accept.use { ch =>
ch.write(ByteBuffer.wrap("hello".getBytes)) *>
IO.sleep(1.second) *> ch.shutdownInput *> ch.shutdownOutput
}
connect.both(accept).void
}
}
.flatMap(address => IOServerSocketChannel.open.evalTap(_.bind(address)).use_)
}

}