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

Better handling of zeromq connection close #1383

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
@@ -1,6 +1,6 @@
package almond.channels.zeromq

import java.nio.channels.{ClosedByInterruptException, Selector}
import java.nio.channels.{ClosedByInterruptException, ClosedSelectorException, Selector}
import java.nio.charset.StandardCharsets.UTF_8

import almond.channels._
Expand Down Expand Up @@ -137,7 +137,8 @@ final class ZeromqConnection(
case Channel.Input => stdin0
}

@volatile private var selectorOpt = Option.empty[Selector]
@volatile private var selectorClosing = false
@volatile private var selectorOpt = Option.empty[Selector]

private def withSelector[T](f: Selector => T): T =
selectorOpt match {
Expand Down Expand Up @@ -190,18 +191,29 @@ final class ZeromqConnection(
(channel, new PollItem(socket.channel, Poller.POLLIN))
}

withSelector { selector =>
ZMQ.poll(selector, pollItems.map(_._2).toArray, pollingDelay.toMillis)
val doRead = withSelector { selector =>
try {
ZMQ.poll(selector, pollItems.map(_._2).toArray, pollingDelay.toMillis)
true
}
catch {
case _: ClosedSelectorException if selectorClosing || selectorOpt.isEmpty =>
// channel was closed
false
}
}

pollItems
.collectFirst {
case (channel, pi) if pi.isReadable =>
channelSocket0(channel)
.read
.map(_.map((channel, _)))
}
.getOrElse(IO.pure(None))
if (doRead)
pollItems
.collectFirst {
case (channel, pi) if pi.isReadable =>
channelSocket0(channel)
.read
.map(_.map((channel, _)))
}
.getOrElse(IO.pure(None))
else
IO.pure(None)
}.evalOn(threads.pollingEc).flatMap(identity)

def close(partial: Boolean, lingerDuration: Duration): IO[Unit] = {
Expand All @@ -222,8 +234,13 @@ final class ZeromqConnection(
if (!partial)
heartBeatThreadOpt.foreach(_.interrupt())

selectorOpt.foreach(_.close())
selectorOpt = None
try {
selectorClosing = true
selectorOpt.foreach(_.close())
selectorOpt = None
}
finally
selectorClosing = false

log.debug(s"Closed channels for $params" + (if (partial) " (partial)" else ""))
}.evalOn(threads.selectorOpenCloseEc)
Expand Down
Loading