The function code gets retried on deadlock, release the scratch buffer
before testing/looping in that case.  Reference #996.