Skip to content

Commit

Permalink
Merge pull request #157 from armanbilge/pr/fix-shutdown-enotconn
Browse files Browse the repository at this point in the history
Ignore `ENOTCONN` on socket shutdown
  • Loading branch information
armanbilge authored Aug 27, 2023
2 parents d8d73b5 + 5a16c12 commit fc6f369
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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_)
}

}

0 comments on commit fc6f369

Please sign in to comment.