Skip to content

Commit

Permalink
Fix NPE when tab is close during load (JabRef#10841)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor authored Jan 30, 2024
1 parent 5286e67 commit 6e5f3c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/gui/util/BackgroundTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -37,6 +39,8 @@ public abstract class BackgroundTask<V> {
Localization.lang("Downloading"), IconTheme.JabRefIcons.DOWNLOAD.getGraphicNode()
);

private static final Logger LOGGER = LoggerFactory.getLogger(BackgroundTask.class);

private Runnable onRunning;
private Consumer<V> onSuccess;
private Consumer<Exception> onException;
Expand Down Expand Up @@ -92,6 +96,7 @@ public boolean isCanceled() {
}

public void cancel() {
LOGGER.debug("Canceling task");
this.isCanceled.set(true);
}

Expand Down

0 comments on commit 6e5f3c5

Please sign in to comment.