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

Bracket should not evaluate "use" if Task was canceled during "acquire" #1087

Merged
merged 1 commit into from
Dec 13, 2019
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
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2014-2019 by The Monix Project Developers.
* See the project homepage at: https://monix.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package monix.eval

import scala.util.Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,19 @@ private[monix] object TaskBracket {
val conn = ctx.connection

val releaseFrame = makeReleaseFrame(ctx, value)
val onNext = {
val fb = try use(value)
catch { case NonFatal(e) => Task.raiseError(e) }
fb.flatMap(releaseFrame)
}

conn.push(releaseFrame.cancel)
Task.unsafeStartNow(onNext, ctx, cb)
}

// Check if Task wasn't already cancelled in acquire
if (!conn.isCanceled) {
val onNext = {
val fb = try use(value)
catch { case NonFatal(e) => Task.raiseError(e) }
fb.flatMap(releaseFrame)
}

Task.unsafeStartNow(onNext, ctx, cb)
}
}

def onError(ex: Throwable): Unit =
cb.onError(ex)
Expand Down
20 changes: 20 additions & 0 deletions monix-eval/shared/src/test/scala/monix/eval/TaskBracketSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,24 @@ object TaskBracketSuite extends BaseTestSuite {
sc.tick()
assertEquals(f.value, Some(Success(())))
}

test("use is not evaluated on cancel") { implicit sc =>
import scala.concurrent.duration._
var use = false
var release = false

val task = Task
.sleep(2.second)
.bracket(_ => Task { use = true })(_ => Task { release = true })

val f = task.runToFuture
sc.tick()

f.cancel()
sc.tick(2.second)

assertEquals(f.value, None)
assertEquals(use, false)
assertEquals(release, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ object BracketObservableSuite extends BaseTestSuite {
}
}

test("bracket use is not evaluated on cancel") { implicit sc =>
import scala.concurrent.duration._
var use = false
var release = false

val task = Observable
.evalDelayed(2.second, ())
.bracket(_ => Observable.eval { use = true })(_ => Task { release = true })

val f = task.completedL.runToFuture
sc.tick()

f.cancel()
sc.tick(2.second)

assertEquals(f.value, None)
assertEquals(use, false)
assertEquals(release, true)
}

class Semaphore(var acquired: Int = 0, var released: Int = 0) {
def acquire: Task[Handle] =
Task { acquired += 1 }.map(_ => Handle(this))
Expand Down