Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initializeTestEnvironment sometimes failed due to Node.js fetch failed by SocketError: other side closed #8690

Open
jin-qin opened this issue Jan 5, 2025 · 5 comments
Labels

Comments

@jin-qin
Copy link

jin-qin commented Jan 5, 2025

Operating System

Ubuntu 22.04

Environment (if applicable)

GitHub Actions

Firebase SDK Version

11.1.0

Firebase SDK Product(s)

Firestore

Project Tooling

React Native (Expo), Jest, TypeScript.

Detailed Problem Description

Hi Firebase team,

We recently occasionally have this error in our GitHub workflow, we've never had this issue in local device:

    TypeError: fetch failed
      4 |
      5 | export async function initializeTestEnv() {
    > 6 |   const testEnv = await initializeTestEnvironment({
        |                   ^
      7 |     projectId: "xxx",
      8 |     firestore: {
      9 |       rules: fs.readFileSync("firestore.rules", "utf8"),
      at async discoverEmulators (node_modules/@firebase/rules-unit-testing/src/impl/discovery.ts:31:15)
      at async initializeTestEnvironment (node_modules/@firebase/rules-unit-testing/src/initialize.ts:66:32)
      at async initializeTestEnv (lib/utils/unit-test.ts:6:19)
      at async Object.<anonymous> (lib/api-client/__tests__/user-report-test.node.ts:16:15)
    Cause:
    SocketError: other side closed

It seems await fetchImpl(makeUrl(hub, '/emulators')); in discoverEmulators throwed the SocketError: other side closed and the codes didn't catch it then the codes crashed, can we retry when discoverEmulators catches such an error?

Our Github workflow is similar to below:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20.15"

      - name: Install dependencies
        run: |
          npm install -g firebase-tools
          yarn

      - name: Lint
        run: yarn eslint .

      - name: Check TypeScript errors
        run: yarn tsc --noEmit

      - name: Run tests
        run: firebase emulators:exec --only "firestore,auth,storage" "jest --runInBand --updateSnapshot --detectOpenHandles --forceExit --coverage"

We invoke initializeTestEnv like below:

await initializeTestEnvironment({
    projectId: "xxx",
    firestore: {
      rules: fs.readFileSync("firestore.rules", "utf8"),
      host: 'localhost',
      port: 8080,
    },
    storage: {
      rules: fs.readFileSync("storage.rules", "utf8"),
      host: 'localhost',
      port: 9199,
    },
    hub: {
      host: 'localhost',
      port: 4400
    }
  })

We invoke it in each Jest unit test file in beforeAll, there are 5 Jest test files in our codebase, so this function is called 5 consecutive times.

Steps and code to reproduce issue

N/A, this is an occasional issue and only observed in GitHub Actions workflows.

@jin-qin jin-qin added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Jan 5, 2025
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@jbalidiong jbalidiong added api: firestore needs-attention testing-sdk testing with emulator and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels Jan 6, 2025
@tom-andersen
Copy link
Contributor

Hi @jin-qin!

Retrying should be a safe approach as far as I know. It is possible that the emulator isn't ready to accept connections, so upon second attempt it will work. If the problem persists, please let us know.

@DellaBitta
Copy link
Contributor

Yes, agreed! Generally this error means that the runtime couldn't connect to the emulator. If the problem persists, it would be helpful to know if the emulator has logged any errors when it was initialized.

@jin-qin
Copy link
Author

jin-qin commented Jan 7, 2025

Thank you folks for the quick responses!

The only error I got is SocketError: other side closed which caused by fetch API.

The weird part is, my 5 tests ran sequentially and the first one succeeded, but the second one failed by this issue, then the rest of the tests succeeded again, sometimes the order may be different.

My request is: is it possible for firebase-js-sdk to add a retry functionality while invoking fetch API to mitigate this kind of failure?

@DellaBitta
Copy link
Contributor

I'll add the ability to configure retries to the Rules Unit Testing product backlog but it might take a while for the team to get to it since there's a viable workaround.

I've removed the Firestore label from this issue since it's the Emulator Hub that's closing the socket connection, not the Firestore emulator or product SDK.

It might be worth opening an issue in firebase-tools repo, which hosts the emulators, since their Emulator Hub is what's closing the socket connection. I went to create an issue on your behalf but their issue templates requires more details than I have here (your firebase-cli version, etc.). Please do link to this issue when you create one there so that they know that we sent you. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants