Skip to content

Commit

Permalink
Fix panic on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbyttow committed Dec 20, 2020
1 parent 07ff0af commit 1cec7b1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
13 changes: 9 additions & 4 deletions vips/govips.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include "govips.h"

static void govips_logging_handler(
const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data)
const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data)
{
govipsLoggingHandler((char *)log_domain, (int)log_level, (char *)message);
govipsLoggingHandler((char *)log_domain, (int)log_level, (char *)message);
}

void vips_set_logging_handler(void)
{
g_log_set_default_handler(govips_logging_handler, NULL);
g_log_set_default_handler(govips_logging_handler, NULL);
}

void vips_unset_logging_handler(void)
{
g_log_set_default_handler(NULL, NULL);
}
20 changes: 15 additions & 5 deletions vips/govips.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (

var (
running = false
hasShutdown = false
initLock sync.Mutex
statCollectorDone chan struct{}
once sync.Once
Expand All @@ -56,9 +57,14 @@ type Config struct {
// Startup sets up the libvips support and ensures the versions are correct. Pass in nil for
// default configuration.
func Startup(config *Config) {
if hasShutdown {
panic("govips cannot be stopped and restarted")
}

initLock.Lock()
runtime.LockOSThread()
defer initLock.Unlock()

runtime.LockOSThread()
defer runtime.UnlockOSThread()

if running {
Expand All @@ -78,9 +84,8 @@ func Startup(config *Config) {
defer freeCString(cName)

// Initialize govips logging handler and verbosity filter to historical default
if !currentLoggingOverriden {
if !currentLoggingOverridden {
LoggingSettings(nil, LogLevelInfo)
currentLoggingOverriden = false
}

// Override default glib logging handler to intercept logging messages
Expand Down Expand Up @@ -151,13 +156,16 @@ func Startup(config *Config) {

// Shutdown libvips
func Shutdown() {
hasShutdown = true

if statCollectorDone != nil {
statCollectorDone <- struct{}{}
}

initLock.Lock()
runtime.LockOSThread()
defer initLock.Unlock()

runtime.LockOSThread()
defer runtime.UnlockOSThread()

if !running {
Expand All @@ -166,10 +174,12 @@ func Shutdown() {
}

C.vips_shutdown()
C.vips_unset_logging_handler()
running = false
}

// ShutdownThread clears the cache for for the given thread.
// ShutdownThread clears the cache for for the given thread. This needs to be
// called when a thread using vips exits.
func ShutdownThread() {
C.vips_thread_shutdown()
}
Expand Down
3 changes: 2 additions & 1 deletion vips/govips.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ static void govips_logging_handler(
const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data);

void vips_set_logging_handler(void);
void vips_set_logging_handler(void);
void vips_unset_logging_handler(void);
4 changes: 2 additions & 2 deletions vips/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
var (
currentLoggingHandlerFunction LoggingHandlerFunction
currentLoggingVerbosity LogLevel
currentLoggingOverriden bool
currentLoggingOverridden bool
)

// govipsLoggingHandler is the private bridge function exported to the C library
Expand Down Expand Up @@ -63,7 +63,7 @@ func LoggingSettings(handler LoggingHandlerFunction, verbosity LogLevel) {
// TODO turn on debugging in libvips and redirect to handler when setting verbosity to debug
// This way debugging information would go to the same channel as all other logging

currentLoggingOverriden = true
currentLoggingOverridden = true
}

func defaultLoggingHandlerFunction(messageDomain string, messageLevel LogLevel, message string) {
Expand Down

0 comments on commit 1cec7b1

Please sign in to comment.