diff --git a/automation/tests/aegisub.cpp b/automation/tests/aegisub.cpp
index 197ab032040054eac3e2ce4c6468038b2182a428..ea667cd946d3d3053bd6f8ca24b0ffb1aacea7bc 100644
--- a/automation/tests/aegisub.cpp
+++ b/automation/tests/aegisub.cpp
@@ -33,6 +33,12 @@ void check(lua_State *L, int status) {
 		exit(status);
 	}
 }
+
+int close_and_exit(lua_State *L) {
+	int status = lua_tointeger(L, 1);
+	lua_close(L);
+	exit(status);
+}
 }
 
 int main(int argc, char **argv) {
@@ -54,6 +60,13 @@ int main(int argc, char **argv) {
 	preload_modules(L);
 	Install(L, {"include"});
 
+	// Patch os.exit to close the lua state first since busted calls it when
+	// it's done
+	lua_getglobal(L, "os");
+	lua_pushcfunction(L, close_and_exit);
+	lua_setfield(L, -2, "exit");
+	lua_pop(L, 1);
+
 	// Build arg table for scripts
 	lua_createtable(L, argc - 1, 0);
 	for (int i = 1; i < argc; ++i) {
@@ -76,5 +89,6 @@ int main(int argc, char **argv) {
 
 	int base = lua_gettop(L) - argc + 1;
 	check(L, lua_pcall(L, argc - 2, LUA_MULTRET, base));
+	lua_close(L);
 }