wait for queries to finish before freeing context (bb #648)