From 3b1021d9543dd67438a4090e2c868086681b1d01 Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Sun, 19 Nov 2023 00:51:19 +0100 Subject: [PATCH] Ensure adapter is available during global destruct Global Destruction The order in which objects are destroyed during the global destruction before the program exits is unpredictable. This means that any objects contained by your object may already have been destroyed. You should check that a contained object is defined before calling a method on it: https://perldoc.perl.org/perlobj#Global-Destruction Signed-off-by: Mikko Koivunalho --- lib/Log/Any/Manager.pm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Log/Any/Manager.pm b/lib/Log/Any/Manager.pm index a94b2f2..ff7fe29 100644 --- a/lib/Log/Any/Manager.pm +++ b/lib/Log/Any/Manager.pm @@ -35,7 +35,11 @@ sub get_adapter { # Create a new adapter for this category if it is not already in cache # my $category_cache = $self->{category_cache}; - if ( !defined( $category_cache->{$category} ) ) { + + # The order in which objects are destroyed during the global destruction + # before the program exits is unpredictable. + # {adapter} is an object, so already at this point it can be undef! + if ( !defined( $category_cache->{$category} ) || !defined( $category_cache->{$category}->{adapter} )) { my $entry = $self->_choose_entry_for_category($category); my $adapter = $self->_new_adapter_for_entry( $entry, $category ); $category_cache->{$category} = { entry => $entry, adapter => $adapter };