Ghost: [Docker] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Welcome 👋
Please keep in mind that GitHub is our office. We invite you to share any bug reports here 🐛…
For anything else please use https://forum.ghost.org 👫

Issue Summary

Sometimes, my docker install of Ghost crashes and restarts the container. The docker logs show this error:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Followed by this output:

    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/var/lib/ghost/versions/4.11.0/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/var/lib/ghost/versions/4.11.0/node_modules/express/lib/response.js:170:12)
    at /var/lib/ghost/versions/4.11.0/core/server/web/shared/middlewares/error-handler.js:200:32
    at /var/lib/ghost/versions/4.11.0/node_modules/express-hbs/lib/hbs.js:635:23
    at /var/lib/ghost/versions/4.11.0/node_modules/express-hbs/lib/resolver.js:29:5
    at tryCatcher (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/var/lib/ghost/versions/4.11.0/node_modules/bluebird/js/release/async.js:15:14)
    at processImmediate (internal/timers.js:464:21)

After that, the service is restarted and it functions fine again.

To Reproduce

  1. Setup Ghost using official Docker image
  2. Run it with docker-compose that restarts the container on failure.
  3. At some point the user gets a half loading page on the first visit. Refreshing with F5 gives a 404 error.
  4. After a few minutes you can press F5 again and Ghost will be back up and running.

Technical details:

  • Ghost Version: 4.11.0 (docker)
  • Node Version: What is in the 4.11.0 docker container
  • Browser/OS: Server runs Manjaro ARM with Docker. Client runs Manjaro x64 using Firefox.
  • Database: What is in the 4.11.0 docker container.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (2 by maintainers)

Most upvoted comments

I have EXACTLY the same issue. Haven’t found any solutions to it yet. Probably will switch away from Ghost soon too because of this. Would be better to find a solution though.

Posted about it on the Ghost Forums too, but got 0 replies: https://forum.ghost.org/t/cannot-set-headers-site-only-loads-properly-on-third-refresh/25011

I just today updated my containers to 4.15 4.16 4.17 4.18 and can confirm that the big still exists, with the same error log as in the initial issue. Upon loading the home page, the images fail to load, and after hitting „reload“, the „site is under maintenance“ message appears.

I have noticed however that it seems to happen more often in the official Casper theme than in other themes, this could be a coincidence though. Edit: This was a coincidence. I’ve switched to the Alto theme and the issue persists. It probably only happened on that site because it has more traffic. I’ll probably just switch away from using Ghost.

Also, here is the current log
[2021-10-12 19:02:50] INFO Ghost server started in 0.362s
[2021-10-12 19:02:50] INFO Database is in a ready state.
[2021-10-12 19:02:50] INFO Ghost database ready in 0.75s
[2021-10-12 19:02:53] INFO Ghost booted in 3.641s
[2021-10-12 19:02:53] INFO Adding offloaded job to the queue
[2021-10-12 19:02:53] INFO Scheduling job update-check at 15 0 13 * * *. Next run on: Wed Oct 13 2021 13:00:15 GMT+0000 (Coordinated Universal Time)
[2021-10-12 19:02:54] INFO "GET /favicon.ico" 200 5ms
[2021-10-12 19:02:54] INFO "GET /" 200 427ms
[2021-10-12 19:02:54] INFO "GET /assets/built/screen.css?v=c5726d68cc" 200 4ms
[2021-10-12 19:02:54] INFO "GET /assets/built/main.min.js?v=c5726d68cc" 200 4ms
[2021-10-12 19:02:54] INFO "GET /content/images/size/w150/2021/08/3uPfmGu1_400x400.jpg" 302 5ms
[2021-10-12 19:02:54] INFO "GET /content/images/size/w1140/2021/09/brandon-romanchuk-fBPYXUAUAmI-unsplash.jpg" 302 9ms
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (internal/errors.js:322:7)
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/var/lib/ghost/versions/4.18.0/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/var/lib/ghost/versions/4.18.0/node_modules/express/lib/response.js:170:12)
    at /var/lib/ghost/versions/4.18.0/core/server/web/shared/middlewares/error-handler.js:240:32
    at /var/lib/ghost/versions/4.18.0/node_modules/express-hbs/lib/hbs.js:635:23
    at /var/lib/ghost/versions/4.18.0/node_modules/express-hbs/lib/resolver.js:29:5
    at tryCatcher (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:15:14)
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (internal/errors.js:322:7)
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/var/lib/ghost/versions/4.18.0/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/var/lib/ghost/versions/4.18.0/node_modules/express/lib/response.js:170:12)
    at /var/lib/ghost/versions/4.18.0/core/server/web/shared/middlewares/error-handler.js:240:32
    at /var/lib/ghost/versions/4.18.0/node_modules/express-hbs/lib/hbs.js:635:23
    at /var/lib/ghost/versions/4.18.0/node_modules/express-hbs/lib/resolver.js:29:5
    at tryCatcher (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate](/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:15:14)
[2021-10-12 19:02:54] INFO "GET /content/images/2021/08/3uPfmGu1_400x400.jpg" 200 2ms
[2021-10-12 19:02:54] INFO "GET /content/images/2021/09/brandon-romanchuk-fBPYXUAUAmI-unsplash.jpg" 200 2ms
[2021-10-12 19:02:55] INFO "GET /content/images/size/w1140/2021/09/image0.jpg" 302 3ms
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (internal/errors.js:322:7)
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/var/lib/ghost/versions/4.18.0/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/var/lib/ghost/versions/4.18.0/node_modules/express/lib/response.js:170:12)
    at /var/lib/ghost/versions/4.18.0/core/server/web/shared/middlewares/error-handler.js:240:32
    at /var/lib/ghost/versions/4.18.0/node_modules/express-hbs/lib/hbs.js:635:23
    at /var/lib/ghost/versions/4.18.0/node_modules/express-hbs/lib/resolver.js:29:5
    at tryCatcher (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/var/lib/ghost/versions/4.18.0/node_modules/bluebird/js/release/async.js:15:14)

Interestingly, it does not crash the container every time, but about every third to fourth time… Which still is multiple times a day.

~On my servers, while this bug still persists after the latest update for the container (4.15), it doesn’t lead to the container crashing anymore. Only the log message is still there.~

Sadly this is not true anymore, I just had a lucky streak. Version 4.15 and 4.16 in Docker still display this bug.

The error is in my log docker log <container-id>.

I haven’t had it happen in a while though (now running 4.16). When it does happen, the main page will not be loading any pictures properly. And hitting F5 just after that, results in a Bad Gateway error, because the container is rebooting. When the container is done rebooting, everything works fine.