Skip to content

spurious segmentation fault #151

Closed
Closed

Description

@bkw

This one is probably a toughie:

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:

https://gist.github.com/bkw/8bb899281c2c87727f46

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.

Activity

  1. lovell commented on Jan 20, 2015

    @lovell
    Owner

    Thanks for the useful backtrace Bernhard.

    ...
    #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.

    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?

  2. jcupitt commented on Jan 20, 2015

    @jcupitt
    Contributor

    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:

    int
    vips_jpegload_buffer( void *buf, size_t len, VipsImage **out, ... )

    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.

  3. bkw commented on Jan 20, 2015

    @bkw
    ContributorAuthor

    yes, it's 7.42

  4. bkw commented on Jan 20, 2015

    @bkw
    ContributorAuthor

    using sharp.cache(0) seems to avoid the crashes so far.

    update: no, it doesn't, sorry. Just happened again.

  5. added a commit that references this issue on Jan 20, 2015
  6. jcupitt commented on Jan 20, 2015

    @jcupitt
    Contributor

    I added some notes on buffer lifetime, it wasn't very clear before. Of course that might well not be the issue here.

  7. lovell commented on Jan 20, 2015

    @lovell
    Owner

    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?

  8. bkw commented on Jan 20, 2015

    @bkw
    ContributorAuthor

    Yes. The basic setup is like this:

    request
      .get(url)
      .pipe(
        sharp()
          .resize(x,y)
          .greyscale()
          .raw()
          .toBuffer(…)
      )

    So sharp reads from a stream and writes to a buffer. Obviously, I'm using the intake branch.

  9. added and removed on Jan 20, 2015
  10. added a commit that references this issue on Jan 20, 2015
  11. jcupitt commented on Jan 20, 2015

    @jcupitt
    Contributor

    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.

  12. added a commit that references this issue on Jan 21, 2015
  13. lovell commented on Jan 21, 2015

    @lovell
    Owner

    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.

  14. bkw commented on Jan 21, 2015

    @bkw
    ContributorAuthor

    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 !

  15. bkw commented on Jan 21, 2015

    @bkw
    ContributorAuthor

    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?

  16. jcupitt commented on Jan 21, 2015

    @jcupitt
    Contributor

    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.

  17. bkw commented on Jan 21, 2015

    @bkw
    ContributorAuthor

    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.

  18. lovell commented on Jan 21, 2015

    @lovell
    Owner

    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.

  19. bkw commented on Jan 21, 2015

    @bkw
    ContributorAuthor

    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?

  20. jcupitt commented on Jan 21, 2015

    @jcupitt
    Contributor

    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.

  21. added this to the v0.9.0 milestone on Jan 22, 2015
  22. lovell commented on Jan 22, 2015

    @lovell
    Owner

    @bkw Other than the liborc-related problem, is everything else on the intake branch now working as you'd expect?

  23. lovell commented on Jan 23, 2015

    @lovell
    Owner

    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.

  24. lovell commented on Jan 23, 2015

    @lovell
    Owner

    @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions