commit:     b089dd5bed1b7d2a5d4edd447d9df38ae12093b6
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Tue Oct 24 01:26:26 2023 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Tue Oct 24 02:24:34 2023 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=b089dd5b

bin/emerge: 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/emerge | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/bin/emerge b/bin/emerge
index f5dbc31066..6bd3d2166f 100755
--- a/bin/emerge
+++ b/bin/emerge
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-# Copyright 2006-2022 Gentoo Authors
+# Copyright 2006-2023 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
 import os
@@ -18,9 +18,7 @@ class SignalInterrupt(KeyboardInterrupt):
         self.signum = signum
 
 
-global_event_loop = None
-try:
-
+def main():
     def signal_interrupt(signum, _frame):
         raise SignalInterrupt(signum)
 
@@ -84,17 +82,26 @@ try:
             sys.exit(1)
         sys.exit(retval)
 
-except KeyboardInterrupt as e:
-    # This block ensures that ^C interrupts are handled quietly. We handle
-    # KeyboardInterrupt instead of installing a SIGINT handler, since
-    # exiting from signal handlers intermittently causes python to ignore
-    # the SystemExit exception with a message like this:
-    # Exception SystemExit: 130 in <function remove at 0x7fd2146c1320> ignored
-    signum = getattr(e, "signum", signal.SIGINT)
-    signal.signal(signum, signal.SIG_DFL)
-    sys.stderr.write(f"\n\nExiting on signal {signum}\n")
-    sys.stderr.flush()
-    raise_signal(signum)
-finally:
-    if global_event_loop is not None:
-        global_event_loop().close()
+
+if __name__ == "__main__":
+    global_event_loop = None
+    try:
+        main()
+    except KeyboardInterrupt as e:
+        # This block ensures that ^C interrupts are handled quietly. We handle
+        # KeyboardInterrupt instead of installing a SIGINT handler, since
+        # exiting from signal handlers intermittently causes python to ignore
+        # the SystemExit exception with a message like this:
+        # Exception SystemExit: 130 in <function remove at 0x7fd2146c1320> 
ignored
+        signum = getattr(e, "signum", signal.SIGINT)
+        signal.signal(signum, signal.SIG_DFL)
+        sys.stderr.write(f"\n\nExiting on signal {signum}\n")
+        sys.stderr.flush()
+        raise_signal(signum)
+    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.
+        if global_event_loop is not None:
+            global_event_loop().close()

Reply via email to