mogrify may cause data loss

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
naoliv
Posts: 110
Joined: 2007-12-10T18:54:27-07:00
Location: Brazil

mogrify may cause data loss

Post by naoliv »

Hi!

From http://bugs.debian.org/324203 and http://bugs.debian.org/358407, we can see that a file can get lost/corrupted if interrupted when mogrify is running.

A test script that shows this behavior:

Code: Select all

#!/usr/bin/perl
use Time::HiRes qw( usleep );
use POSIX;
$file = "file.jpg";
$size=(stat($file))[7];
$i=0;
while( $size > 0 && $i < 100000 ){
        if($p=fork) {
                usleep(rand(10000));
                for($i=0;$i<10000;$i++) {
                        kill(15,$p);
                }
        } else {
                usleep(rand(10000));
                system("mogrify -scale 1024 $file") or die "mog";
                exit;
        }
        $i++;
}
print "Done $i $size.\n";
(maybe you want to echo 2 > /proc/sys/vm/overcommit_memory on a Linux system, before testing it).

Can mogrify be modified to work atomically, please?

Thank you!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: mogrify may cause data loss

Post by magick »

I agree with Daniel Kobras on this one:
If you rely on data integrity, use 'convert foo.jpg tmp.jpg && mv tmp.jpg foo.jpg'
You can also use the mogrify -path or -format options to leave the original image file alone and write to an alternative file format or path.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: mogrify may cause data loss

Post by anthony »

I would go further and test for file existance and size as well!

In many scripts I also check the final image size to make sure I have it right!

Remember Murphy's Law, and it it's always IM that is the problem either! Disk space memory limits, user aborts, networking failures on NFS or faked file systems, even full system crashes. It is a messy world we live in even if most of the time computers try to make it neat and clean for us.

Viva Chaos!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply