Re: [xSocket-develop] suspendRead() and resumeRead() cause program hang?
Status: Inactive
Brought to you by:
grro
|
From: fulichen <ful...@du...> - 2008-04-22 08:03:06
|
Hi Gregor,
Thanks for your quickly response, and we find the bug occur from the race condition among IoSocketHandler's 3 methods(onWriteableEvent(), suspendRead(), resumeRead()), could we synchronized these 3 methods to resolve this probem like the following code? (but this approach maybe loose some performace)
fchen.
====================================================================
Object suspendReadLocker = new Object(); // new data member for suspendRead
int onWriteableEvent() throws IOException {
assert (IoSocketDispatcher.isDispatcherThread());
int sent = 0;
synchronized(suspendReadLocker) { // synchronized with suspendReadLocker
if (suspendRead) {
if (LOG.isLoggable(Level.FINEST)) {
LOG.finest("[" + getId() + "] writeable event occured. update interested to none (because suspendRead is set) and write data to socket");
}
updateInterestedSetNonen();
} else {
// if (LOG.isLoggable(Level.FINEST)) {
// LOG.finest("[" + getId() + "] writeable event occured. update interested to read and write data to socket");
// }
updateInterestedSetRead();
}
}
...
...
}
public void suspendRead() throws IOException {
synchronized(suspendReadLocker) { // synchronized with suspendReadLocker
suspendRead = true;
// update to write (why?). Reason:
// * avoid race conditions in which current write need will be swallowed
// * write falls back to `none interested set`
updateInterestedSetWrite();
}
}
public void resumeRead() throws IOException {
synchronized(suspendReadLocker) { // synchronized with suspendReadLocker
if (suspendRead) {
suspendRead = false;
// update to write (why not read?). Reason:
// * avoid race conditions in which current write need will be swallowed
// * write falls back to `read interested set` if there is no data to write
updateInterestedSetWrite();
}
}
}
----- Original Message -----
From: <gre...@we...>
To: <xso...@li...>
Cc: "fulichen" <ful...@du...>
Sent: Tuesday, April 22, 2008 3:10 PM
Subject: Re: [xSocket-develop] suspendRead() and resumeRead() cause program hang?
Hi fchen,
the problem here is, that the read buffer size will be managed by the application specific app handler (worker thread) by using the suspend an resume read methods. The socket will be read by a dedicated IO-Thread (Dispacher-Thread). For this reason race conditions exists, which can cause memory problems in your use case.
My suggested approach doesnt work good to limit the read buffer size. Sorry. The suspend and resume read method hasnt been introduced for such a use case.
I introduced a new parameter MaxReadBufferThreshold. This parameter will be managed by calling the <connection>.setMaxReadBufferThreshold(...) method. If the xSocket connections internal app-level read buffer exceeds this size the connection will stop reading. This will be controlled within the IO-Thread. Please note this is a threshold value. By exceeding this threshold the read will be stopped, which means the read buffer size will exceed this threshold (max size = maxReadBufferThreshold + socket read buffer size * 2).
For an example see http://xsocket.svn.sourceforge.net/viewvc/xsocket/xsocket/core/trunk/src/test/java/org/xsocket/connection/LimitedReadTest.java?view=markup. I also attached a bin jar of the current snapshot.
Gregor
> -----Ursprüngliche Nachricht-----
> Von: "fulichen" <ful...@du...>
> Gesendet: 22.04.08 03:57:22
> An: <xso...@li...>
> Betreff: [xSocket-develop] suspendRead() and resumeRead() cause program hang?
> Hello Gregor,
>
> Thanks for your response, but we find suspendRead() and resumeRead()
> will cause program hang, is it a bug (maybe there is race condition)?
> or how can we fix our test?
>
> The test program is following, and the test environment Java version
> 1.6.0_03 on Windows Vista.
> TestClient result hanging a while after start (but not each
> TestClient test, try some times will get hang)
>
> =================================
> public class TestClient implements IDataHandler
> {
> //-------------------------------
> static public void main(String[] args)throws IOException, InterruptedE
> xception
> {
> TestClient c = new TestClient();
> NonBlockingConnection nbc = new NonBlockingConnection("127.0.0.1",
> 1000, c);
> Thread.sleep(100000000);
> }
> public boolean onData(INonBlockingConnection connection)throws
> IOException
> {
> System.out.println("onData enter");
> try
> {
> connection.suspendRead(); // here call to suspendRead
> int available = connection.available();
> System.out.println("onData available = " + available);
> byte[] data = connection.readBytesByLength(available);
> }
> finally
> {
> connection.resumeRead(); // call resumeRead here
> System.out.println("onData exit");
> }
> return true;
> }
> }
> =================================
> public class TestServer
> {
> //-------------------------------
> static public void main(String[] args)throws IOException
> {
> TestServer s = new TestServer();
> s.doServer();
> }
> void doServer() throws IOException
> {
> ServerSocket ssck = new ServerSocket(1000);
> Socket sck = ssck.accept();
> OutputStream out = new BufferedOutputStream(sck.getOutputStream());
> for (int i = 0; i < 100000000; i++)
> {
> String s = "Message " + i + "\r\n";
> //1.write out a string
> out.write(s.getBytes());
> //2.write out bytes length
> ByteBuffer buffer = ByteBuffer.allocate(4);
> buffer.putInt(i);
> buffer.rewind();
> out.write(buffer.getInt());
> //3.write out bytes
> byte[] data = new byte[i];
> out.write(data);
> out.flush();
> }
> }
> }
> ========================================
> TestClient result
> onData enter
> onData available = 10027
> onData exit
> onData enter
> onData available = 16
> onData exit
> onData enter
> onData available = 3752
> onData exit
> onData enter
> onData available = 10044
> onData exit
> onData enter
> onData available = 3752
> onData exit
>
> ----- Original Message -----
> From: <gre...@we...>
> To: "fulichen" <ful...@du...>
> Sent: Monday, April 21, 2008 7:21 PM
> Subject: Re: [xSocket-develop] OutOfMemory exception
>
> Hi fchen,
>
> by calling the <connection>.suspendRead() you can stop reading the
> socket. Reading will be resumed by calling the <connection>.
> reaumeRead() method. For an example see http://xsocket.svn.sourceforge.
> net/viewvc/xsocket/xsocket/core/trunk/src/test/java/org/xsocket/
> connection/LimitedReadTest.java?view=markup
>
> Gregor
>
> > -----Ursprî
sgliche Nachricht-----
> > Von: "fulichen" <ful...@du...>
> > Gesendet: 21.04.08 12:35:09
> > An: <xso...@li...>
> > Betreff: [xSocket-develop] OutOfMemory exception
>
> Our application need to handle many many connection,
> > and the data need time to process,
> > because xSocket Dispatcher read data when it is avialable,
> > our application often run out of memory.
> >
> > How we limit Dispatcher data reading, or what can we do to prevent
> > OutOfMemoryException?
> >
> > fchen---------------------------------------------------------------
> --
> > -------- This SF.net email is sponsored by the 2008 JavaOne(SM)
> > Conference Don't miss this year's exciting event. There's still
> time
> > to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/
> > clk;198757673;13503038;p?http://java.sun.com/javaone________________
> __
> > _____________________________ xSocket-develop mailing list xSocket-
> > de...@li...://lists.sourceforge.net/lists/
> >
> > listinfo/xsocket-develop
>
> --
> Gregor Roth
> ----------------------------------------------------------------------
> --- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save $
> 100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;
> 198757673;13503038;p?http://java.sun.com/javaone______________________
> _________________________ xSocket-develop mailing list xSocket-
> de...@li... https://lists.sourceforge.net/lists/
>
> listinfo/xsocket-develop
--
Gregor Roth
|