sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Undefined hub.startSession on v6 upgrade

Open namoscato opened this issue 5 years ago • 26 comments

  • [x] Review the documentation: https://docs.sentry.io/
  • [x] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
  • [x] Use the latest release: https://github.com/getsentry/sentry-javascript/releases
  • [ ] Provide a link to the affected event from your Sentry account

Package + Version

  • [x] other: @sentry/react

Version:

6.0.4

Description

I'm getting an error when bumping @sentry/react to v6.0.4:

Uncaught TypeError: hub.startSession is not a function
    at startSessionTracking (sdk.ts:221)
    at init (sdk.ts:106)
    at Module.init (sdk.ts:18)
    at Module.<anonymous> (index.tsx:22)
    at Module../src/index.tsx (index.tsx:64)
    at __webpack_require__ (bootstrap:856)
    at fn (bootstrap:150)
    at Object.1 (wdyr.js:11)
    at __webpack_require__ (bootstrap:856)
    at checkDeferredModules (bootstrap:45)
    at Array.webpackJsonpCallback [as push] (bootstrap:32)
    at main.chunk.js:1

Debugging this line, hub indeed does not seem to have a startSession function on its prototype.

Disabling autoSessionTracking fixes the issue, but is this expected?

namoscato avatar Feb 03 '21 02:02 namoscato

Yes, that would do it, if the first one initialized was < 5.27.0

Hm ok. So running two sentry instances is not supported? Where does it conflict?

And you're right - we weren't accounting for the case of a rogue, version-mismatched second Sentry instance, and therefore considered calling hub.startSession in @sentry/browser safe (i.e., we didn't stick it in a try/catch) - as we do calling all sorts of other methods which originate in other @sentry/* packages. That's a whole lot of method calls, and if nothing else, given many people's long-standing concern about about bundle size, wrapping every such call in try/catch isn't feasible.

Have you thought about making a "catch_call" function like this:

const log_error = console.error.bind(console),
catch_call = (fn) => {
      return function (){
        try {
          const result_that_could_be_promise = fn.apply(this, arguments);
          if(typeof result_that_could_be_promise?.catch == "function"){
             return result_that_could_be_promise.catch(log_error);
          }
          return result_that_could_be_promise;
        } catch(e) {
          log_error(e);
        }
      }
    }

Then instead of

try {
function_that_will_fail();
} catch(e){
log_error(e);
}

you just have to do catch_call(function_that_will_fail)().

And really you just need to wrap exposed functions into it.

If you wrap the ones that are executed asynchronously (i.e. through setTimeout or addEventListener) that's a bonus, but those would just land in the console and not interrupt the app which started sentry.

You could even wrap the whole sentry object that sentry.init returns and (therefore all exposed functions) like this:

const unexported_sentry = what_init_returns_now;
const safe_sentry = new Proxy(unexported_sentry, {
  apply(target, thisArg, argumentsList) {
    return catch_call(target).apply(thisArg, argumentsList);
  }
});

Doesn't seem like much bundle size to me.

Everything above untested but it may work.

If this error truly brought your app down, then I apologize. I'm not sure how it would do that, though, and I'd be curious to hear how it did if so.

Yeah it did, but only for around a day.

So basically I'm working for a startup that does AI recommendations. And to lower the hurdle of integrating our recs into their site (which is like the biggest sale-blocker) we often integrate by letting them add a "content script" to their site which then adds recommendations to the page (or replaces them).

And we're using sentry in the script to catch the edge cases where something is not working as expected.

Is it maybe possible to only get captureException, captureMessage and withScope from the sentry sdk and not load the rest? I'm already catching all my errors* with a function similar to the one above since I don't want anything to leak to the console and my error function feeds them into sentry.

*: Except sentry's initialisation, but I just changed that ;) (And of course I probably miss some function calls every now and then but that's life)

danieltroger avatar Feb 19 '21 09:02 danieltroger

Unfortunately, Proxy is not supported in IE, thus we'd need to pull a whole polyfill for that. And it's actually one of the solutions to the ad-blockers issue that we recommend - https://docs.sentry.io/platforms/javascript/troubleshooting/#dealing-with-ad-blockers

kamilogorek avatar Feb 19 '21 09:02 kamilogorek

Yes, that would do it, if the first one initialized was < 5.27.0

Hm ok. So running two sentry instances is not supported? Where does it conflict?

We rely on a global Hub object, which doesn't get overwritten by the second Sentry.init() call. If that call is from 6.x, it tries to start a session, and that's where you run into the problem you're having. What we tell people to do is to create a second hub, rather than calling Sentry.init() again.

That said, we are rethinking the hub/client architecture in version 7, which is being worked on now, and we'll keep this issue in mind as we do.

If this error truly brought your app down, then I apologize. I'm not sure how it would do that, though, and I'd be curious to hear how it did if so.

Yeah it did, but only for around a day.

So basically I'm working for a startup that does AI recommendations. And to lower the hurdle of integrating our recs into their site (which is like the biggest sale-blocker) we often integrate by letting them add a "content script" to their site which then adds recommendations to the page (or replaces them).

And we're using sentry in the script to catch the edge cases where something is not working as expected.

Hmmm. I think I understand your setup, and it does better explain how there might be two Sentry instances, if your client is running Sentry themselves and then you try to run Sentry on top of that. But what I'm not clear about is how it would bring your app (or even your Sentry instance) down. The hub.startSession call is the last thing Sentry.init does, after everything else has already been set up. So, yes, the error would trigger with the version mismatch, but after that, everything should work normally. (I was able to send a test message from the console on https://houdinisportswear.com/sv-se, for example.)

In any case, because you're now the third person with this same problem (albeit for different reasons each), I've put up a PR wrapping that specific call in a check for the existence of hub.startSession.

lobsterkatie avatar Feb 25 '21 02:02 lobsterkatie

We rely on a global Hub object, which doesn't get overwritten by the second Sentry.init() call

That explains it! What is it called though? Hub doesn't seem to be defined on the pages where I had issues (Uncaught ReferenceError: Hub is not defined).

create a second hub

thanks for the link! That's awesome, that's exactly what we need. I'll switch over to setting up our own hub. I am annoyed by the noise that gets through even with whitelisted script urls because of e.g. the setTimeout wrapping, but withScope is worth it.

That said, we are rethinking the hub/client architecture in version 7, which is being worked on now, and we'll keep this issue in mind as we do.

Cool

and it does better explain how there might be two Sentry instances, if your client is running Sentry themselves and then you try to run Sentry on top of tha

Exactly that's the case.

But what I'm not clear about is how it would bring your app (or even your Sentry instance) down.

Because the first thing it does is initializing sentry so that it can catch all further errors and feed them into sentry. But if an uncaught error is thrown then the execution of the script stops.

So, yes, the error would trigger with the version mismatch, but after that, everything should work normally.

It's great that the sentry instance still works, but the error stops the execution of our whole script since nothing else is running yet. Try to append console.log("still alive"); to the code block that I posted above, insert a DSN and paste it into houdini. You won't see the "still alive" being logged.

In any case, because you're now the third person with this same problem (albeit for different reasons each), I've put up a PR wrapping that specific call in a check for the existence of hub.startSession.

Awesome, thanks a lot :)

danieltroger avatar Mar 01 '21 13:03 danieltroger

Hey @lobsterkatie what do I have to do to get back the os/browser/user agent and breadcrumbs? I now have this:

import { BrowserClient, Hub } from "@sentry/browser";

export const setup_sentry () => {
  const client = new BrowserClient({
    dsn: "MY_DSN",
    release: process.env.sentry_version,
  });
  const hub = new Hub(client);
  return hub;
}

But my errors are now super useless, see https://sentry.io/share/issue/b9a6f84e6ff9415d9bdf381aac6b8b84/. And it doesn't event tell me the browser where the error occured:

Screen Shot 2021-03-03 at 11 53 35

danieltroger avatar Mar 03 '21 10:03 danieltroger

@danieltroger, yeah I've been battling the same thing as mentioned above and in https://github.com/getsentry/sentry-javascript/issues/3206.

The latest update from @lobsterkatie over there (thanks!):

having two conflicting Sentry instances (one with the user agent integration and one without) could be the root cause of the problem

I'm still waiting on a new release of my presumably problematic dependency (filestack-js), so I haven't confirmed the @sentry/minimal version bump actually fixes this issue (but it's sounding like it will).

I'll let @lobsterkatie shed some more light on your scenario, though, as it sounds like you don't have control over the version of the other Sentry instance.

namoscato avatar Mar 03 '21 11:03 namoscato