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 raise and throws in instrument helper #292

Merged
merged 1 commit into from
May 10, 2017

Conversation

tombruijn
Copy link
Member

@tombruijn tombruijn commented May 10, 2017

By using a "soft return", by saving the return value in a variable, it
can happen that an event is not finished when the block that's given to
an instrument method raises and error or uses throw to throw a signal.
Using ensure we ensure that the event is finished even when that
happens.

Reported in

As reported in #289 (comment). Doesn't fix the (potential) parent issue of padrino starting two transactions in certain scenarios, still debugging that.

Notes

@thijsc
Copy link
Member

thijsc commented May 10, 2017

I think this was necessary to actually return the proper value in all scenarios. Doesn't this break some tests?

By using a "soft return", by saving the return value in a variable, it
can happen that an event is not finished when the block that's given to
an instrument method raises and error or uses `throw` to throw a signal.
Using `ensure` we _ensure_ that the event is finished even when that
happens.
@tombruijn tombruijn force-pushed the fix-instrument-helper-with-throw-and-raise branch from 4867eb1 to 792ec18 Compare May 10, 2017 10:26
@tombruijn
Copy link
Member Author

It shouldn't, but let's wait on TravisCI to give the verdict in combination for all gems.

The use of ensure, rather than using a return_value variable, doesn't affect the return value of a block. It allows you to execute code whatever happens beforehand, in this case a raise or a throw.

def foo
  "bar"
ensure
  123
end

foo # => "bar"

returns "bar" as a return value from the method.

See also this spec: https://github.com/appsignal/appsignal-ruby/pull/292/files#diff-0968d4ed9ff8f5b249a2c7e8bc32784bR19

@thijsc
Copy link
Member

thijsc commented May 10, 2017

Ok. If it works that's of course fine. It's just that I remember adding this for a specific scenario that was broken, but not exactly sure what it was.

@thijsc
Copy link
Member

thijsc commented May 10, 2017

Ah I get it. The current implementation is:

    def instrument(name, title = nil, body = nil, body_format = Appsignal::EventFormatter::DEFAULT)
      Appsignal::Transaction.current.start_event
      return_value = yield if block_given?
      Appsignal::Transaction.current.finish_event(name, title, body, body_format)
      return_value
    end

So the new implementation that uses ensure is just a better way of making this behaviour happen.

@tombruijn tombruijn self-assigned this May 10, 2017
@tombruijn tombruijn merged commit a7e1356 into master May 10, 2017
@tombruijn tombruijn deleted the fix-instrument-helper-with-throw-and-raise branch May 10, 2017 13:18
tombruijn added a commit that referenced this pull request Jun 9, 2017
By using a "soft return", by saving the return value in a variable, it
can happen that an event is not finished when the block that's given to
an instrument method raises and error or uses throw to throw a signal.
Using ensure we ensure that the event is finished even when that
happens.

Follow up from #292.
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