As the topic suggests I have a server and some clients.
The server accepts I/O connections concurrently (no queueing in socket connections) but I have this troubling issue and I do not know how to bypass it!
If I force a client to throw an I/O Exception the server detects it and terminates the client thread correctly (verified from Task Manager (Windows) and System Monitor (Ubuntu) ). But If I emulate an I/O that is “hanging” like
i.e.
Thread.sleep(60*1000);
or
private static Object lock = new Object();
synchronized(lock) {
while (true) {
try {
lock.wait();
} catch (InterruptedException e) {
/* Foo */
}
}
}
then all subsequent I/O operations (connection & data transfer) seem to block or wait until the “hanging” client is terminated. The applications makes use of the ExecutorService so if the “hanging” client does not complete the operations in the suggested time limit then the task will time out and the client is forced to exit. The subsequent “blocked” I/Os will resume but I wonder why the server doesn’t accept any I/O connections or performs any I/O operations when a client “hangs”?
NOTE:The client threading takes place in the server main like this:
while (true) {
accept client connection;
submit client task;
||
\ /
\/
// ExecutorService here in the form
// spService.submit(new Callable<Tuple<String[], BigDecimal[]>>() {
// ... code ... }}).get(taskTimeout, taskTimeUnit);
check task result & perform cleanup if result is null;
otherwise continue;
}
I think I have found the source of my problems!
I do use one thread-per-client model but I run my tests locally i.e. in the same machine which means all of them have the same IP!
So each client is assigned the same IP with the server! I guess that this leaves server and clients to differ only in port number but since each client is mapped to a different localport for each server connection then the server shouldn’t block. I have confirmed that each client and server use different I/Os (compared references) and I wrap their sockets using
<Input/Output>Streams toBufferedReaders &PrintWriters but still when a client hangs all other clients hang too (so maybe the I/O channels are indeed the same???)!I will test this on another machine and check the results back with you! 🙂
EDIT: Confirmed the erratic behaviour. It seems that even with remote clients if one hangs the other clients seem to hang too! :/
Don’t know but I am determined to fix this. It’s just that it’s pretty weird since I am pretty sure I use one thread-per-client (I/Os differ, client sockets differ, IPs seem to be not a problem, I even map each client in the server to a localport of my choice …)
May be I’ll switch to NIO if I don’t find a solution soon enough.
SOLUTION: Solved the problem!
It seemed that the
ExecutorServicehad to be run in a seperate thread otherwise if an I/O in a client blocked, all I/Os would block!That’s strange given the fact that I’ve tried both an
Executors.newFixedThreadPool(<nThreads>);andExecutors.newCachedThreadPool();and the client actions (aka I/Os) should take place in a new Thread for each client.In any case, I used a method and wrapped the calls so each client instace would use a
final ExecutorService baseWorker = Executors.newSingleThreadExecutor();and created a new Thread explicitly each time using<Thread instance>.start();so each thread would run in the background 🙂