fork() doesn't create processes but threads under Windows. Each thread gets a copy of all the data stored in Perl managed space. The cloning (making a duplicate of the data structures) doesn't affect the private data stored by the XS part of modules. So you only have one copy of that. It also doesn't dup system handles (except I believe sockets and filehandles). This means that

  1. Since some data is shared between threads and the modules were not written to handle this it's possible that more threads will access the data at once and corrupt it.
  2. When a thread finishes all Perl objects are destroyed. The DESTROY method of some object frees some XS memory or some handles. So if the memory or the handle is shared by several threads it will suddenly become invalid.

To try to fix the first problem you'll need to use some locking to prevent concurrent access and wrap the function/method calls in that. I'm afraid the performance will suffer and it will be hard to find out whether this is the problem and what to lock.

For the second problem you have to ensure the destroyed objects do not free the memory or the handles. There are several solutions.

  1. You can redefine the objects' DESTROY method by your own in all but one (the last to die) threads. - This'll only work fine if you do not create new objects in the threads after fork().
  2. You can rebless() the objects to a different class. Either to a void class just before you destroy them (hard to do safely) or to a "fake" class that inherits all methods except DESTROY (and maybe Close) from the original one immediately after fork().

    It may be hard to do though because the object that needs this treatment may be well hidden inside a different object.

    Another problem may be that a module installs an END block that'll free some memory or handles. In that case you'll need to try to "turn it off" by handmodifying some variables. You have to look at the module source of course.

    Even if you do all this it is not guaranteed to help. Some modules just do not work well with threads (yet). DBI comes to mind. In that case you might end up changing the code so that only one thread uses that module and provides its functionality to the other threads somehow.

    On the other hand I have a script that uses LWP in several threads and it works just fine. I do not create and destroy threads all the time though. I precreate several worker threads and pipe them commands. And it does sometimes crash while exiting (like that it would matter).

      Jenda

    P.S.: I wrote something on this some time ago, it's available here.

    P.P.S.: Nemyslim, ze bys na ty strance "zalozil" anonymous proxy ;-)


    In reply to Re: Merlyn Proxy sever fail on win32? by Jenda
    in thread Merlyn Proxy sever fail on win32? by LiTinOveWeedle

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



  3. Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  4. Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  5. Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  6. Please read these before you post! —
  7. 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
  8. 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;
  9. Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  10. See Writeup Formatting Tips and other pages linked from there for more info.