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

Closed
3 tasks done
jiangbo0216 opened this issue Aug 19, 2023 · 7 comments
Closed
3 tasks done
Labels
Package: serverless Issues related to the Sentry Serverless SDK Stale

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

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Aug 19, 2023
@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!

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Sep 1, 2023
@getsantry getsantry bot removed the status in GitHub Issues with 👀 Sep 13, 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 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
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Oct 29, 2023
@getsantry getsantry bot removed the status in GitHub Issues with 👀 Nov 2, 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

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 20, 2024
@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.

@getsantry
Copy link

getsantry bot commented Jan 1, 2025

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added the Stale label Jan 1, 2025
@getsantry getsantry bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
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 Stale
Projects
Archived in project
Archived in project
Development

Successfully merging a pull request may close this issue.

8 participants