Let me start by saying that this post is about fixing the bug in Net::SSH2 explained here and here.

The issue I am facing is that Net::SSH2 wraps the objects in the underlaying libssh2 library using the sv_setref_pv function that just creates a new SV and places the pointer into the IV slot. When a thread containing Net::SSH2 objects creates a new thread, those SVs are cloned but the objects they point to are not. Later, when in some thread the object gets out of scope and its DESTROY method is called, the underlaying object is deallocated leaving the clones with a dangling pointer and after that, any use of those, including destroying then, may cause the application to crash.

So, my question is, what mechanism could be used to avoid that. How XS/C objects can be wrapped in a multi-thread friendly way?

The only viable way I can envision in to use magic for that, replacing the sv_setref_pv wrapping for one using a magic structure of type PERL_MAGIC_ext (~) with svt_dup pointing to a callback that does reference counting.

Are there any easier ways for doing it?


In reply to Multi-thread friendly object life-time handling for XS modules by salva

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.