multithreadingperlsshlibssh2ietf-netconf

Perl - How to make a library specific to individual threads


I am writing a multi threaded script in perl. In which I am using a library Net::Netconf::Manager which inturn uses Net::SSH2. This Net::SSH2(libssh2) doesn't seem to be thread safe when 'shared handles' simulataneously.

I quote as in libssh2 website

Thread-safe: just don't share handles simultaneously

  1. I am not sure what this "sharing handles" mean. Also I would like to know how to 'not share handles'.

When I run my script, occasionally i see error trace with backtrace and memory map denoting *** glibc detected *** perl: double free or corruption (out): 0x00007f0320012d70 *** error. This error is because of the thread safeness of the Net::SSh2 library.

  1. How to make this Net::Netconf::Manager available to every thread instead of having it declared globally with 'use' . I want all the threads to have their own access to this library independent of other threads.

Please let me know your views.


Solution

  • I am the current maintainer for Net::SSH2.

    I have never went after thread safeness for that module but a shallow inspection of its code, shows that probably, that double free error is caused by the Perl side of the Net::SSH2 objects being cloned on thread creation while the C side is not. That results in libssh2 objects being destroyed and released twice which results in the program crashing.

    So, if you want to use Net::SSH2 in a multithread application you should ensure that threads are never created from threads where objects of this module exist.

    Even then there may be other bugs lurking on the module.

    Obviously the right thing to do would be to fix the module. If you want to do it yourself, I would try to help you. Just get in touch with me so that we could discuss the details first... Otherwise, now that you have brought that issue to my attention, well, probably at some point I will fix it myself... but this is not going to happen overnight.