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

Weird behavior with pubsub function when using --inspect-functions #4289

Closed
schultek opened this issue Mar 11, 2022 · 1 comment
Closed

Weird behavior with pubsub function when using --inspect-functions #4289

schultek opened this issue Mar 11, 2022 · 1 comment
Labels
Needs: Author Feedback Issues awaiting author feedback

Comments

@schultek
Copy link

[REQUIRED] Environment info

firebase-tools: 10.2.1

Platform: macOS

[REQUIRED] Test case

I created a blank new project using firebase init and activated functions, storage and emulators (functions, storage, pubsub).

I then created two functions:

  1. Storage function, that is publishing a pubsub message when triggered
  2. Pubsub function listening to the topic
import * as functions from 'firebase-functions';
import { PubSub } from '@google-cloud/pubsub';

const pubsub = new PubSub();

export const onFile = functions.storage
  .bucket()
  .object()
  .onFinalize((object) => {
    console.log('OBJECT WRITTEN', object);
    pubsub.topic('TEST').publishMessage({ data: Buffer.from('test') });
  });

export const onMessage = functions.pubsub.topic('TEST').onPublish((message) => {
  console.log('GOT MESSAGE', message);
});

[REQUIRED] Steps to reproduce

In the package.json change the serve script to npm run build && firebase emulators:start --only functions,storage,pubsub --inspect-functions. Then

  1. Call npm run serve
  2. Upload any file to the storage emulator using the emulator ui
  3. Observe

[REQUIRED] Expected behavior

When uploading the file what should happen is:

  1. The storage function gets called
  2. It prints OBJECT WRITTEN and the storage object
  3. It publishes a pubsub message
  4. The pubsub function gets called
  5. It prints GOT MESSAGE and the message

[REQUIRED] Actual behavior

  1. The storage function gets called
  2. It prints OBJECT WRITTEN and the storage object
  3. It publishes a pubsub message
  4. The storage function gets called again
  5. It prints OBJECT WRITTEN and the pubsub message
  6. Since it publishes another pubsub message, the function gets called in an infinite loop
kilian@Kilians-MacBook-Pro functions % npm run serve

> serve
> npm run build && firebase emulators:start --only functions,storage,pubsub --inspect-functions


> build
> tsc

i  emulators: Starting emulators: functions, pubsub, storage
⚠  emulators: It seems that you are running multiple instances of the emulator suite for project at-development-302017. This may result in unexpected behavior.
⚠  functions: You are running the functions emulator in debug mode (port=9229). This means that functions will execute in sequence rather than in parallel.
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, hosting
✔  functions: Using node@16 from host.
⚠  functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /Users/kilian/.config/gcloud/application_default_credentials.json. Non-emulated services will access production using these credentials. Be careful!
i  pubsub: Pub/Sub Emulator logging to pubsub-debug.log
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/Users/kilian/Desktop/test/functions" for Cloud Functions...
✔  functions[us-central1-onFile]: storage function initialized.
✔  functions[us-central1-onMessage]: pubsub function initialized.

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:8002                │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5002 │ http://localhost:8002/functions │
├───────────┼────────────────┼─────────────────────────────────┤
│ Pub/Sub   │ localhost:8085 │ n/a                             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Storage   │ localhost:9199 │ http://localhost:8002/storage   │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
 
>  Debugger listening on ws://localhost:9229/bfcbb360-d440-4c40-b272-5e9c12326c97
>  Debugger listening on ws://localhost:9229/bfcbb360-d440-4c40-b272-5e9c12326c97
>  For help, see: https://nodejs.org/en/docs/inspector
i  functions: Beginning execution of "onFile"
>  OBJECT WRITTEN {
>    kind: '#storage#object',
>    name: '2021-11-27T13:05:05.760Z.jpg',
>    bucket: 'at-development-302017.appspot.com',
>    generation: '1647003462436',
>    metageneration: '1',
>    contentType: 'image/jpeg',
>    timeCreated: '2022-03-11T13:57:42.435Z',
>    updated: '2022-03-11T13:57:42.445Z',
>    storageClass: 'STANDARD',
>    size: '719578',
>    md5Hash: 'degsNKFq/wfjF/MGsWP9dQ==',
>    etag: 'someETag',
>    crc32c: '----Sw==',
>    timeStorageClassUpdated: '2022-03-11T13:57:42.435Z',
>    id: 'at-development-302017.appspot.com/2021-11-27T13:05:05.760Z.jpg/1647003462436',
>    selfLink: 'http://localhost:9199/storage/v1/b/at-development-302017.appspot.com/o/2021-11-27T13%3A05%3A05.760Z.jpg',
>    mediaLink: 'http://localhost:9199/download/storage/v1/b/at-development-302017.appspot.com/o/2021-11-27T13%3A05%3A05.760Z.jpg?generation=1647003462436&alt=media'
>  }
>  {"severity":"WARNING","message":"Function returned undefined, expected Promise or value"}
i  functions: Finished "onFile" in ~1s
i  functions: Beginning execution of "onFile"
i  functions: Finished "onFile" in ~1s
>  OBJECT WRITTEN {
>    data: { type: 'Buffer', data: [ 116, 101, 115, 116 ] },
>    attributes: {}
>  }
>  {"severity":"WARNING","message":"Function returned undefined, expected Promise or value"}
i  functions: Beginning execution of "onFile"
i  functions: Finished "onFile" in ~1s
>  OBJECT WRITTEN {
>    data: { type: 'Buffer', data: [ 116, 101, 115, 116 ] },
>    attributes: {}
>  }
>  {"severity":"WARNING","message":"Function returned undefined, expected Promise or value"}
i  functions: Beginning execution of "onFile"
i  functions: Finished "onFile" in ~1s
>  OBJECT WRITTEN {
>    data: { type: 'Buffer', data: [ 116, 101, 115, 116 ] },
>    attributes: {}
>  }
>  {"severity":"WARNING","message":"Function returned undefined, expected Promise or value"}

This only happens when using --inspect-functions, otherwise it works fine!

firebase-debug.log
pubsub-debug.log
ui-debug.log

@schultek schultek added the bug label Mar 11, 2022
@joehan
Copy link
Contributor

joehan commented Mar 14, 2022

This looks like it is related to #4232, which was fixed in firebase-tools@10.2.2 . Could you update to the latest version and see if this still occurs?

@joehan joehan added the Needs: Author Feedback Issues awaiting author feedback label Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Author Feedback Issues awaiting author feedback
Projects
None yet
Development

No branches or pull requests

2 participants