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

Fixing context disposal vs. statistics thread #3009

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .unreleased/bug-fixes/dumps-by-statistics-thread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix thread synchronization between the statistics thread and Z3Context, see #3009
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class BoundedCheckerPassImpl @Inject() (
val checker =
new SeqModelChecker[SnapshotT](params, input, filteredTrex, Seq(DumpFilesModelCheckerListener))
val outcome = checker.run()
rewriter.dispose()
filteredTrex.dispose()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here! 😃

logger.info(s"The outcome is: " + outcome)
outcome
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,21 @@ class Z3SolverContext(val config: SolverConfig) extends SolverContext with LazyL
private val statisticsLock: ReentrantLock = new ReentrantLock()
// start a new thread to collect statistics
private val statisticsThread = new Thread(() => {
while (state == Running()) {
var interrupted = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is Thread#isInterrupted, I don't think you need an extra local flag

while (state == Running() && !interrupted) {
// Sleep for a while.
// If we call printStatistics right away, we can easily run into a race condition with Z3 initializing.
// This produces a core dump.
Thread.sleep(config.z3StatsSec * 1000)
// make sure that the context is not being disposed right now. Otherwise, we can get a nice core dump.
statisticsLock.lock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grabbing the lock before you Thread.sleep() does not look right, it blocks out the other thread for a rather long time.
Why is it necessary?

try {
if (state == Running()) {
printStatistics()
}
Thread.sleep(config.z3StatsSec * 1000)
printStatistics()
} catch {
case _: InterruptedException =>
// terminate the thread immediately upon interruption
logger.info(s"Finishing the statistics thread ${id}")
interrupted = true
} finally {
statisticsLock.unlock()
}
Expand All @@ -173,6 +177,7 @@ class Z3SolverContext(val config: SolverConfig) extends SolverContext with LazyL
state = Disposed()
// Try to obtain the lock, to let the statistics thread finish its work.
// If it is stuck for some reason, continue after the timeout in any case.
statisticsThread.interrupt()
statisticsLock.tryLock(2 * config.z3StatsSec, java.util.concurrent.TimeUnit.SECONDS)
Comment on lines +180 to 181
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're waiting 2*z3StatsSec for the lock, why do you need the interrupt?

I can see how it shortcuts while the thread is sleeping. But then you shouldn't need to wait 2*z3StatsSec afterwards.

Also, what happens if you interrupt it while it's not sleeping?
From the javadoc it does not look like it'd have any effect.

try {
if (config.debug) {
Expand Down
Loading