As far as I can tell, the issue here is that the `DStream` object is reachable from `transformFunc(r )`, and `DStream.writeObject` generates a runtime exception if it is called from within the closure serializer.