how to forcibly unblock from openDataInputStream?

This is an issue I have discovered in the J2MEWTK 1.03. I believe this may be a bug. However, I'd like some peer review before I post to the bug database. The summary is that the HttpConnection can enter a state in which it is impossible to break from a blocking call, even with the assistance of a separate timeout thread.

Consider the following sequence executed by the "worker" thread:

1) client opens connection and sets properties:

HttpConnection connection = ( HttpConnection )Connector.open( url );

connection.setRequestMethod( HttpConnection.POST );

connection.setRequestProperty( "Content-Type", "application/octet-stream" );

2) client gets output stream and writes request:

dos = connection.openDataOutputStream();

dos.write( data );

// close or don't close dos until after read, doesn't matter

3) client tries to open input stream:

dis = connection.openDataInputStream();

4) the misbehaved server or poor network connection delivers an ack for the request but fails to deliver the response. So a separate timeout thread detects the hung connection and attempts to release the worker thread by closing the connection:

connection.close()

However, the worker thread continues to hang on the call to openDataInputStream() and will hang indefinitely until it receives something from the server. If it eventually receives *anything* from the server, the HTTP stack will then honor the close by sending an immediate RST. However, if no packet comes from the server, it will hang "forever." I have verified this behavior by using a packet sniffer and implementing an intentionally misbehaved server and watching for 20min or so.

I'm not aware of any API available in MIDP to help me code around this issue (e.g. Thread.interrupt()).

Any advise on a workaround or opinions on the validity of this proposed defect are welcome.

thanks,

-J

[1978 byte] By [jchalfan] at [2007-9-26 14:49:35]
# 1

Maybe this helps:

public InputStream send(String data) throws TimeoutException{

try {

conn = (StreamConnection) Connector.open("socket://"+server, Connector.READ_WRITE);

outStream = conn.openOutputStream();

outStream.write(data.getBytes());

inStream = conn.openInputStream();

int countdown = TIMEOUT;

long starttime;

do{

starttime = System.currentTimeMillis();

int dataAvailable = inStream.available();

if (dataAvailable > 0) {

return inStream;

} else {

try {

Thread.sleep(50);

} catch (InterruptedException e) {}

}

countdown -= (int)(System.currentTimeMillis() - starttime);

if (countdown <= 0)

throw new TimeoutException();

} while (countdown > 0);

} catch (IOException e) {

close();

e.printStackTrace();

}

return null;

}

In the code above you code catch the TimeoutException and close the InputStream and the Connection.

r1ck at 2007-7-2 16:58:58 > top of Java-index,Java Mobility Forums,Java ME Technologies...