GitHub user ataraxer opened a pull request:

    https://github.com/apache/kafka/pull/2204

    KAFKA-4205; KafkaApis: fix NPE caused by conversion to array

    NPE was caused by `log.logSegments.toArray` resulting in array containing 
`null` values. The exact reason still remains somewhat a mystery to me, but it 
seems that the culprit is `JavaConverters` in combination with concurrent data 
structure access.
    
    Here's a simple code example to prove that:
    ```scala
    import java.util.concurrent.ConcurrentSkipListMap
    // Same as `JavaConversions`, but allows explicit conversions via 
`asScala`/`asJava` methods.
    import scala.collection.JavaConverters._
    
    case object Value
    val m = new ConcurrentSkipListMap[Int, Value.type]
    new Thread { override def run() = { while (true) m.put(9000, Value) } 
}.start()
    new Thread { override def run() = { while (true) m.remove(9000) } }.start()
    new Thread { override def run() = { while (true) { 
println(m.values.asScala.toArray.headOption) } } }.start()
    ```
    
    Running the example will occasionally print `Some(null)` indicating that 
there's something shady going on during `toArray` conversion.
    
    `null`s magically disappear by making the following change:
    ```diff
    - println(m.values.asScala.toArray.headOption)
    + println(m.values.asScala.toSeq.headOption)
    ```

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ataraxer/kafka KAFKA-4205

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/kafka/pull/2204.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #2204
    
----
commit bcd32760015e9dfd564813076a07dbe1612eab00
Author: Anton Karamanov <atara...@yandex-team.ru>
Date:   2016-12-02T14:37:42Z

    KAFKA-4205; KafkaApis: fix NPE caused by conversion to array

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to