Debugging concurrent requests, for simple cases

This is a simple strategy for confirming that some values are shared between requests. There must be a more 'correct' way to inspect values in concurrent requests, but I was unable to find it. I normally use a debugger to set breakpoints, but there was no way to set a breakpoint for one request but continue to process a second.

To get around this limitation I modified my code to force the desired concurrency. I used a CyclicBarrier to force the first thread to wait for the second.

object ValueLock {
  val barrier = new java.util.concurrent.CyclicBarrier(2)
}

Then I added logging and synchronization to the request path.

print(s"BEFORE VALUE: ${myvalue}")
ValueLock.barrier.await()
print(s"AFTER VALUE: ${myvalue}")

Then start a request with myvalue set to 1 and then a second request set to 0. In my particular case I found the second request overwrote the value used by the first request.

BEFORE VALUE: 1
BEFORE VALUE: 0
AFTER VALUE: 0
AFTER VALUE: 0

This works in a pinch. There are other ways to confirm this behavior, such as inspecting memory locations referenced in each request, but I like the barrier method because it doesn't rely on a mental model of the service. The behavior is directly confirmed in the log lines.

social