-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
base: main
Are you sure you want to change the base?
Changes from all commits
4ed7929
26c78a0
d3f45b5
fb11296
be05b3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is |
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Grabbing the lock before you |
||
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() | ||
} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're waiting I can see how it shortcuts while the thread is sleeping. But then you shouldn't need to wait Also, what happens if you interrupt it while it's not sleeping? |
||
try { | ||
if (config.debug) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch here! 😃