lazy seq that encounters an exception has differing behavior on repeated use

Description

It seems the below does not compile with 1.8.0 and 1.9.0-alpha14, the same errors appear in both versions.

Patch: 0001-CLJ-2069-cache-exceptions-thrown-during-lazy-seq-rea.patch

Proposal: Cache exceptions thrown during lazy-seq realization, to avoid re-running bodyfn which is declared as `^:once`

Prescreened by: Alex Miller

Environment

OS X EI Capitan, Java HotSpot(TM) 64-Bit Server VM 1.8.0_101-b13

Activity

Show:
import
November 27, 2016, 4:42 PM

Comment made by: find_my_way

Maybe I should use take-while instead of filter.

However, can anyone explain why I get ArithmeticException while running

for the first time and get the right result at the second time?

Nicola Mometto
November 28, 2016, 9:27 AM

The NPE is caused by the interaction between:

  • lazy-seq throwing an exception while realizing part of the sequence

  • lazy-seq internally using ^:once for locals clearing

lazy-seq expects the bodyfn to be run exactly once and then the result to be cached, but if an exception gets thrown during the execution of bodyfn, the function will get run again when the sequence tries to be realized a second time. However if locals clearing has already happened (even partially) this means some locals in bodyfn will now be nil rather than holding their actual value.

Nicola Mometto
November 28, 2016, 9:36 AM

Attached patch that fixes this issue

Assignee

Unassigned

Reporter

import

Labels

Approval

Prescreened

Patch

Code

Affects versions

Priority

Minor
Configure