[
https://issues.apache.org/jira/browse/GROOVY-12677?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18137471#comment-18137471
]
ASF GitHub Bot commented on GROOVY-12677:
-----------------------------------------
codeconsole opened a new pull request, #3198:
URL: https://github.com/apache/groovy/pull/3198
A `memoize()` closure stores a null result in its cache as the private
place-holder `Memoize.MemoizeNullValue`. That class is not `Serializable`, so
the memoized closure cannot be serialized once it has cached a null:
```groovy
def m = { x -> null }.dehydrate().memoize()
m(1)
new ByteArrayOutputStream().withObjectOutputStream { it.writeObject(m) }
// java.io.NotSerializableException:
org.codehaus.groovy.runtime.memoize.Memoize$MemoizeNullValue
```
Seen in 4.0.33, 5.0.8 and 6.0.0 too.
`MemoizeNullValue` now implements `Serializable`. That alone is not enough:
`MemoizeFunction.call` compares a cached value with the `MEMOIZE_NULL`
singleton by identity, so a deserialized copy would come back from the cache as
the place-holder object rather than null. `MemoizeNullValue.readResolve`
therefore returns the singleton.
With #3195 (GROOVY-12674), which makes `memoizeAtMost` closures serializable
with their entries, this also keeps a cached null there; the soft-reference
variants drop their cache on serialization under #3195 and are not affected.
Merges cleanly with #3190, #3194 and #3195.
## Tests
`MemoizeTest.testCachedNullResultSurvivesSerialization` round-trips a
memoized closure that has cached a null and checks that the copy returns null
for the cached argument without calling the wrapped closure again. It fails
without the change with the `NotSerializableException`, and fails with the
place-holder returned instead of null when only `readResolve` is left out.
`org.codehaus.groovy.runtime.memoize.*`, `groovy.lang.*Closure*`,
`groovy.*Memoize*` and `org.codehaus.groovy.transform.Memoized*` pass. The
Checkstyle and CodeNarc gates pass.
> A memoize() closure that has cached a null result cannot be serialized
> ----------------------------------------------------------------------
>
> Key: GROOVY-12677
> URL: https://issues.apache.org/jira/browse/GROOVY-12677
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 7.0.0-alpha-1, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> def m = { x -> null }.dehydrate().memoize()
> assert m(1) == null
> def bytes = new ByteArrayOutputStream()
> bytes.withObjectOutputStream { it.writeObject(m) } // fails here
> def copy = new
> ByteArrayInputStream(bytes.toByteArray()).withObjectInputStream(getClass().classLoader)
> { it.readObject() }
> assert copy(1) == null
> {code}
> *Actual:* {{java.io.NotSerializableException:
> org.codehaus.groovy.runtime.memoize.Memoize$MemoizeNullValue}}. The same
> closure serializes when its cached results are not null.
> *Expected:* the memoized closure serializes. The copy returns null for the
> cached arguments without calling the wrapped closure again.
> The memoizing closure stores a null result as a private place-holder,
> {{Memoize.MemoizeNullValue}}, which is not {{Serializable}}. The closure
> recognises the place-holder by identity, so a deserialized copy must also
> resolve back to the shared instance.
> Verified on 4.0.33, 5.0.8, 6.0.0 and master.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)