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

Error: Can't set headers after they are sent #471

Closed
joncursi opened this issue Jul 19, 2017 · 19 comments
Closed

Error: Can't set headers after they are sent #471

joncursi opened this issue Jul 19, 2017 · 19 comments

Comments

@joncursi
Copy link

Every time I make a graphql query from my client, the server console outputs:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 32): Error: Can't set headers after they are sent.

The data arrives fine on the client, but I keep getting this error spit out on the server console. It only happens on graphql queries (I'm using Apollo client and Apollo server). I'm not sure where to start since there is no stack trace, so I figured I would start here and see if anyone has any ideas on what's going on!

    "graphql": "0.10.5",
    "graphql-date": "^1.0.3",
    "graphql-loader": "^1.2.1",
    "graphql-log": "^0.1.3",
    "graphql-redis-subscriptions": "^1.2.0",
    "graphql-server-express": "^1.0.2",
    "graphql-subscriptions": "^0.4.4",
    "graphql-tools": "^1.0.0",
    "graphql-type-json": "^0.1.4",

My server is a Meteor app @ [email protected] and using the meteor/apollo integration, so the graphql server is based on express.

@cfnelson
Copy link

@joncursi I was having a similar issue Error: Can't set headers after they are sent. after clearing my node_modules and reinstalling.

I was able to resolve it by installing "graphql-server-express": "1.0.0",. Something is breaking on "graphql-server-express": "1.0.2", for me.

@mklopets
Copy link
Contributor

Can you provide a reduced test case for your setup that throws this error?

@VladDubrovskis
Copy link

VladDubrovskis commented Jul 20, 2017

We run into similar issue and rolled back to v1.0.0 for now. It seems to be caused by this change:
3d03295#diff-93774063e5fc49e60474cd9b82249731

Our use case that was breaking, is that we had a 404 middleware, however as res.end() was called by GraphQL, the 404 middleware was throwing error.

In theory, any middleware that you try to apply to all requests that might be modifying headers will break

@ArnaudRinquin
Copy link

Our app spits the same error after upgrading to 1.0.2 but, surprisingly, it does not break anything.

Here is the stack-trace:

Error: Can't set headers after they are sent.
  at validateHeader (_http_outgoing.js:504:11)
  at ServerResponse.setHeader (_http_outgoing.js:511:3)
  at ServerResponse.header (/tmp/node_modules/express/lib/response.js:719:10)
  at ServerResponse.send (/tmp/node_modules/express/lib/response.js:164:12)
  at ServerResponse.json (/tmp/node_modules/express/lib/response.js:250:15)
  at ServerResponse.send (/tmp/node_modules/express/lib/response.js:152:21)
  at /app/src/app.js:82:9
  at Layer.handle_error (/tmp/node_modules/express/lib/router/layer.js:71:5)
  at trim_prefix (/tmp/node_modules/express/lib/router/index.js:310:13)
  at /tmp/node_modules/express/lib/router/index.js:280:7
  at Function.process_params (/tmp/node_modules/express/lib/router/index.js:330:12)
  at next (/tmp/node_modules/express/lib/router/index.js:271:10)
  at /app/src/app.js:75:5
  at Layer.handle [as handle_request] (/tmp/node_modules/express/lib/router/layer.js:95:5)
  at trim_prefix (/tmp/node_modules/express/lib/router/index.js:312:13)
  at /tmp/node_modules/express/lib/router/index.js:280:7
  at Function.process_params (/tmp/node_modules/express/lib/router/index.js:330:12)
  at next (/tmp/node_modules/express/lib/router/index.js:271:10)
  at /tmp/node_modules/express/lib/router/index.js:618:15
  at next (/tmp/node_modules/express/lib/router/index.js:256:14)
  at /tmp/node_modules/apollo-server-express/dist/expressApollo.js:22:13
  at <anonymous>

Meaningful versions:

body-parser: "1.15.2"
dataloader: "^1.3.0"
debug: "^2.2.0"
express: "4.14.0"
express-graphql: "^0.6.6"
graphql: "^0.10.5"
graphql-redis-subscriptions-async-iterator: "^1.2.0"
graphql-server-express: "^1.0.0"
graphql-subscriptions: "^0.4.4"
graphql-tools: "^1.1.0"
graphql-type-json: "^0.1.4"
morgan: "1.7.0"
optics-agent: "^1.1.6"
request: "2.77.0"
subscriptions-transport-ws: "^0.8.1"

@goldo
Copy link

goldo commented Jul 21, 2017

same for us here ! Thanks the tip

@Friss
Copy link

Friss commented Jul 22, 2017

Here's a small example of the error. Any middleware after your GraphQL endpoint will be run, such as 404 / error handlers. These handlers are also generated as part of the express generator.

https://gist.github.com/Friss/90604b5f53ad4bb38146a8f49c730c13

@freiksenet
Copy link
Contributor

We've reverted the patch that caused the problems. I'm very sorry for any inconvinience!

@frederikhors
Copy link

The problem is still there: #1523

Any hint?

@LazerJesus
Copy link

+1

@jakec-dev
Copy link

+1 same issue here

@dmitrijs-pavlovs-dev
Copy link

Same here ericmakesapps/graphql-passport#36

@gaurav-bothra
Copy link

+1 same issue here
#4527

@marcoantoni0
Copy link

+1

@grantmontgomery
Copy link

Same issue too I'm trying to write a "Set-Cookie" header in my response object after logging in a user.

@alicerocheman
Copy link

alicerocheman commented Feb 25, 2022

Still encountering this issue.
The app works, but my server console keeps giving me this error.
Specifically:

[1] error - Error: write EPIPE
[1] error - Error: read ECONNRESET
[1] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[1]     at new NodeError (node:internal/errors:371:5)
[1]     at ServerResponse.setHeader (node:_http_outgoing:576:11)
[1]     at NodeNextResponse.setHeader (/Users/username/Dev/project/front/app/node_modules/next/dist/server/base-http/node.js:56:19)
[1]     at DevServer.renderError (/Users/username/Dev/project/front/app/node_modules/next/dist/server/base-server.js:1134:17)
[1]     at DevServer.renderError (/Users/username/Dev/project/front/app/node_modules/next/dist/server/next-server.js:493:22)
[1]     at DevServer.run (/Users/username/Dev/project/front/app/node_modules/next/dist/server/dev/next-dev-server.js:452:35)
[1]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[1]     at async DevServer.handleRequest (/Users/username/Dev/project/front/app/node_modules/next/dist/server/base-server.js:307:20) {
[1]   code: 'ERR_HTTP_HEADERS_SENT'
[1] }

conf:

    "@apollo/client": "^3.5.9",
    "next": "^12.1.0",

@glasser
Copy link
Member

glasser commented Feb 25, 2022

@alicerocheman Sorry you're running into this, but we won't be able to help without a full reproduction recipe that lets us see it ourselves without any creativity (eg, something starting with git clone or using codesandbox.io). It's probably a subtle interplay between NextJS, Apollo Server, and your own app code.

@leonard-henriquez
Copy link

leonard-henriquez commented Nov 8, 2022

I faced this issue and I found various examples of this error.
All those examples use Apollo server but they use different frameworks.
So, could this issue come from Apollo Server ?

Is this enough to investigate this issue @glasser ?

NB: the snipped provided by @porcellus on the SuperTokens issue offer a solution to this problem in their specific use case. Could it be used to solve this more general issue ?

@glasser
Copy link
Member

glasser commented Nov 9, 2022

No, we really need to be given an example that we can run without creativity and see exactly what's happening. (Many of the ones you have linked are from very old versions of Apollo Server — the first one is from Apollo Server v1, I think. The entire way that AS connects to web frameworks has been reworked multiple times since then; if the issue can't be reproduced in AS v4 then there's not much we're going to do.)

It sounds like the common thread involves doing some sort of error handling that sends a response; perhaps the error handling is firing in cases where the response has already been sent?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests