Headers Already Sent When Page Reloads

39 views
Skip to first unread message

Caleb Beery

unread,
Aug 2, 2022, 12:09:16 PM8/2/22
to LoopbackJS
I've got a separate front end application that is calling my Loopback 4 API endpoints. When the front end application's page is refreshed too quickly, my Loopback 4 API produces this error:

Error: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (/Users/caleb.beery/sites/trimaster-lb4/node_modules/express/lib/response.js:794:10)
    at HttpContext.done (/Users/caleb.beery/sites/trimaster-lb4/node_modules/strong-remoting/lib/http-context.js:580:9)
    at /Users/caleb.beery/sites/trimaster-lb4/node_modules/strong-remoting/lib/rest-adapter.js:540:11
    at /Users/caleb.beery/sites/trimaster-lb4/node_modules/async/dist/async.js:3681:19
    at wrapper (/Users/caleb.beery/sites/trimaster-lb4/node_modules/async/dist/async.js:271:20)
    at replenish (/Users/caleb.beery/sites/trimaster-lb4/node_modules/async/dist/async.js:441:29)
    at iterateeCallback (/Users/caleb.beery/sites/trimaster-lb4/node_modules/async/dist/async.js:430:21)
    at /Users/caleb.beery/sites/trimaster-lb4/node_modules/async/dist/async.js:327:20
    at /Users/caleb.beery/sites/trimaster-lb4/node_modules/async/dist/async.js:3679:17
    at interceptInvocationErrors (/Users/caleb.beery/sites/trimaster-lb4/node_modules/strong-remoting/lib/remote-objects.js:731:22)
    at /Users/caleb.beery/sites/trimaster-lb4/node_modules/loopback-phase/node_modules/async/dist/async.js:473:16
    at replenish (/Users/caleb.beery/sites/trimaster-lb4/node_modules/loopback-phase/node_modules/async/dist/async.js:1009:25)
    at iterateeCallback (/Users/caleb.beery/sites/trimaster-lb4/node_modules/loopback-phase/node_modules/async/dist/async.js:998:17)
    at /Users/caleb.beery/sites/trimaster-lb4/node_modules/loopback-phase/node_modules/async/dist/async.js:972:16


Some information about the Loopback 4 app: 
This contains a partially migrated Loopback 3 application in the "lb3app" directory in the root of the project. Some of the front end application's API calls are hitting the Loopback 4 endpoints and some of them are hitting the Loopback 3 endpoints.

I'm not really even sure how to start debugging this. Any suggestions?

Caleb Beery

unread,
Aug 2, 2022, 12:55:08 PM8/2/22
to LoopbackJS
It looks like this is a similar issue with no resolution:

https://github.com/loopbackio/loopback-next/discussions/7196

Caleb Beery

unread,
Aug 2, 2022, 2:03:55 PM8/2/22
to LoopbackJS
Adding this to my application seems to keep the node process from crashing, allowing pages to refresh with no issues. Not sure this is a good idea, though.

process.on('uncaughtException', (reason, promise) => {
   console.log('uncaughtException at:', reason.stack ?? reason);
});

Rifa Achrinza

unread,
Aug 5, 2022, 11:46:04 PM8/5/22
to loopb...@googlegroups.com
Hi Caleb Beery,

Based on the error stack trace, the frontend app is using the Strong
Remoting feature of LB3.

Would it be possible to provide some steps and/or a minimal PoC repo to consistently replicate the issue?
> --
> You received this message because you are subscribed to the Google
> Groups "LoopbackJS" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]loopbackjs+...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/loopbackjs/e4844160-39ca-498a-9aaa
> -ffad72d73350n%40googlegroups.com.
>
>References
>
> 1. mailto:loopbackjs+...@googlegroups.com
> 2. https://groups.google.com/d/msgid/loopbackjs/e4844160-39ca-498a...@googlegroups.com?utm_medium=email&utm_source=footer
attachment.sig

Caleb Beery

unread,
Aug 6, 2022, 2:42:24 PM8/6/22
to LoopbackJS
Hi Rifa,

This post documents the issue I was having. They also provide an example repo and a change you can make to reproduce the issue.

These are the conditions needed to reproduce:
1. A Loopback 3 app is contained in a Loopback 4 app, in the "lb3app" directory.
2. One of the Loopback 3 models contains a remote method that uses a callback (not async/await)
3. The remote method takes some time to run, and before it returns, the request is cancelled (front end that calls it is reloaded, for example)
4. Error

Rifa Achrinza

unread,
Aug 7, 2022, 8:47:40 AM8/7/22
to loopb...@googlegroups.com
Hi Caleb,

Thanks for the additional info! As I wasn't able to immediately find the
root cause, I've went ahead and opened it as a bug in our issue
tracker and added it to our icebox for the time being. If you are
able to find a patch, please do submit it as a PR on the `loopback-next`
Git repository; The maintainers will help with reviewing and landing the
PR.

Best regards,
Rifa
> > an email to [1][1]loopbackjs+...@googlegroups.com.
> > To view this discussion on the web visit
> >
> [2][2]https://groups.google.com/d/msgid/loopbackjs/e4844160-39ca-498
> a-9aaa
> > -ffad72d73350n%[3]40googlegroups.com.
> >
> >References
> >
> > 1. mailto:[4]loopbackjs+...@googlegroups.com
> > 2.
> [5]https://groups.google.com/d/msgid/loopbackjs/e4844160-39ca-498a..
> .@googlegroups.com?utm_medium=email&utm_source=footer
>
> --
> You received this message because you are subscribed to the Google
> Groups "LoopbackJS" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [6]loopbackjs+...@googlegroups.com.
> To view this discussion on the web visit
> [7]https://groups.google.com/d/msgid/loopbackjs/f0405d7b-4ea4-4e58-90ad
> -321c72d45a5dn%40googlegroups.com.
>
>References
>
> 1. file:///tmp/mutt.html
> 2. https://groups.google.com/d/msgid/loopbackjs/e4844160-39ca-498a-9aaa
> 3. http://40googlegroups.com/
> 4. file:///tmp/mutt.html
> 5. https://groups.google.com/d/msgid/loopbackjs/e4844160-39ca-498a...@googlegroups.com?utm_medium=email&utm_source=footer
> 6. mailto:loopbackjs+...@googlegroups.com
> 7. https://groups.google.com/d/msgid/loopbackjs/f0405d7b-4ea4-4e58...@googlegroups.com?utm_medium=email&utm_source=footer
attachment.sig
Reply all
Reply to author
Forward
0 new messages