libssh server - multithreaded races and other bugs now in a git repo near you :)

Having put in code to handle Avahi discovery in what has now been labeled as the 0.3 release, I have been focusing on having an integrated libssh based server.

Aris over the libssh team had helped debug a pretty nasty bug wherein forwarded X clients would fail with error messages such as bad packet length, HMAC error,etc. Thanks to Aris's efforts he was able to pinpoint this issue to a race condition that occured due to the fact that I had multiple threads doing ssh event loops for the same session. For the curious you can read Aris's archived mail on the subject and the proposed solution at

Keeping his suggestions in mind, I refactored my code such that there would be only one thread per session, although there could be multiple connects i.e. sessions. This worked fantastic for me. I could finally have different ssh clients connect to the same instance of the server and X forward simple x clients like xcalc, xclock, xeyes, xgc.

Unfortunately as soon as I tried to forward more complex applications such as libreoffice, chromium etc things weren't working. Further analysis and a look at the openssh code showed that these applications spawn multiple connects to the same $DISPLAY. Therefore as I understand it (could be wrong) the server needs to accept and process different event connections on the same session.

I have refactored the code again to account for this but basically this reintroduces the same race condition with multiple event handling we had seen earlier and while programs like libreoffice, chromium etc move further along they eventually crash due to bad packet length, mac errors,etc.

I tried to protect the event handling function ssh_event_dopoll with a mutex hoping this will resolve the issue, however it didn't make any difference.

Possible solutions :

  • refactor my code somehow to avoid using multiple threads per session and still work with complex X applications.
  • have libssh handle multithreading for a session without there being a race condition.

I am yet back again to the drawing board and I am hoping yet again that the libssh guys will come to my rescue and enlighten me :)

In the meantime just so you guys know I'm not faffing around and making excuses I've put up the code for the independent server in a separate git repo. You can download this code and play with it by issuing the command :

git clone git://

Previous Post Next Post