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

fix(utils): Try-catch monkeypatching to handle frozen objects/functions #9031

Merged
merged 3 commits into from
Sep 15, 2023

Conversation

mydea
Copy link
Member

@mydea mydea commented Sep 15, 2023

This try-catches any monkeypatching we do on objects, to avoid us throwing e.g. if trying to patch a frozen object.

Obv. the monkey patching will not actually work then, but I guess it's better to not wrap stuff than to error out in userland. I also added some tests for this!

Fixes #9030

@mydea mydea requested review from lforst and Lms24 September 15, 2023 08:06
@mydea mydea self-assigned this Sep 15, 2023
const proto = original.prototype || {};
wrapped.prototype = original.prototype = proto;
addNonEnumerableProperty(wrapped, '__sentry_original__', original);
} catch (o_O) {} // eslint-disable-line no-empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: I would add a log here (maybe even non-debug!). I can already see myself debugging an issue that some instrumentation isn't working. This is a very opaque way to fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a debug log, but wouldn't do a log. Because it is really not a user problem, the linked issue:

function callback() {
    console.log("Hello");
}
setTimeout(callback, 50); // OK
Object.freeze(callback);
setTimeout(callback, 50); // BOOM!

Is generally totally OK to write but would result in this. the user shouldn't see a log because of this IMHO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug log sounds good!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a debug log & integration tests to actually make sure the issue raised is fixed. Interestingly, if the function if frozen the actual monkey patching still works, only the setting of the wrapped property fails. So the error is still captured even if the callback is frozen, which is good I guess.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 15, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 75.56 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.49 KB (+0.34% 🔺)
@sentry/browser - Webpack (gzipped) 22.09 KB (+0.47% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 70.25 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.59 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped) 20.66 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 222.11 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 86.64 KB (+0.02% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 61.49 KB (+0.03% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.47 KB (0%)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 75.58 KB (+0.06% 🔺)
@sentry/react - Webpack (gzipped) 22.12 KB (+0.47% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 93.47 KB (+0.05% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 51.07 KB (+0.09% 🔺)

@mydea mydea merged commit 042e7ce into develop Sep 15, 2023
81 checks passed
@mydea mydea deleted the fn/fix-wrap-freezed branch September 15, 2023 11:52
billyvg pushed a commit that referenced this pull request Sep 15, 2023
…ns (#9031)

This try-catches any monkeypatching we do on objects, to avoid us
throwing e.g. if trying to patch a frozen object.

Obv. the monkey patching will _not_ actually work then, but I guess it's
better to not wrap stuff than to error out in userland. I also added
some tests for this!

Fixes #9030
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: Attempting to define property on object that is not extensible.
3 participants