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

Bug 1627841 - Cancel on exceptions in wrapped measurement function #808

Merged
merged 2 commits into from
Apr 8, 2020

Conversation

badboy
Copy link
Member

@badboy badboy commented Apr 7, 2020

Needs iOS tests before merge.

@auto-assign auto-assign bot requested a review from Dexterp37 April 7, 2020 08:45
@badboy badboy requested review from travis79 and removed request for Dexterp37 April 7, 2020 08:45
@badboy badboy force-pushed the 1627841/measure-but-throw branch from 50b55f0 to c8cccaf Compare April 7, 2020 08:49
funcToMeasure()
} catch (e: Exception) {
cancel()
throw e
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should throw exceptions from our APIs. We never do, our APIs are execption safe. We should instead document that we swallow exception and if a different behaviour is required then the product should not use this method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I kinda disagree with that. We're handling user-provided functions (for convenience) and we're merely passing on the behavior of their code (it's not that different from returning their return value).

Copy link
Contributor

Choose a reason for hiding this comment

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

This would be the only API function behaving different, which is something I don't think we should do (... that would be an exception ). We can handle user-defined function, but it doesn't mean it should break consistency with our other APIs.

This doesn't block any other use-case, since the other APIs can still be used.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO it doesn't behave "differently". Glean itself still doesn't produce any exceptions (I see what you did there!).

If users have potentially-throwing functions they need to measure they again need to come up with the exact same wrappper I wrote here instead of using our measure utility.

Copy link
Contributor

Choose a reason for hiding this comment

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

Functions with exceptions can still be measured. To me this behaves differently: we guarantee that our APIs do not throw exceptions. Having one API suddenly behaving differently (even if the exception originates from a user defined function) can be confusing and a source of subtle bugs. Consumers of the API have two (with no exception) have 3 choices:

(1) Catch any exception in the function they need to measure.
(2) Live with the fact that our APIs swallow exceptions.
(3) Not use this utility wrapper.

I'd rather have consistency with the fact that users can expect no exception to come through our APIs than this added flexibility

Copy link
Member

Choose a reason for hiding this comment

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

@travis79 I do like the idea of allowing choosing a behaviour, but I think we should always cancel on exception and only allow to choose whether or not to propagate the exception :)

As long as we default to propagating the exception since that would be the behavior if the function weren't being measured, I am okay with making that configurable within the function. As for whether or not to cancel? My opinion on that is weakly held as I don't have a concrete use case for not cancelling, so I am fine with the cancel on exception behavior also.

Copy link
Contributor

Choose a reason for hiding this comment

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

As long as we default to propagating the exception since that would be the behavior if the function weren't being measured

I think we should definitely cancel, but I'm not sure we should default to "propagate exceptions". See my previous comments :)

Copy link
Member

Choose a reason for hiding this comment

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

I think we should definitely cancel, but I'm not sure we should default to "propagate exceptions". See my previous comments :)

The only problem is that we would be swallowing application exceptions and not Glean exceptions, and I'm not comfortable with that. If a consuming application expected a function to throw in certain cases and we swallow that inside of the measure function, then we are preventing the application from handling its own exceptions. I totally agree that Glean shouldn't cause exceptions in the application, but we shouldn't interfere with an exception raised by a function defined in the application.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should definitely cancel, but I'm not sure we should default to "propagate exceptions". See my previous comments :)

The only problem is that we would be swallowing application exceptions and not Glean exceptions, and I'm not comfortable with that. If a consuming application expected a function to throw in certain cases and we swallow that inside of the measure function, then we are preventing the application from handling its own exceptions.

All right, while I don't agree with this default behaviour, your position is relatively similar to @badboy 's one.

So I'd be fine with taking the initial solution by @badboy with documentation explicitly stating that this might throw if the function throws.

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 realize that I forgot the docs on this function, but there is a sentence on the other function. Will adopt that and then land it.

badboy added 2 commits April 8, 2020 10:46
Swift is more explicit: fallible functions have to be annotated as such (`throws`).
Everything else can't throw.
If someone wants to measure a fallible function (but get the same
cancellation of the timing when thrown) we need another function.

Luckily we can overload functions, so we can keep the same name.
@badboy badboy force-pushed the 1627841/measure-but-throw branch from c8cccaf to 6497c09 Compare April 8, 2020 08:46
@badboy badboy requested a review from Dexterp37 April 8, 2020 08:46
@badboy badboy merged commit b9f56bb into master Apr 8, 2020
@badboy badboy deleted the 1627841/measure-but-throw branch April 8, 2020 09:09
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.

3 participants