andreasgal/j2me.js — pull request #364 by mykmelez (2014-09-30 00:04:37+00:00)
Title: return -1 early when Protocol.read0 called on closed socket

I'm not sure this is quite right. In particular, I see some strange log output from the SSLStreamConnection instrumentation: on desktop, the body of the HTTP response appears twice; while on device, a bunch of null characters sometimes appears after the body.

Nevertheless, this does appear to fix a recent regression in a midlet that uses a socket connection: the midlet blocks on a call to read0 that occurs after the socket connection has been closed.

And this is at least reasonable, since the description of *read0* says it should return "the total number of bytes read into the buffer, or `-1` if there is no more data because the end of the stream has been reached."

Perhaps the underlying bug is that we don't notify the midlet when the socket closes. Although I'm unsure why this is happening now when it didn't happen last week, as I don't think this code has changed. Maybe it's a race condition between socket closure and *read0* which has been exposed by the recent improvements to overall perf.