From 6e5f3c5aa90be013c90f34af8c6508f1e26d87a7 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Tue, 30 Jan 2024 17:07:01 +0100 Subject: [PATCH] Fix NPE when tab is close during load (#10841) --- src/main/java/org/jabref/gui/LibraryTab.java | 39 +++++++++++++++---- .../org/jabref/gui/util/BackgroundTask.java | 5 +++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 31c3c4bfc07..ea00153a7a4 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -25,6 +25,7 @@ import javafx.scene.control.ProgressIndicator; import javafx.scene.control.SplitPane; import javafx.scene.control.Tab; +import javafx.scene.control.TabPane; import javafx.scene.control.Tooltip; import javafx.scene.layout.BorderPane; import javafx.util.Duration; @@ -261,7 +262,12 @@ private void onDatabaseLoadingFailed(Exception ex) { } private void setDatabaseContext(BibDatabaseContext bibDatabaseContext) { - if (this.getTabPane().getSelectionModel().selectedItemProperty().get().equals(this)) { + TabPane tabPane = this.getTabPane(); + if (tabPane == null) { + LOGGER.debug("User interrupted loading. Not showing any library."); + return; + } + if (tabPane.getSelectionModel().selectedItemProperty().get().equals(this)) { LOGGER.debug("This case should not happen."); stateManager.setActiveDatabase(bibDatabaseContext); } @@ -778,12 +784,31 @@ private void onCloseRequest(Event event) { * Perform necessary cleanup when this Library is closed. */ private void onClosed(Event event) { - changeMonitor.ifPresent(DatabaseChangeMonitor::unregister); - PdfIndexerManager.shutdownIndexer(bibDatabaseContext); - AutosaveManager.shutdown(bibDatabaseContext); - BackupManager.shutdown(bibDatabaseContext, - preferencesService.getFilePreferences().getBackupDirectory(), - preferencesService.getFilePreferences().shouldCreateBackup()); + if (dataLoadingTask != null) { + dataLoadingTask.cancel(); + } + try { + changeMonitor.ifPresent(DatabaseChangeMonitor::unregister); + } catch (RuntimeException e) { + LOGGER.error("Problem when closing change monitor", e); + } + try { + PdfIndexerManager.shutdownIndexer(bibDatabaseContext); + } catch (RuntimeException e) { + LOGGER.error("Problem when shutting down PDF indexer", e); + } + try { + AutosaveManager.shutdown(bibDatabaseContext); + } catch (RuntimeException e) { + LOGGER.error("Problem when shutting down autosave manager", e); + } + try { + BackupManager.shutdown(bibDatabaseContext, + preferencesService.getFilePreferences().getBackupDirectory(), + preferencesService.getFilePreferences().shouldCreateBackup()); + } catch (RuntimeException e) { + LOGGER.error("Problem when shutting down backup manager", e); + } } /** diff --git a/src/main/java/org/jabref/gui/util/BackgroundTask.java b/src/main/java/org/jabref/gui/util/BackgroundTask.java index 9e6fe46980c..a0eda270279 100644 --- a/src/main/java/org/jabref/gui/util/BackgroundTask.java +++ b/src/main/java/org/jabref/gui/util/BackgroundTask.java @@ -22,6 +22,8 @@ import com.google.common.collect.ImmutableMap; import com.tobiasdiez.easybind.EasyBind; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class is essentially a wrapper around {@link Task}. @@ -37,6 +39,8 @@ public abstract class BackgroundTask { Localization.lang("Downloading"), IconTheme.JabRefIcons.DOWNLOAD.getGraphicNode() ); + private static final Logger LOGGER = LoggerFactory.getLogger(BackgroundTask.class); + private Runnable onRunning; private Consumer onSuccess; private Consumer onException; @@ -92,6 +96,7 @@ public boolean isCanceled() { } public void cancel() { + LOGGER.debug("Canceling task"); this.isCanceled.set(true); }