commit:     986b59bd7443b23146217f581e7b3b82a5d4912f
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Oct 24 01:43:03 2023 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Tue Oct 24 02:09:53 2023 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=986b59bd

bin/emaint: Use __main__ for spawn compat

If the event loop is closed outside of __main__
then it closes the event loop in child processes
for the multiprocessing spawn start method.

Bug: https://bugs.gentoo.org/916142
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 bin/emaint | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/bin/emaint b/bin/emaint
index a1b726b183..da925e1dad 100755
--- a/bin/emaint
+++ b/bin/emaint
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-# Copyright 2005-2020 Gentoo Authors
+# Copyright 2005-2023 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 """System health checks and maintenance utilities.
@@ -53,16 +53,21 @@ try:
     from portage.emaint.main import emaint_main
     from portage.util._eventloop.global_event_loop import global_event_loop
 
-    try:
-        emaint_main(sys.argv[1:])
-    except OSError as e:
-        if e.errno == errno.EACCES:
-            print("\nemaint: Need superuser access")
-            sys.exit(1)
-        else:
-            raise
-    finally:
-        global_event_loop().close()
+    if __name__ == "__main__":
+        try:
+            emaint_main(sys.argv[1:])
+        except OSError as e:
+            if e.errno == errno.EACCES:
+                print("\nemaint: Need superuser access")
+                sys.exit(1)
+            else:
+                raise
+        finally:
+            # Only close the event loop for __main__,
+            # since outside of __main__ it would close the
+            # event loop for child processes when using
+            # the multiprocessing spawn start method.
+            global_event_loop().close()
 
 except KeyboardInterrupt as e:
     # Prevent traceback on ^C

Reply via email to