Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackgroundTask interrupted when shutting down JabRef #6

Open
LoayGhreeb opened this issue Jun 25, 2024 · 2 comments
Open

BackgroundTask interrupted when shutting down JabRef #6

LoayGhreeb opened this issue Jun 25, 2024 · 2 comments

Comments

@LoayGhreeb
Copy link
Owner

I want to close the Lucene IndexWriter when the library is closed or when JabRef shuts down. The index should be closed in the background because writing commits to the disk takes time.

Which method should be used for submitting background tasks? All Lucene indexing runs in the background using org.jabref.gui.util.BackgroundTask, but for closing the index, I used org.jabref.logic.util.HeadlessExecutorService.

  • If I use org.jabref.logic.util.HeadlessExecutorService#INSTANCE.execute(), it works without any interruptions, and JabRef waits until the closing task is finished:
    @Override
    public void close() {
    HeadlessExecutorService.INSTANCE.execute(() -> {
    try {
    LOGGER.debug("Closing linked files index");
    searcherManager.close();
    optimizeIndex();
    indexWriter.close();
    indexDirectory.close();
    LOGGER.debug("Linked files index closed");
    if (databaseContext.getFulltextIndexPath().getFileName().toString().equals("unsaved")) {
    LOGGER.debug("Deleting unsaved index directory");
    FileUtils.deleteDirectory(indexDirectoryPath.toFile());
    }
    } catch (IOException e) {
    LOGGER.error("Error while closing linked files index", e);
    }
    });
    }
  • If I use org.jabref.gui.util.BackgroundTask#executeWith to close the index, the task gets interrupted when it takes some time.
             BackgroundTask.wrap(() -> {
                 try {
                     LOGGER.debug("Closing linked files index");
                     searcherManager.close();
                     optimizeIndex();
                     indexWriter.close();
                     indexDirectory.close();
                     LOGGER.debug("Linked files index closed");
                     if (databaseContext.getFulltextIndexPath().getFileName().toString().equals("unsaved")) {
                         LOGGER.debug("Deleting unsaved index directory");
                         FileUtils.deleteDirectory(indexDirectoryPath.toFile());
                     }
                 } catch (IOException e) {
                     LOGGER.error("Error while closing linked files index", e);
                 }
             }).executeWith(taskExecutor);
         }
Stack trace
[pool-2-thread-3] org.jabref.logic.search.indexing.DefaultLinkedFilesIndexer.optimizeIndex()
WARN: Could not force merge segments.: java.nio.channels.ClosedChannelException
	at java.base/sun.nio.ch.FileLockImpl.release(FileLockImpl.java:58)
	at java.base/java.nio.channels.FileLock.close(FileLock.java:336)
	at [email protected]/org.apache.lucene.store.NativeFSLockFactory$NativeFSLock.close(NativeFSLockFactory.java:195)
	at [email protected]/org.apache.lucene.util.IOUtils.close(IOUtils.java:85)
	at [email protected]/org.apache.lucene.util.IOUtils.close(IOUtils.java:72)
	at [email protected]/org.apache.lucene.index.IndexWriter.rollbackInternalNoCommit(IndexWriter.java:2559)
	at [email protected]/org.apache.lucene.index.IndexWriter.rollbackInternal(IndexWriter.java:2464)
	at [email protected]/org.apache.lucene.index.IndexWriter.maybeCloseOnTragicEvent(IndexWriter.java:5774)
	at [email protected]/org.apache.lucene.index.IndexWriter.doFlush(IndexWriter.java:4321)
	at [email protected]/org.apache.lucene.index.IndexWriter.flush(IndexWriter.java:4255)
	at [email protected]/org.apache.lucene.index.IndexWriter.forceMerge(IndexWriter.java:2128)
	at [email protected]/org.jabref.logic.search.indexing.DefaultLinkedFilesIndexer.optimizeIndex(DefaultLinkedFilesIndexer.java:332)
	at [email protected]/org.jabref.logic.search.indexing.DefaultLinkedFilesIndexer.lambda$close$5(DefaultLinkedFilesIndexer.java:344)
	at [email protected]/org.jabref.gui.util.BackgroundTask$2.call(BackgroundTask.java:91)
	at [email protected]/org.jabref.gui.util.BackgroundTask$2.call(BackgroundTask.java:88)
	at [email protected]/org.jabref.gui.util.UiTaskExecutor$1.call(UiTaskExecutor.java:173)
	at [email protected]/javafx.concurrent.Task$TaskCallable.call(Task.java:1399)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)

@koppor, please take a look when you are available. Refs: https://github.com/JabRef/jabref-issue-melting-pot/issues/443

@koppor
Copy link

koppor commented Jul 2, 2024

https://github.com/jobrunr/jobrunr could be tried as another framework 😅🙈. Refs https://github.com/JabRef/jabref-issue-melting-pot/issues/460

@koppor
Copy link

koppor commented Jul 8, 2024

I want to close the Lucene IndexWriter when the library is closed or when JabRef shuts down. The index should be closed in the background because writing commits to the disk takes time.

And it is OK to not show any progress in the UI?

As far as I understand the PR JabRef#11385, there was some renaming to distinguish between UI and nonUI (called "headless").

As far as I understand your options and pros and cons, I think, calling HeadlessExecutorService is the right choice? We accept that no progress is shown in the UI. Nevertheless, there should be a LOGGER.info be called at the start and at the end. Sometimes, JabRef hangs during exiting (refs JabRef#11430) and it should be "easy" to trace which component hangs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants