Skip to content

Repeatedly calling .mapValues can lead to a stack overflow #9499

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

Closed
scabug opened this issue Sep 30, 2015 · 2 comments
Closed

Repeatedly calling .mapValues can lead to a stack overflow #9499

scabug opened this issue Sep 30, 2015 · 2 comments
Assignees

Comments

@scabug
Copy link

scabug commented Sep 30, 2015

The following code throws a stack overflow on the last line:

var m: Map[Int, Double] = Map.empty
(1 until 10000).foreach{i => 
   m = m.mapValues{x => x}
}
m.isEmpty
m.toList

Even if the map is empty, the repeated wrapping of MappedValues causes a stack overflow when the map is finally traversed (and leaks memory up until then). This can happen far away from the map's creation and is very hard to track down. This just caught us in production, essentially bringing down one of our live deployments.

This is related to #4776
but is an example of a runtime error, not just a design issue.

@scabug
Copy link
Author

scabug commented Sep 30, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9499?orig=1
Reporter: Chris Nicholls (Chris_N)
See #4776

@scabug
Copy link
Author

scabug commented Sep 30, 2015

@retronym said:
I would argue that this is really the same bug (albeit from a different perspective) as #4776: had you been aware that the operation was a view, you might have had a better chance at reasoning that it would be stack-unsafe to chain a large number of transformations.

Note that we've got a high performance map (restricted to non-primitive keys) in Scala 2.11 called AnyRefMap that includes a strict operation mapValuesNow. This wasn't lifted into the interface for Map, though.

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

No branches or pull requests

2 participants