Skip to content

bun build and firebase-admin: firestore not able to fetch a document when called inside an bundled executable. #19601

Closed as duplicate

Description

@JaapWeijland

What version of Bun is running?

1.2.13

What platform is your computer?

Darwin 24.4.0 arm64 arm

What steps can reproduce the bug?

(Here is a repo with the minimal reproduction of this bug)

  • Create an application with bun and Firebase Firestore
  • Do a query with Firestore
  • Bundle the app
  • Run the app with bun poc/poc.js
  • Experience the hang for 43 secs.

Error:

29 |  */
30 | function callErrorFromStatus(status, callerStack) {
31 |     const message = `${status.code} ${constants_1.Status[status.code]}: ${status.details}`;
32 |     const error = new Error(message);
33 |     const stack = `${error.stack}\nfor call at\n${callerStack}`;
34 |     return Object.assign(new Error(message), status, { stack });
                                  ^
error: undefined undefined: undefined
     code: undefined,
  details: undefined,
 metadata: Metadata {
  internalRepr: Map {},
  options: [Object ...],
  set: [Function: set],
  add: [Function: add],
  remove: [Function: remove],
  get: [Function: get],
  getMap: [Function: getMap],
  clone: [Function: clone],
  merge: [Function: merge],
  setOptions: [Function: setOptions],
  getOptions: [Function: getOptions],
  toHttp2Headers: [Function: toHttp2Headers],
  toJSON: [Function: toJSON],
},

      at callErrorFromStatus (/Users/jaapweijlandprive/Documents/WeijlandIT/darts-expo/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/call.js:34:30)

Full poc:

import { getApps, initializeApp, cert } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';

export const getFirebaseServiceAccount = () => {
    const serviceAccount = process.env.FIREBASE_SERVICE_ACCOUNT;
    if (!serviceAccount) {
        throw new Error(
            'FIREBASE_SERVICE_ACCOUNT is not set in environment variables.',
        );
    }
    return JSON.parse(serviceAccount);
};

// Initialize Firebase Admin only if it hasn't been initialized
if (getApps().length === 0) {
    const serviceAccount = getFirebaseServiceAccount();
    initializeApp({
        credential: cert(serviceAccount),
    });
    console.log('Firebase Admin initialized.');
}

const firestore = getFirestore();

const run = async () => {
    const user = await firestore
        .collection('environments/development/users')
        .doc('lyZ9eFzyJwNqqBZ8C4nzW8ahpw03')
        .get();
    console.log('Done fetching.');
    console.log(user.data());
};

run();

Commands:

  • bun build ./src/poc.ts --target bun --outdir ./poc --sourcemap
  • bun poc/poc.js

What is the expected behavior?

Firestore fetching a document.

What do you see instead?

A hang for about 40 seconds, after which an error:

29 |  */
30 | function callErrorFromStatus(status, callerStack) {
31 |     const message = `${status.code} ${constants_1.Status[status.code]}: ${status.details}`;
32 |     const error = new Error(message);
33 |     const stack = `${error.stack}\nfor call at\n${callerStack}`;
34 |     return Object.assign(new Error(message), status, { stack });
                                  ^
error: undefined undefined: undefined
     code: undefined,
  details: undefined,
 metadata: Metadata {
  internalRepr: Map {},
  options: [Object ...],
  set: [Function: set],
  add: [Function: add],
  remove: [Function: remove],
  get: [Function: get],
  getMap: [Function: getMap],
  clone: [Function: clone],
  merge: [Function: merge],
  setOptions: [Function: setOptions],
  getOptions: [Function: getOptions],
  toHttp2Headers: [Function: toHttp2Headers],
  toJSON: [Function: toJSON],
},

      at callErrorFromStatus (/Users/[...]/Documents/[...]/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/call.js:34:30)

Additional information

  • There are 10 documents in the environments/development/users collection
  • Running bun src/poc.ts does work as expected, so it looks like it has something to do with building.
  • Version firebase-admin: 13.3.0

Activity

  1. kan2k commented on May 18, 2025

    @kan2k

    encountered the same issue in a docker environment for bun 1.2.9

    this solved it but doesnt support streaming
    const firestore = initializeFirestore(app, { preferRest: true });

  2. plgrazon commented on Jun 9, 2025

    @plgrazon

    @kan2k @JaapWeijland having this issue as well, but I was able to solve it by declaring firebase-admin as an external module

    I know this is not optimal since it beats the purpose of compiling the app.

  3. Andrii-Antoniuk commented on Jul 28, 2025

    @Andrii-Antoniuk

    similar happens for @grpc/grpc-js (loaded by @zilliz/milvus2-sdk-node)

  4. Obiwarn commented on Aug 20, 2025

    @Obiwarn

    same here. any idea where this may come from?

  5. fwf-ben commented on Nov 13, 2025

    @fwf-ben

    Just ran into this - really hard to diagnose, only happens in my built code in my docker image, I was not running the built code locally.

    fix was:

    bun build src/index.ts --outdir dist --target bun --external firebase-admin
    
  6. Gorthog commented on Dec 10, 2025

    @Gorthog

    now even the workaround of using it as external doesn't work :-(

  7. Gorthog commented on Dec 10, 2025

    @Gorthog

    this is a regression from bun 1.3.4
    with bun 1.3.3 it works

    FROM oven/bun:1.3.3-alpine AS builder
    RUN bun build ./src/main.ts \
    	--minify \
    	--sourcemap \
    	--target bun-linux-x64-musl \
    	--compile \
    	--external firebase-admin \
    	--outfile bun_app
    

    if I change to latest image, oven/bun:alpine - when i start bun_app i get:

    error: Cannot find module 'firebase-admin/app' from '/$bunfs/root/bun_app'

  8. MichaelTendoSsemwanga commented on Apr 3, 2026

    @MichaelTendoSsemwanga

    +1

  9. blikblum commented on Apr 13, 2026

    @blikblum

    I created an other repository that works without the need to create an service account: https://github.com/blikblum/bun-firebase

    It uses firebase web/modular SDK instead of firebase-admin but the bug is the same / related to GRPC

    Running npm run start:bun (execute src directly) works fine
    Running npm run run:bun:dist (execute built js file) shows the error

  10. gogakoreli commented on May 21, 2026

    @gogakoreli

    We may have found the underlying mechanism for this class of bug. See #31158.

    TL;DR: Bun's JSC uses SIGPWR (signal 30) for thread suspension on Linux (ceb3e74, shipped ~v1.2.3). When any dlopen'd library spawns threads that don't handle SIGPWR, JSC's suspension retry loop becomes infinite — flooding the process with ~8,500 signals/second and permanently starving the event loop.

    Why this might explain your issue: firebase-admin → @grpc/grpc-js → native addons that may spawn background threads. If any of those threads don't acknowledge SIGPWR, you'd see the exact symptom: network calls hang indefinitely after bundling (which changes JIT tier-up behavior, triggering the suspension path more aggressively).

    Quick test: if you can reproduce the hang, run strace -f -e tgkill <your_binary> and look for infinite tgkill(..., SIGPWR) in the output. If you see thousands of SIGPWR signals, it's the same bug.

    Full investigation with reproduction and strace proof: #31158

  11. added 2 commits that reference this issue on Aug 13, 2026
    debe29c
    2c1ba3e
  12. robobun commented on Aug 15, 2026

    @robobun
    Collaborator

    This is the same bug tracked in #9367: bundling a google-gax/@grpc/grpc-js based client (here firebase-admin's Firestore, there @google-cloud/pubsub and friends) with bun build makes every RPC fail with the identical Error: undefined undefined: undefined from grpc-js callErrorFromStatus while the unbundled code works. The root-cause analysis in #9367 (protobufjs' eval("require") probe failing inside Bun bundles, so proto-loader serializers return Uint8Array instead of Buffer) was worked out on the Firestore case itself, and the same workarounds (REST fallback or --external) apply to both; please follow #9367 for updates. (#9367 is in turn being consolidated into #14891, which is where this will be tracked.)

    Duplicate of #9367

    Please add a 👍 or any extra details over there so it all stays in one place. If this is actually a different problem, say so here and we'll reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions