Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Prevent possible infinite recursion #2128

Merged
merged 2 commits into from
Dec 10, 2015

Conversation

JonRowe
Copy link
Member

@JonRowe JonRowe commented Dec 7, 2015

Fixes #2127

@seanwalbran
Copy link

@JonRowe Just a note that this doesn't catch cause loops of more than one exception (e.g. when error.cause != error, but error.cause.cause == error), which I happened to run into with rails+haml: https://gist.github.com/seanwalbran/b63fbe7117645c5ddef1#file-gistfile1-txt-L26

@myronmarston
Copy link
Member

Thanks for reporting that, @seanwalbran!

It sounds like the fix needs to be more sophisticated -- either it needs to keep track of all exceptions seen so far (e.g. via an extra arg passed as part of the recursion) so it can notice when it's seeing one it has already seeing, or maybe we should just put a hard limit on the max depth (e.g. 10 or 20 or something) and give up when we've followed Exception#cause that many times.

@JonRowe JonRowe force-pushed the prevent_loop_when_cause_is_exception branch 2 times, most recently from 849edd0 to c374ec5 Compare December 9, 2015 00:41
@JonRowe
Copy link
Member Author

JonRowe commented Dec 9, 2015

I've reworked this to take @seanwalbran's issue into account

def final_exception(exception, previous=[])
if exception.cause && !previous.include?(exception.cause)
previous << exception.cause
final_exception(exception.cause, previous)
Copy link
Member

Choose a reason for hiding this comment

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

It'll probably be slightly faster if you call cause only once:

def final_exception(exception, previous=[])
  cause = exception.cause
  return exception unless cause
  return exception if previous.include?(cause)

  previous << cause
  final_exception(cause, previous)
end

Not a merge blocker, though.

@myronmarston
Copy link
Member

LGTM otherwise.

@JonRowe JonRowe force-pushed the prevent_loop_when_cause_is_exception branch from c374ec5 to 558cd49 Compare December 10, 2015 03:52
JonRowe added a commit that referenced this pull request Dec 10, 2015
@JonRowe JonRowe merged commit 43de491 into master Dec 10, 2015
@JonRowe JonRowe deleted the prevent_loop_when_cause_is_exception branch December 10, 2015 05:14
@joeletizia
Copy link

Any chance of a 3.5 release for this this?

@JonRowe
Copy link
Member Author

JonRowe commented Dec 24, 2015

We could put out a patch release of 3.4 (@samphippen might need your help)

@taylorzr
Copy link

I ran into this issue as well, any chance this will be released as a patch to 3.4?

@JonRowe
Copy link
Member Author

JonRowe commented Feb 18, 2016

Hmm apparently I forgot to backport this so it was never released in recent patch versions, something I've just rectified with 3.4.3.

@dgilperez
Copy link

I just got hit by this at 3.4.4 - expected?

@JonRowe
Copy link
Member Author

JonRowe commented Jul 1, 2016

This should have been rectified in 3.4.4 can you provide more details?

@dgilperez
Copy link

I'm sorry, I somehow worked around it and can't reproduce it now.

MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
…xception

Prevent possible infinite recursion
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants