Skip to content

Wraping a function Sentry.GCPFunction.wrapEventFunction leads to premature function exit #3325

Closed
@killthekitten

Description

@killthekitten

Package + Version

@sentry/serverless

Version:

6.2.2

Description

It looks like Sentry.GCPFunction.wrapEventFunction does not correctly signal function termination in async background cloud functions. The function exits prematurely while the async code continues running in the background. You can see this behavior in the logs:

Screenshot 2021-03-12 at 14 53 10

This is a follow up to an issue in the nodejs-pubsub client, where multiple users reported that pub/sub event functions operate with an unexpectedly high latency. In my case, removing the sentry wrapper solved the issue.

image

The access to the CPU is limited once the function terminates, and any code that keeps running in background takes minutes to execute or eventually times out.

It looks like only the pub/sub triggered functions are affected. The HTTP triggered functions wrapped in Sentry.GCPFunction.wrapHttpFunction work as expected.

package.json

{
  "name": "sentry-gcf-repro",
  "version": "1.0.0",
  "main": "function.js",
  "license": "MIT",
  "dependencies": {
    "@sentry/serverless": "^6.2.2"
  },
  "engines": {
    "node": ">= 14.15 <15"
  }
}

function.js

const Sentry = require("@sentry/serverless");

Sentry.GCPFunction.init({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 1.0,
});

const sleep = (ms) => {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

const sayHello = async () => {
  await sleep(5000);
  console.log("Hello")
}

exports.sayHelloHandler = Sentry.GCPFunction.wrapEventFunction(
  sayHello
);

Steps to reproduce

You would need to create a topic, deploy the function and send a message to the topic:

gcloud pubsub topics create hello
gcloud functions deploy sayHello --trigger-topic hello --runtime nodejs14 --entry-point sayHelloHandler --region europe-west3
gcloud pubsub topics publish hello --message="{}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions