Skip to content

Commit

Permalink
Fix KPorfolioSolver instance closing
Browse files Browse the repository at this point in the history
The solver wasn't been closed when it was in the configuration phase,
since `use` block were created only after the configuration happened.
The issue happened when `CancellationException` was thrown during
the configuration.
  • Loading branch information
niyaznigmatullin committed Aug 16, 2023
1 parent 448fbbf commit 3a47cdd
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class KSMTSolver(
z3SolverInternal.toString()
}

private suspend fun check(state: Bool_, query: Bool_): Pair<KSolverStatus, Any> = buildSolver().use { solver ->
private suspend fun check(state: Bool_, query: Bool_): Pair<KSolverStatus, Any> = runSolver { solver ->
if (logFormulae) {
log.run {
debug("State: {}", state)
Expand All @@ -275,7 +275,7 @@ class KSMTSolver(
val result = solver.checkAsync(timeout.seconds)
log.debug("Solver finished")

return when (result) {
return@runSolver when (result) {
KSolverStatus.SAT -> `try`<Pair<KSolverStatus, Any>> {
val model = solver.modelAsync()
if (logFormulae) log.debug(model)
Expand All @@ -298,13 +298,14 @@ class KSMTSolver(
}
}

private suspend fun buildSolver(): KPortfolioSolver {
private suspend fun <R> runSolver(body: suspend (KPortfolioSolver) -> R): R {
if (!currentCoroutineContext().isActive) yield()
return portfolioSolverManager.createPortfolioSolver(ef.ctx).also {
return portfolioSolverManager.createPortfolioSolver(ef.ctx).use {
it.configureAsync {
setIntParameter("random_seed", ksmtSeed)
setIntParameter("seed", ksmtSeed)
}
body(it)
}
}

Expand Down Expand Up @@ -706,7 +707,7 @@ class KSMTSolver(
private suspend fun checkIncremental(
state: Bool_,
queries: List<Triple<Bool_, List<Bool_>, KSMTContext>>
): List<Triple<KSolverStatus, Any, KSMTContext>> = buildSolver().use { solver ->
): List<Triple<KSolverStatus, Any, KSMTContext>> = runSolver { solver ->
solver.assertAndTrackAsync(
state.asAxiom() as KExpr<KBoolSort>
)
Expand All @@ -715,7 +716,7 @@ class KSMTSolver(
)
solver.pushAsync()

return queries.map { (hardConstraints, softConstraints, ctx) ->
return@runSolver queries.map { (hardConstraints, softConstraints, ctx) ->
solver.assertAndTrackAsync(
hardConstraints.asAxiom() as KExpr<KBoolSort>
)
Expand Down

0 comments on commit 3a47cdd

Please sign in to comment.