Skip to content

Commit da8bc03

Browse files
fix: ensure Topic#publish1 returns accurate result during concurrent closure
1 parent 8290854 commit da8bc03

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

core/shared/src/main/scala/fs2/concurrent/Topic.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,22 @@ object Topic {
164164
signalClosure.tryGet.flatMap {
165165
case Some(_) => Topic.closed.pure[F]
166166
case None =>
167-
state.get
168-
.flatMap { case (subs, _) => foreach(subs)(_.send(a).void) }
169-
.as(Topic.rightUnit)
167+
state.get.flatMap { case (subs, _) =>
168+
subs.foldLeft(F.pure(Topic.rightUnit)) { case (acc, (_, chan)) =>
169+
acc.flatMap {
170+
case Left(Topic.Closed) => Topic.closed.pure[F]
171+
case Right(_) =>
172+
chan.send(a).flatMap {
173+
case Right(_) => Topic.rightUnit.pure[F]
174+
case Left(_) =>
175+
signalClosure.tryGet.map {
176+
case Some(_) => Topic.closed
177+
case None => Topic.rightUnit
178+
}
179+
}
180+
}
181+
}
182+
}
170183
}
171184

172185
def subscribeAwait(maxQueued: Int): Resource[F, Stream[F, A]] =

core/shared/src/test/scala/fs2/concurrent/TopicSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class TopicSuite extends Fs2Suite {
188188

189189
// https://github.com/typelevel/fs2/issues/3644
190190
test(
191-
"when publish1 returns success, subscribers must receive the event, even if the publish1 races with close".fail
191+
"when publish1 returns success, subscribers must receive the event, even if the publish1 races with close"
192192
) {
193193
val check: IO[Unit] =
194194
Topic[IO, String]

0 commit comments

Comments
 (0)