Skip to content

Add and implement BukkitScheduler getAsyncExecutor#8595

Closed
A248 wants to merge 1 commit into
PaperMC:masterfrom
A248:async-executor-api
Closed

Add and implement BukkitScheduler getAsyncExecutor#8595
A248 wants to merge 1 commit into
PaperMC:masterfrom
A248:async-executor-api

Conversation

@A248
Copy link
Copy Markdown
Contributor

@A248 A248 commented Nov 26, 2022

This is a second-try / recreation of #4064 . In this PR I renamed the method to getAsyncExecutor, for symmetry with getMainThreadExecutor. These methods work nicely in tandem. Their use is particularly relevant when working with CompletableFuture.

One could argue they are not strictly counterparts and therefore should be named differently. The existing method getMainThreadExecutor provides pure convenience and nothing else. In contrast, getAsyncExecutor adds a new mechanism for using the common thread pool that circumvents traditional tasks; it does not merely redirect calls to runTaskAsynchronously. However, I do not find this argument sufficient justification for creating asymmetrical method names considering their most obvious use-case -- interoperation with CompletableFuture and other task-chaining APIs -- will be the same.

@A248 A248 requested a review from a team as a code owner November 26, 2022 22:23
public class CraftAsyncScheduler extends CraftScheduler {

- private final ThreadPoolExecutor executor = new ThreadPoolExecutor(
+ final ThreadPoolExecutor executor = new ThreadPoolExecutor(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AT, make sure to add it to the patch header

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this acronym mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access transformer, see the paper contributing guide.

Comment on lines +45 to +106
+ private void waitForAsyncTasksToShutdown(boolean reload) {
+
+ // Wait for plugins to close their threads
int pollCount = 0;

- // Wait for at most 5 seconds for plugins to close their threads
- while (pollCount < 10*5 && getScheduler().getActiveWorkers().size() > 0) {
+ // During reload, wait for at most 2.5 seconds, in shutdown, 5 seconds
+ long sleepTime = (reload) ? 50 : 100;
+
+ while (pollCount < 50 && !getScheduler().isAllFinished()) {
try {
- Thread.sleep(100);
+ Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
pollCount++;
}

- List<BukkitWorker> overdueWorkers = getScheduler().getActiveWorkers();
+ List<BukkitWorker> overdueWorkers = this.getScheduler().getActiveWorkers();
for (BukkitWorker worker : overdueWorkers) {
Plugin plugin = worker.getOwner();
- String author = "<NoAuthorGiven>";
- if (plugin.getDescription().getAuthors().size() > 0) {
- author = plugin.getDescription().getAuthors().get(0);
+ warnNotShuttingDownAsyncTasks(plugin, reload);
+ if (reload && console.isDebugging()) {
+ io.papermc.paper.util.TraceUtil.dumpTraceForThread(worker.getThread(), "still running");
}
- getLogger().log(Level.SEVERE, String.format(
- "Nag author: '%s' of '%s' about the following: %s",
- author,
- plugin.getDescription().getName(),
- "This plugin is not properly shutting down its async tasks when it is being shut down. This task may throw errors during the final shutdown logs and might not complete before process dies."
- ));
}
+ for (Plugin plugin : getScheduler().getUnfinishedFromExecutors()) {
+ warnNotShuttingDownAsyncTasks(plugin, reload);
+ }
+ }
+
+ private void warnNotShuttingDownAsyncTasks(Plugin plugin, boolean reload) {
+ String message;
+ if (reload) {
+ message = "This plugin is not properly shutting down its async tasks when it is being reloaded. "
+ + "This may cause conflicts with the newly loaded version of the plugin";
+ } else {
+ message = "This plugin is not properly shutting down its async tasks when it is being shut down. "
+ + "This task may throw errors during the final shutdown logs and might not complete before process dies.";
+ }
+ getLogger().log(Level.SEVERE, String.format(
+ "Nag author(s): '%s' of '%s' about the following: %s",
+ plugin.getDescription().getAuthors(),
+ plugin.getDescription().getFullName(),
+ message
+ ));
+ }
+
+ public void waitForAsyncTasksShutdown() {
+ waitForAsyncTasksToShutdown(false);
}
// Paper end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a bit of conflict with a pre-existing paper patch. Can you please merge some of these changes into the already existing Wait for Async Tasks during shutdown patch?
This should hopefully pretty up the diff here.

}

- private int nextId() {
+ int nextId() { // Paper - private -> package private
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AT


// Paper start
- private final CraftScheduler asyncScheduler;
+ final CraftAsyncScheduler asyncScheduler;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change the type up here, instead just cast it when needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field is also made package-private, which fact is used in PluginExecutor.

throw new UnsupportedOperationException("Use BukkitRunnable#runTaskTimerAsynchronously(Plugin, long, long)");
}

- // Paper start - add getMainThreadExecutor
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to edit paper comments edit them in the patch they came from

public BukkitTask runTaskTimerAsynchronously(@NotNull Plugin plugin, @NotNull BukkitRunnable task, long delay, long period) throws IllegalArgumentException;

- // Paper start - add getMainThreadExecutor
+ // Paper start
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see below about paper comments

@A248
Copy link
Copy Markdown
Contributor Author

A248 commented Nov 27, 2022

There are in fact several existing patches whose changes this PR interacts with:

  • 0397-misc-debugging-dumps
  • 0407-Wait-for-Async-Tasks-during-shutdown
  • 0572-Add-getMainThreadExecutor-to-BukkitScheduler

They leave me many choices. I am able to merge them all into one patch, which seems simplest. Alternatively I might edit the existing patches individually, and manipulate patch ordering, so as to produce minimal difference in the latest patch, 0949-Implement-CraftScheduler-getAsyncExecutor-API. There are various combinations of these operations. What is preferred?

Additionally, there is of course 185-Improved-Async-Task-Scheduler, on which many other changes are based.

@Owen1212055
Copy link
Copy Markdown
Member

It's mostly that you're overlapping a lot with Wait-for-Async-Tasks-during-shutdown here, mostly editing a method that it adds itself. So, at least the majority of that diff can be shifted to that patch.

So at least for now, try to keep the patch noise low for PR viewability.

@Warriorrrr Warriorrrr moved this from Changes required to Waiting For Author in Paper PR Queue Mar 5, 2025
@kennytv kennytv added the pre-softspoon: never rebased Pre-hardfork pull requests that were not re-opened with the new main branch label Mar 23, 2025
@kennytv kennytv deleted the branch PaperMC:master March 23, 2025 19:15
@kennytv kennytv closed this Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pre-softspoon: never rebased Pre-hardfork pull requests that were not re-opened with the new main branch pre-softspoon

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants