You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm pulling image urls from a queue, download them and run some sharp transformations on them.
Every once in a while a get a segmentation fault like this:
Unfortunately I have a very hard time reproducing this. I tried processing the same images in succession but no luck. But when I go back to processing the queue, sooner or later the crash happens again.
If you have any advice how to enhance this bug report, please let me know.
...
#3 0x00007fbf454a32fd in jpeg_finish_decompress (cinfo=cinfo@entry=0x7fbf1c2552a0) at jdapimin.c:387
#4 0x00007f3de59a5ea8 in readjpeg_free (jpeg=0x7f3dc4294c70) at jpeg2vips.c:198
...
#379 0x00007f3de59ccd3e in vips_cache_trim () at cache.c:713
...
The inclusion of vips_cache_trim suggests libvips is doing a cache eviction. The mention of jpeg_finish_decompress means it's trying to remove a JPEG-backed image object that failed to complete decompression.
Assuming this is vips 7.42, I think that's OK. jpeg_finish_decompress() is only called from readjpeg_free(), and readjpeg_free() is only called from a ::close callback. This is the only path for a shutdown of the reader, and you would expect it to come here during cache eviction.
Could this be jpeg reading from a memory buffer? If it is, it could be trying to shut down a reader attached to a memory buffer that has been freed. Could node have trashed the input buffer for some reason?
out now owns the memory buffer. You must not free that buffer until out emits ::close.
Actually, it's stricter, I think. The jpeg reader will shut down on out::close, so the memory buffer must stay alive until out::postclose, since you don't know in what order ::close handlers will fire.
I wondered why it was reading the input during close. I skim-read libjpeg and all jpeg_finish_decompress() does is verify the last few bytes in the file and raise an error if they're wrong. I think we can live without that, so I removed the call. It doesn't leak, jpeg_destroy(), which we still call, frees resources.
Of course, node will still need to not free the buffer until ::postclose.
Based on John's suggestion, the latest code on the intake branch now waits for the postclose signal emitted by libvips before deleting the (copy of the) input buffer.
It also now correctly notifies the V8 garbage collector of the memory allocation, which means garbage collection should run in a more timely manner, with less danger of hitting swap for long-running processes.
I've run the leak tests and everything seems OK on Node 0.10 stable, but it currently segfaults on Node 0.11 unstable. The latter uses completely different memory allocation logic for Buffers so I need to do more triage/testing.
I upgraded libvips to github master and sharp to intake with 855945b. I'm currently running it with 10 concurrent transforms and caching enabled, like I was when I first encountered the segfaults. Seems to work fine so far, I'll let you know how it goes on.
Thanks for your incredibly fast and superb support, @lovell and @jcupitt !
Oh poop. That's a crash in Orc, the run-time compiler. Which version are you using?
Could you try without Orc? If it still crashes, maybe the input buffer to the sharpen has vanished. If it doesn't, it might be your orc that's at fault. I'm using 0.4.23, if that's any help.
I've found liborc <0.4.20 segfaults and <0.4.22 leaks.
The Node 0.11 unstable segfault was related to the V8 notifications of memory (de)allocation. The version of V8 used by Node 0.11 requires you to know which "Isolate" VM should be notified.
I'm going to remove the V8 notification code as its use is optional, it only helps the GC when memory is really tight and the multi-threaded nature of how to solve the problem makes my head hurt.
I was wondering: When all I do is decode jpegs, scale them down to a fixed-aspect size, grayscale, rotate, flip and extract regions from that, will I even notice a difference in terms of performance without orc?
Orc makes convolution, which sharp uses for the sharpening pass of resize, about 4x faster. But of course that's only a small part of the process, I'd guess it's saving less than 10% of the runtime.
I'd be interested in the results if you get any timings.
The intake branch has now been merged to master so these fixes will be available in v0.9.0. Feel free to open a new issue if the liborc problems are not solved by upgrading to 0.4.23.
As always, thanks for your help @jcupitt and thank you @bkw for reporting this problem in the first place.
@bkw If you're able to share details publicly, it's always great to find out what people are doing with sharp (and therefore libvips) in the survey at #35
Activity
lovell commentedon Jan 20, 2015
Thanks for the useful backtrace Bernhard.
The inclusion of
vips_cache_trimsuggests libvips is doing a cache eviction. The mention ofjpeg_finish_decompressmeans it's trying to remove a JPEG-backed image object that failed to complete decompression.https://github.com/jcupitt/libvips/blob/master/libvips/foreign/jpeg2vips.c#L194-L198
@jcupitt Would you expect this object be present in the cache in this state?
jcupitt commentedon Jan 20, 2015
Assuming this is vips 7.42, I think that's OK. jpeg_finish_decompress() is only called from readjpeg_free(), and readjpeg_free() is only called from a ::close callback. This is the only path for a shutdown of the reader, and you would expect it to come here during cache eviction.
Could this be jpeg reading from a memory buffer? If it is, it could be trying to shut down a reader attached to a memory buffer that has been freed. Could node have trashed the input buffer for some reason?
I think the rule is, if you call:
outnow owns the memory buffer. You must not free that buffer untiloutemits ::close.Actually, it's stricter, I think. The jpeg reader will shut down on out::close, so the memory buffer must stay alive until out::postclose, since you don't know in what order ::close handlers will fire.
bkw commentedon Jan 20, 2015
yes, it's 7.42
bkw commentedon Jan 20, 2015
using sharp.cache(0) seems to avoid the crashes so far.
update: no, it doesn't, sorry. Just happened again.
clearer docs for load from buffer
jcupitt commentedon Jan 20, 2015
I added some notes on buffer lifetime, it wasn't very clear before. Of course that might well not be the issue here.
lovell commentedon Jan 20, 2015
Thanks @jcupitt, I agree with your thinking - the Buffer is being freed too early in Node-land.
@bkw Are you using Buffer/Stream based input in JavaScript-land?
bkw commentedon Jan 20, 2015
Yes. The basic setup is like this:
So sharp reads from a stream and writes to a buffer. Obviously, I'm using the
intakebranch.don't call jpeg_finish_decompress()
jcupitt commentedon Jan 20, 2015
I wondered why it was reading the input during close. I skim-read libjpeg and all jpeg_finish_decompress() does is verify the last few bytes in the file and raise an error if they're wrong. I think we can live without that, so I removed the call. It doesn't leak, jpeg_destroy(), which we still call, frees resources.
Of course, node will still need to not free the buffer until ::postclose.
Delete input buffer on postclose #151
lovell commentedon Jan 21, 2015
Based on John's suggestion, the latest code on the
intakebranch now waits for thepostclosesignal emitted by libvips before deleting the (copy of the) input buffer.It also now correctly notifies the V8 garbage collector of the memory allocation, which means garbage collection should run in a more timely manner, with less danger of hitting swap for long-running processes.
I've run the leak tests and everything seems OK on Node 0.10 stable, but it currently segfaults on Node 0.11 unstable. The latter uses completely different memory allocation logic for Buffers so I need to do more triage/testing.
bkw commentedon Jan 21, 2015
I upgraded libvips to github master and sharp to intake with 855945b. I'm currently running it with 10 concurrent transforms and caching enabled, like I was when I first encountered the segfaults. Seems to work fine so far, I'll let you know how it goes on.
Thanks for your incredibly fast and superb support, @lovell and @jcupitt !
bkw commentedon Jan 21, 2015
After a while I did get a new segfault, but the backtrace looks completely different:
https://gist.github.com/bkw/29ae5896dfb1487ec116
Should a open a new issue for that?
jcupitt commentedon Jan 21, 2015
Oh poop. That's a crash in Orc, the run-time compiler. Which version are you using?
Could you try without Orc? If it still crashes, maybe the input buffer to the sharpen has vanished. If it doesn't, it might be your orc that's at fault. I'm using 0.4.23, if that's any help.
bkw commentedon Jan 21, 2015
This was on ubuntu-14.04.1 LTS, currently at 0.4.18. I'll try out a new master build of libvips without orc now.
lovell commentedon Jan 21, 2015
I've found liborc <0.4.20 segfaults and <0.4.22 leaks.
The Node 0.11 unstable segfault was related to the V8 notifications of memory (de)allocation. The version of V8 used by Node 0.11 requires you to know which "Isolate" VM should be notified.
I'm going to remove the V8 notification code as its use is optional, it only helps the GC when memory is really tight and the multi-threaded nature of how to solve the problem makes my head hurt.
Remove optional AdjustAmountOfExternalAllocatedMemory #151
bkw commentedon Jan 21, 2015
I was wondering: When all I do is decode jpegs, scale them down to a fixed-aspect size, grayscale, rotate, flip and extract regions from that, will I even notice a difference in terms of performance without orc?
jcupitt commentedon Jan 21, 2015
Orc makes convolution, which sharp uses for the sharpening pass of resize, about 4x faster. But of course that's only a small part of the process, I'd guess it's saving less than 10% of the runtime.
I'd be interested in the results if you get any timings.
lovell commentedon Jan 22, 2015
@bkw Other than the liborc-related problem, is everything else on the
intakebranch now working as you'd expect?lovell commentedon Jan 23, 2015
The
intakebranch has now been merged to master so these fixes will be available in v0.9.0. Feel free to open a new issue if the liborc problems are not solved by upgrading to 0.4.23.As always, thanks for your help @jcupitt and thank you @bkw for reporting this problem in the first place.
lovell commentedon Jan 23, 2015
@bkw If you're able to share details publicly, it's always great to find out what people are doing with sharp (and therefore libvips) in the survey at #35