title: How to write Kotlin-colloquial code to retry synchronously until timeout or success?
body: <p>After reading Kotlin documentation, I came up with the following code (which is not working - see below) to repeat a function call until it returns true, or timeout is reached.<br>
  I want to pause execution until this code block reaches timeout or success - it is not supposed to execute asynchronously.</p>
  
  <pre><code>Log.d(TAG, "This is the last line to be logged")
  runBlocking {
      Log.d(TAG, "this line is never logged")
      try {
          withTimeout(timeoutMsL) {
              while ((isActive) &amp;&amp; (!success)) {
                  success = doSomething()
              }
          } 
      }
      catch (ex: TimeoutCancellationException) {
          Log.d(TAG, "this line is never logged either")
          doSomethingElse()
      }
  }
  </code></pre>
  
  <p><code>timeoutMsL</code> is a <code>Long</code> with typical value 50 ms.
  This code is called from C++ over the JNI.  When I run it</p>
  
  <ul>
  <li>nothing inside the runBlocking block runs</li>
  <li>nothing after the runBlocking block runs</li>
  <li>control returns to the C++ caller</li>
  <li>there is an exception in the JNI, but the JNI doesn't log Kotlin or Java exception details.</li>
  <li>no exception is logged in adb</li>
  <li>when I tried surrounding the above code snippet with a try/catch/log block to catch Kotlin exceptions, nothing is logged</li>
  </ul>
  
  <p>I have read that <code>runBlocking</code> should be avoided, but also you have to call <code>withTimeout</code> from an existing coroutine.<br>
  If I use a normal coroutine, execution of the calling function will continue before timeout /success is reached - I need to prevent this from happening.  </p>
  
  <p>How should this be coded in Kotlin?</p>