Cloud Functions: How to safely stop in-flight Firestore writes during a security migration?

Hello Google Cloud community,

I’m developing a multi-tenant SaaS application using Firebase Authentication, Cloud Firestore and Google Cloud Functions (Node.js 20).

We are preparing a security migration of our account onboarding system. Before deploying the changes, we need to establish a reliable cutover point that prevents older Cloud Function executions from committing privileged Firestore writes.

Our current architecture

We have three existing callable functions:

  • resolveMyEnterprise — can provision an enterprise and its owner membership.

  • createWorkerInvitation — creates worker invitations.

  • acceptWorkerInvitation — accepts invitations and provisions worker relationships.

These functions currently use the same runtime service account as other business functions.

Our new implementation introduces explicit tenant verification, server-side authorization, transactional idempotency and a Firestore-based onboarding barrier.

The problem

We reproduced three race conditions using the Firestore Emulator:

  1. An older Cloud Function starts a Firestore transaction.

  2. The new onboarding barrier is set to CLOSED and its transaction commits.

  3. The older function continues and successfully commits a privileged write after the barrier was closed.

The old functions do not read the new barrier document, so the barrier cannot invalidate their transactions.

We understand that:

  • Changing IAM permissions may prevent new invocations but does not necessarily terminate requests already running.

  • Deploying a new function revision does not automatically prove that every previous execution has finished.

  • Firestore Security Rules do not restrict Admin SDK writes.

  • Revoking permissions from the shared service account could affect unrelated production functions.

Our questions

  1. Is there an officially supported way to stop admission and safely drain or terminate all in-flight executions of specific Cloud Functions without affecting other functions sharing the same service account?

  2. Does replacing, disabling or deleting a function provide any documented guarantee regarding already-running executions and Firestore transactions?

  3. How can we establish a verifiable cutover point after which no legacy execution can commit a privileged Firestore write?

  4. Can Cloud Run revision controls provide a stronger solution for second-generation functions? How should already-running requests be handled?

  5. Would migrating the replacement handlers to dedicated service accounts help, given that existing instances still use the shared identity?

  6. If strict quiescence cannot be guaranteed by the platform, what migration pattern would you recommend for this situation?

We are willing to temporarily disable new registrations and worker invitations during maintenance. Existing users and unrelated business functionality should remain available.

We have not deployed the migration because our local concurrency tests demonstrate that our current cutover strategy is insufficient.

We are looking for a documented and operationally verifiable solution, not merely a fixed waiting period or the absence of requests in logs.

The exact Cloud Functions generation (1st or 2nd generation) is still being verified.

Any guidance or official documentation would be greatly appreciated.

Thank you!

Hallo guys.

Meybe Here are the deterministic migration steps to create a guaranteed breakpoint:

  1. Implement a Maintenance Revision (Circuit Breaker). Implement temporary code for all three functions. This code intentionally doesn’t load the Firebase Admin SDK dependencies to prevent premature transaction initiation and immediately rejecting new requests.

const { onCall, HttpsError } = require(“firebase-functions/v2/https”); // Implement this temporary version to override the current functions exports.resolveMyEnterprise = onCall((request) => { throw new HttpsError(‘unavailable’, ‘The registration system is currently undergoing security maintenance.’); }); exports.createWorkerInvitation = onCall((request) => { throw new HttpsError(‘unavailable’, ‘The registration system is currently undergoing security maintenance.’); }); exports.acceptWorkerInvitation = onCall((request) => { throw new HttpsError(‘unavailable’, ‘The registration system is currently undergoing security maintenance.’); });

  1. Force Terminate Old Executions (In-Flight Termination) Deploying the maintenance revision above ensures new traffic is handled by the circuit breaker, but the old execution may still be processing its promises. You can force terminate it by deleting the previous revision.

Use the Google Cloud CLI to find and delete revisions executing the old code:

1. Find the name of the old revision that still handles in-flight traffic: gcloud run revisions list --service=resolvemyenterprise --region=YOUR_REGION # 2. Explicitly delete the old revision: gcloud run revisions delete OLD_REVISION_NAME --region=YOUR_REGION

Verification Point: This command triggers a graceful shutdown of Cloud Run. After this command completes and the 10-second window passes, the platform guarantees that all containers running the old version receive a SIGKILL. There will be no remnants of the old process in memory.

  1. Set a Firestore Barrier. Once you’ve ensured that the old code execution is eliminated at the compute level, you’ve reached a system quiescence point. Change the barrier document in Firestore to CLOSED.

  2. Deploy the New Architecture with a Dedicated Service Account. It’s now safe to deploy new operational code. Since the old functionality is no longer present, the transition to a dedicated Service Account will separate the security domains from this point forward.

const { onCall } = require(“firebase-functions/v2/https”); const admin = require(“firebase-admin”); admin.initializeApp(); const db = admin.firestore(); exports.resolveMyEnterprise = onCall({ // Using isolated service account serviceAccount: “auth-custom-account@project-id.iam.gserviceaccount.com”, // Function duration limitation timeoutSeconds: 60 }, async (request) => { const barrierRef = db.collection(‘System’).doc(‘RegistrationBarrier’); return db.runTransaction(async (transaction) => { const barrierSnap = await transaction.get(barrierRef); if (barrierSnap.data().status === ‘CLOSED’) { throw new Error(“Registration is currently closing, transaction canceled.”); } // New account creation logic with strict server-side authorization… }); });

Once the new code is fully implemented, back to firestore anda open again.

Pada Sel, 27 Okt 2026, 08.00, newzik 4you <no-reply@discuss.google.dev> menulis: