Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


#21 Race condition can cause NullPointerException on disconnect

Brad Davis

In Session.java the disconnect function has a block
synchronized on connectThread, in which it calls yield,
interrupt and then sets connectThread to null. This
seems to be an attempt to avoid the race condition of
having the member io set to null while the
connectThread is still running. However, it doesn't
work. The problem is that nothing else in the code
even synchronizes on connectThread. The disconnect
thread can pass right through that sync block, and set
io to null even when the while loop in Session.run() is
about to access the io variable.

The correct thing to do is encase access to the bulk of
the run loop in a block synchronized on connectThread,
and to place the interrupt call outside the sync block
in disconnect. This means that if disconnect is
called, it basically sets a flag for the loop to end,
which causes the loop to leave its sync block. The
(now empty) sync block in disconnect only serves to
ensure that the run loop has indeed exited its
dangerous portion.

Attached is a test case i can use to reproduce the null
pointer exception, though it happens intermittently as
it is a race condition.

I will also submit a patch to the patch section to
correct this.


Bugs: #82


  • Brad Davis
    Brad Davis

    source for a unit test which triggers the null pointer exception. The unit test doesn't neccessarily fail, but null pointer exceptions are generated in the session thread.