parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Http Headers related error with code pulled.

Open madkid66 opened this issue 10 years ago • 69 comments

We pulled parse-server code from master yesterday and while using that code, we notice the following stack trace in log for all the cloud calls being made. Anyone else seeing this? Also, any suggestions, tips for resolving this are appreciated. Thank you.

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at /var/app/current/node_modules/express/lib/application.js:153:9
    at /var/app/current/node_modules/express/lib/router/index.js:137:5
    at /var/app/current/node_modules/express/lib/router/index.js:250:10
    at next (/var/app/current/node_modules/express/lib/router/index.js:160:14)
    at next (/var/app/current/node_modules/express/lib/router/index.js:176:38)
    at Immediate.<anonymous> (/var/app/current/node_modules/express/lib/application.js:190:9)
    at Immediate.<anonymous> (/var/app/current/node_modules/parse-server/node_modules/express/lib/router/index.js:618:15)
    at Immediate.immediate._onImmediate (timers.js:435:18)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

madkid66 avatar Jul 22 '16 18:07 madkid66

I'm noticing this, too. I had done a pull on master branch on 7/12. That had been working fine. When I pulled yesterday and deployed the newer parse server code, I started seeing this error. I suspect it must have been a commit that was introduced between 7/12 and 7/21.

@flovilmart Have you noticed this error, too?

sprabs avatar Jul 22 '16 22:07 sprabs

I haven't noticed it, not sure what would generate it.

flovilmart avatar Jul 23 '16 07:07 flovilmart

It doesn't seem to happen when using parse-server from the CLI, how did you configure/run it? Can you provide more info about the code?

We recently added next handlers (https://github.com/ParsePlatform/parse-server/pull/2338) this is maybe because your setup has a middleware that write the headers after they are written from parse-server

flovilmart avatar Jul 23 '16 08:07 flovilmart

Thank you for the reply Florent.

We are running it on AWS Elastic Beanstalk on a t2.medium and seeing this error in the nodejs logs. The configuration of our environment has not changed over the last 3 weeks and we are running it with verbose set to true. The only thing we changed was we pulled the lasted parse-server code from master.

The error seems to happen any time a cloud code method is invoked. We use promises in our methods. It definitely seems to have been caused by the changes you pointed me to in PromiseRouter.js. Once I removed those changes in my local branch and redeployed, the errors went away. What is the purpose of those changes? Is that something that can be reverted? Or Is there something that our cloud-code needs to do to avoid seeing this error? Please share your thoughts on this.

madkid66 avatar Jul 23 '16 22:07 madkid66

We are running on node chef, Our code is also now breaking. The issue appears that if we make any database queries from server code, cloud function or from express we get the fore mentioned error.

If we call queries from JS client - all works find. If we do anything server side we get the error. We have tried removing our app.js require so that our express set-up is removed, this did not fix the issue.

Can anybody help with this?

at Layer.handle as handle_request at jsonParser (/bundle/node_modules/parse-server/node_modules/body-parser/lib/types/json.js:94:7) at next (/bundle/node_modules/express/lib/router/index.js:271:10) at Function.process_params (/bundle/node_modules/express/lib/router/index.js:330:12) at /bundle/node_modules/express/lib/router/index.js:280:7 at trim_prefix (/bundle/node_modules/express/lib/router/index.js:312:13) at Layer.handle as handle_request at allowCrossDomain (/bundle/index.js:439:9) at ServerResponse.header (/bundle/node_modules/express/lib/response.js:719:10) at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11) Error: Can't set headers after they are sent.

tillifywebb avatar Jul 23 '16 23:07 tillifywebb

Seeing same error as well. AWS ElastBeanstalk, t2.micro, 64bit Amazon Linux 2016.03 v2.1.3, node version 4.4.6 (perhaps that's the issue?).

Errors are seen after running a cloud function, but my app still receives a valid response...

Parse.Cloud.define("getParks", function(request, response){

    if(!request.user){
        response.error();
        return;
    }

    var Parks = Parse.Object.extend("Park");
    var query = new Parse.Query(Parks);
    query.equalTo("Approved", true);

    query.find({useMasterKey: true}).then(function(results) {
        var parks = [];
        ..........
        return parks;
    }).then(function(parks) {
            response.success(parks);
        }, function(error) {
            response.error("Error: " + error.code + " " + error.message);
    });
});

nitrag avatar Jul 24 '16 18:07 nitrag

The fix seem pretty trivial as it looks like it originates from one of our middleware that's run after the cloud functions. As you have the error, can any of you work on a fix?

flovilmart avatar Jul 24 '16 19:07 flovilmart

We have rolled back the last update which has fixed the error. Not sure how we fix it, seems any cloud function throws an error, but response is sent.

tillifywebb avatar Jul 24 '16 20:07 tillifywebb

@madkid66 can you try https://github.com/ParsePlatform/parse-server/pull/2381 's branch and let me know if it fixes?

flovilmart avatar Jul 25 '16 11:07 flovilmart

@flovilmart thank you for the reply. Will try that and update.

madkid66 avatar Jul 25 '16 17:07 madkid66

@flovilmart Thanks for the suggestion. I tried that branch you pointed us to but am still getting this error:

Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11) at /var/app/current/node_modules/express/lib/application.js:153:9 at /var/app/current/node_modules/express/lib/router/index.js:137:5 at /var/app/current/node_modules/express/lib/router/index.js:250:10 at next (/var/app/current/node_modules/express/lib/router/index.js:160:14) at next (/var/app/current/node_modules/express/lib/router/index.js:176:38) at Immediate. (/var/app/current/node_modules/express/lib/application.js:190:9) at Immediate. (/var/app/current/node_modules/parse-server/node_modules/express/lib/router/index.js:618:15) at Immediate.immediate._onImmediate (timers.js:435:18) at processImmediate as _immediateCallback

@tillifywebb @madkid66 Did it work for you both or are you still seeing the issue, too?

sprabs avatar Jul 25 '16 19:07 sprabs

@sprabs We have tried that suggestion but still seeing the issue. Thank you

@flovilmart anything else we can try? Thank you

madkid66 avatar Jul 25 '16 20:07 madkid66

This is related to middleware firing header indiscriminately even though a previous handler had already sent header.

jeffjen avatar Jul 26 '16 09:07 jeffjen

Why was #2338 merged? This seems like a pretty drastic change in behavior for those of us working on getting our self-hosted infrastructure set up.

It appears that now our /parse/classes/:className endpoint is continuing through (not surprising with the addition of a next() into our express app's auth code that's been working fine until 2.2.16 (or 2.2.17)

What's the recommendation here?

woodardj avatar Jul 26 '16 14:07 woodardj

@woodardj it was a bug to not have it, You should always call next() in your express apps. It prevented actions after data was returned to the client. (Eg logging and analytics)

If you are having issues with this, there is more than likely something not quite right in the order of the app.use inside your express application.

The order is very important to app.use

app.use(function(req, res, next) {
    console.log('beforeParse');
    next();
});

app.use('/parse', ParseInstance);

app.use(function(req, res, next) {
    console.log('afterParse');
    next();
});

blacha avatar Jul 26 '16 21:07 blacha

@flovilmart @blacha @woodardj @jeffjen @tillifywebb @sprabs I am trying to understand the flow towards finding a fix for the issue. Since the 'next handlers' were introduced only recently, am assuming it is possible the param 'next' may or may not be passed depending on the code (depending on latest code or not) that is invoking these methods such as handleParseHeaders. I don't see any check (null/undefined for next) in middleware.js before invoking next(). Is it possible that this may the reason for failures we are seeing. Would it help to add if checks before invoking next() everywhere it is being invoked. Please share your thoughts and suggestions.

Thank you

madkid66 avatar Jul 27 '16 00:07 madkid66

@madkid66 Previously parse server was not running next() which broke the flow of express.

express sets up a list of handlers for each URL it handles, it will run that handler then wait for the handler to call next()

given a simple setup like the one in my previous comment the handlers for /parse would look something like: [ beforeParse, Parse, afterParse]

Before this change, Parse would not call next() so afterParse would never run.

The two places I would look into where this problem could occur is in Cloud Functions calling response.success or response.error multiple times, and in the Express app setup.

Something like this would cause issues (you need to return after response.error)

if (error) {
    response.error(error);
}
response.success()

The other area would be inside the express app. Take a look for any app.use that are called after the app.use('/parse', ParseServer)

I haven't had this change break any of my apps (All deployed to ElasticBeanstalk), which would make me think that their might be some inconsistent code in your cloud-functions/express setup.

If anyone has a simple example that breaks, I would love to get my hands on it.

blacha avatar Jul 27 '16 02:07 blacha

@blacha Thank you for the detailed explanation. Will look at our code and get back with any questions.

madkid66 avatar Jul 27 '16 06:07 madkid66

I'm glad we are having an open discussion about this. This is frustrating for my team as we are developing and I would like to get to the bottom of this. Apologies in advance for my long response, but I want to make sure we are on the same page here.

@blacha I'm not sure I understand your explanation about our Cloud Code being the issue, so let me provide details about my situation (that looks like at least four independent people are also complaining about):

  • We are running our parse server on AWS Elastic Beanstalk (t2 medium) with VERBOSE set to 1 in our dev environment. (Looks like you are also running this on EB with also the latest version of the parse-server code, so can we assume the part in the express app is the same for both of us?)
  • The Cloud Code that is deployed to our EB setup is identical to the Cloud Code that is running on parse.com. We have been running this same code (without ANY changes) for over a month (and it's also out in production on parse.com). We have had ZERO issues with it. Some more details about what I've tested/observed:
  1. Despite the errors on EB, the methods are performing exactly as expected. The clients are receiving the right data back and all the operations are being performed without any issues.
  2. I see the error for virtually ANY Cloud Code method we call through our EB endpoint.
  3. The logs are clean on parse.com.
  4. I feel Success/Error handling for Cloud Code is pretty fundamental and obvious to detect if there's a problem there. I am pretty confident that we are handling this correctly. As a sanity test, I intentionally just created a bad Cloud Code method that does this the wrong way and deployed it to parse.com. It gives me a real error that we don't get with our real code.

Based on this analysis (unless I am missing something), I don't think our Cloud Code is the culprit. Also, I think we all already know this, and it's been verified by several others, but when the commit in question is reverted, everything is just fine. We don't see the errors.

Please let me know if there is anything else we should check...

@tillifywebb @madkid66 @woodardj Please feel free to add, if there's anything I missed based on your own deployments. I think we all are in agreement that this needs to be fixed one way or another. Whether it's reverting this change or coming up with a solution on top of this merge...

@flovilmart Obviously, we can't be having these errors in production. It's affecting several of us on the GitHub issue (and probably more who haven't noticed it, yet). Please let us know how you suggest we proceed with this.

sprabs avatar Jul 27 '16 06:07 sprabs

@sprabs Would you be able to take your simplest failing Cloud Code function and remove functionality until it does as little as possible but still reproduces the bug?

AFAIK The backend for Cloud Code on parse.com does not use Express so we will not see this kind of error there. If we know the cause of the issue we can hopefully figure out a way for parse-server to be consistent with parse.com while at the same time being valid Express middleware.

steven-supersolid avatar Jul 27 '16 09:07 steven-supersolid