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

Sentry.GCPFunction.wrapHttpFunction cause Error [ERR_STREAM_WRITE_AFTER_END]: write after end #8848

Open
3 tasks done
jiangbo0216 opened this issue Aug 19, 2023 · 6 comments
Labels
Package: serverless Issues related to the Sentry Serverless SDK Type: Bug

Comments

@jiangbo0216
Copy link

jiangbo0216 commented Aug 19, 2023

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which SDK are you using?

@sentry/serverless

SDK Version

"@sentry/serverless": "^7.64.0"

Framework Version

No response

Link to Sentry event

No response

SDK Setup

No response

Steps to Reproduce

new a simple google-cloud demo

pnpm i @google-cloud/functions-framework @sentry/serverless

  1. create index.js
const Sentry = require('@sentry/serverless');
Sentry.GCPFunction.init({
    dsn: <your dsn>,
    tracesSampleRate: 1.0,
  });
exports.helloWorld = Sentry.GCPFunction.wrapHttpFunction(async (req, res) => {
    Promise.reject('error');
    res.send('Hello, World');
  });
  1. start function
npx @google-cloud/functions-framework --target=helloWorld

Expected Result

process run and don't exit

Actual Result

link: #2344

get a error: Error [ERR_STREAM_WRITE_AFTER_END]: write after end
sentry patch end function, cause res.end turn sync to async

const _end = res.end;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
res.end = function (chunk?: any | (() => void), encoding?: string | (() => void), cb?: () => void): any {
transaction?.setHttpStatus(res.statusCode);
transaction?.finish();
void flush(options.flushTimeout)
.then(null, e => {
__DEBUG_BUILD__ && logger.error(e);
})
.then(() => {
_end.call(this, chunk, encoding, cb);
});
};

and @google-cloud/functions-framework depend res.end onfinished to handle error

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/35533b76face60714a9f6050aaced98da231016b/src/server.ts#L137-L143

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/35533b76face60714a9f6050aaced98da231016b/src/function_wrappers.ts#L105-L130

const wrapHttpFunction = (execute) => {
    return (req, res) => {
        const d = domain.create();
        const errorHandler = (err) => {
            if (res.locals.functionExecutionFinished) {
                console.error(`Exception from a finished function: ${err}`);
            }
            else {
                res.locals.functionExecutionFinished = true;
                (0, logger_1.sendCrashResponse)({ err, res });
            }
        };
        // Catch unhandled errors originating from this request.
        d.on('error', errorHandler);
        d.run(() => {
            process.nextTick(() => {
                const ret = execute(req, res);
                // Catch rejected promises if the function is async.
                if (ret instanceof Promise) {
                    ret.catch(errorHandler);
                }
            });
        });
    };
};

in this case,

 res.send('Hello, World')

start to end response but it is async

so when a error in user handler,

        d.on('error', errorHandler);

errorHandler will be triggered, and sendCrashResponse will be called, At this point, Error [ERR_STREAM_WRITE_AFTER_END]: write after end may occur

@github-actions github-actions bot added the Package: serverless Issues related to the Sentry Serverless SDK label Aug 19, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Aug 19, 2023
@Lms24
Copy link
Member

Lms24 commented Aug 23, 2023

Hi @jiangbo0216, thanks for writing in and for opening a PR! We're very busy this week but we'll try to look into this next week. Thanks!

@jiangbo0216
Copy link
Author

hava any update?

Hi @jiangbo0216, thanks for writing in and for opening a PR! We're very busy this week but we'll try to look into this next week. Thanks!

jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Sep 26, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Sep 26, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Sep 27, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Oct 6, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Oct 6, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Oct 10, 2023
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Feb 23, 2024
jiangbo0216 added a commit to jiangbo0216/sentry-javascript that referenced this issue Feb 23, 2024
@simonnztg
Copy link

What's happening with this issue? We're looking to use Sentry but this issues prevents us from using it in our serverless functions environment

@andreiborza
Copy link
Member

Hey @simonnztg thanks for writing in. We're currently on company-wide hackweek and thus on limited support. We'll take another look at this next week.

@lforst
Copy link
Member

lforst commented Aug 26, 2024

Hi, @simonnztg currently we are not working on this issue because bluntly said, the gcpfunction integration doesn't get much use (might be a self-fulfilling prophecy). If you want to use Sentry in your GCP functions, as a workaround, you should be able to just use the @sentry/node package and call Sentry.init() somewhere in the global scope. This will not automatically track the performance of your functions, but you can manually add Sentry.startSpan() calls though.

(Corporate speech off: I low-key want to use GCP functions for my side project so this integration may get some love soon.)

@contactsimonwilson
Copy link

Hi, @simonnztg currently we are not working on this issue because bluntly said, the gcpfunction integration doesn't get much use (might be a self-fulfilling prophecy). If you want to use Sentry in your GCP functions, as a workaround, you should be able to just use the @sentry/node package and call Sentry.init() somewhere in the global scope. This will not automatically track the performance of your functions, but you can manually add Sentry.startSpan() calls though.

(Corporate speech off: I low-key want to use GCP functions for my side project so this integration may get some love soon.)

Thanks for getting back to me. I thought this might be the case, and I have a workaround in place so we can keep using it for now. FWIW, functions support was one of the main things that attracted me to Sentry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: serverless Issues related to the Sentry Serverless SDK Type: Bug
Projects
Status: No status
Status: No status
Development

Successfully merging a pull request may close this issue.

7 participants