# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1655216929 -7200
# Node ID cc0ec027781305ac2313d3636fb3885d8fcbba2f
# Parent  3729a6bdb5624d32e7183d51c24d11c407c9a6f0
util.startup: Fix async waiting for last shutdown steps

Observed problem: When shutting down prosody would immediately exit
after waiting for s2s connections to close, skipping the last cleanup
events and reporting the exit reason and code.

This happens because prosody.main_thread is in a waiting state and
queuing startup.shutdown is dispatched trough the main loop via
nexttick, but since the main loop was no longer running at that point it
proceeded to the end of the prosody script and exited there.

diff -r 3729a6bdb562 -r cc0ec0277813 prosody
--- a/prosody	Sun Jun 12 01:50:33 2022 +0200
+++ b/prosody	Tue Jun 14 16:28:49 2022 +0200
@@ -82,4 +82,4 @@
 
 loop();
 
-thread:run(startup.shutdown);
+startup.exit();
diff -r 3729a6bdb562 -r cc0ec0277813 util/startup.lua
--- a/util/startup.lua	Sun Jun 12 01:50:33 2022 +0200
+++ b/util/startup.lua	Tue Jun 14 16:28:49 2022 +0200
@@ -353,7 +353,7 @@
 			reason = reason;
 			code = code;
 		});
-		server.setquitting(true);
+		prosody.main_thread:run(startup.shutdown);
 	end
 end
 
@@ -644,6 +644,10 @@
 
 	prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified");
 	prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0);
+	server.setquitting(true);
+end
+
+function startup.exit()
 	os.exit(prosody.shutdown_code);
 end