Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I suspect this test does not do what you think it does. In particular, consider the case that one process opens the file and writes to it, then another process opens the same file and, perhaps, writes to it, then the if ($f) test executes in the first process. Is the result any different because of what the other process did? I think you will find it is not and, therefore, that the statements in the if block are not as protected as you think they are.
Notice also "my $f = undef" and "unless (-e $filename)"
my $f=undef; getlock sub { unless (-e $filename) { open ($f, ">", $filename) or confess; binmode $f; } }; if ($f) {
( I admit that this code is unclear )
Whether your problem is solved depends on what you are trying to do, which you don't say, but what you are calling a solution seems strange to me.
That was proof of concept code, it contained a bug. Bug found, thus solved.
what you are trying to do, which you don't say
This was proof-of-concept code. I.e. I simplified my 1000 lines program to this code. Thus what I am trying to do is behind the scene, and we should focus only on technical part of problem, not business requirements.
If I post my original code, this would require at least couple of hours to set up things and reproduce the problem for anyone who tries to.
the test could pass but another process could modify the source file before the copy executes
Yes, but btw there are no concurrent writes to same file.
Changing the test on the die to 40_000_000 != -s $newfilename makes little difference to the outcome: there is still a variable subset of the possible 25 copied files and there are still several of them with fewer than 40_000_000 characters.
Well, it indeed does not fix program to output correct files, but it causes some processes to DIE, thus eliminating a bug. See assertions.

Actual fix is to extend lock to whole program, until copy is done (as other posters suggested). Or even just extend it to the point when write to source file is done and file is closed.

In reply to Re^4: Strange IO + concurrency issue by vsespb
in thread [SOLVED] Strange IO + concurrency issue by vsespb

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found