diff --git a/docs/changelog.md b/docs/changelog.md index 95ea8978a..381f6abd6 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,6 +4,11 @@ Requires libvips v8.9.1 +### v0.25.3 - TBD + +* Ensure libvips is initialised only once, improves worker thread safety. + [#2143](https://github.com/lovell/sharp/issues/2143) + ### v0.25.2 - 20th March 2020 * Provide prebuilt binaries for Linux ARM64v8. diff --git a/src/sharp.cc b/src/sharp.cc index 00b259b06..ae15648b6 100644 --- a/src/sharp.cc +++ b/src/sharp.cc @@ -21,8 +21,14 @@ #include "utilities.h" #include "stats.h" -Napi::Object init(Napi::Env env, Napi::Object exports) { +static void* sharp_vips_init(void*) { vips_init("sharp"); + return nullptr; +} + +Napi::Object init(Napi::Env env, Napi::Object exports) { + static GOnce sharp_vips_init_once = G_ONCE_INIT; + g_once(&sharp_vips_init_once, static_cast(sharp_vips_init), nullptr); g_log_set_handler("VIPS", static_cast(G_LOG_LEVEL_WARNING), static_cast(sharp::VipsWarningCallback), nullptr);