Make ensure_app() thread-safe