Make exception logging failures more robust
[mudpy.git] / lib / mudpy / misc.py
index 96957bc..02db563 100644 (file)
@@ -6,8 +6,6 @@
 # terms provided in the LICENSE file distributed with this software.
 
 import codecs
-import ctypes
-import ctypes.util
 import os
 import random
 import re
@@ -895,14 +893,13 @@ class User:
             try:
                 self.connection.send(self.output_queue[0])
                 del self.output_queue[0]
-            except:
+            except BrokenPipeError:
                 if self.account and self.account.get("name"):
                     account = self.account.get("name")
                 else:
                     account = "an unknown user"
-                log("Sending to %s raised an exception (broken pipe?)."
-                    % account, 7)
-                pass
+                log("Broken pipe sending to %s." % account, 7)
+                self.state = "disconnecting"
 
     def enqueue_input(self):
         """Process and enqueue any new input."""
@@ -910,7 +907,7 @@ class User:
         # check for some input
         try:
             raw_input = self.connection.recv(1024)
-        except:
+        except (BlockingIOError, OSError):
             raw_input = b""
 
         # we got something
@@ -1492,7 +1489,7 @@ def check_for_connection(listening_socket):
     # try to accept a new connection
     try:
         connection, address = listening_socket.accept()
-    except:
+    except BlockingIOError:
         return None
 
     # note that we got one
@@ -2233,8 +2230,9 @@ def command_show(actor, parameters):
         else:
             try:
                 message = repr(eval(" ".join(arguments[1:])))
-            except:
-                message = "Your expression raised an exception!"
+            except Exception as e:
+                message = ("$(red)Your expression raised an exception...$(eol)"
+                           "$(eol)$(bld)%s$(nrm)" % e)
     elif arguments[0] == "log":
         if len(arguments) == 4:
             if re.match("^\d+$", arguments[3]) and int(arguments[3]) >= 0:
@@ -2399,48 +2397,6 @@ def daemonize(universe):
     # only if this is what we're configured to do
     if universe.contents["internal:process"].getboolean("daemon"):
 
-        # if possible, we want to rename the process to the same as the script
-        new_argv = b"\x00".join(x.encode("utf-8") for x in sys.argv) + b"\x00"
-        short_argv0 = os.path.basename(sys.argv[0]).encode("utf-8") + b"\x00"
-
-        # attempt the linux way first
-        try:
-            argv_array = ctypes.POINTER(ctypes.c_char_p)
-            ctypes.pythonapi.Py_GetArgcArgv.argtypes = (
-                ctypes.POINTER(ctypes.c_int),
-                ctypes.POINTER(argv_array)
-            )
-            argc = argv_array()
-            ctypes.pythonapi.Py_GetArgcArgv(
-                ctypes.c_int(0),
-                ctypes.pointer(argc)
-            )
-            old_argv0_size = len(argc.contents.value)
-            ctypes.memset(argc.contents, 0, len(new_argv) + old_argv0_size)
-            ctypes.memmove(argc.contents, new_argv, len(new_argv))
-            ctypes.CDLL(ctypes.util.find_library("c")).prctl(
-                15,
-                short_argv0,
-                0,
-                0,
-                0
-            )
-
-        except:
-
-            # since that failed, maybe it's bsd?
-            try:
-
-                # much simpler, since bsd has a libc function call for this
-                ctypes.CDLL(ctypes.util.find_library("c")).setproctitle(
-                    new_argv
-                )
-
-            except:
-
-                # that didn't work either, so just log that we couldn't
-                log("Failed to rename the interpreter process (cosmetic).")
-
         # log before we start forking around, so the terminal gets the message
         log("Disassociating from the controlling terminal.")
 
@@ -2507,14 +2463,9 @@ def excepthook(excepttype, value, tracebackdata):
     # try to log it, if possible
     try:
         log(message, 9)
-    except:
-        pass
-
-    # try to write it to stderr, if possible
-    try:
-        sys.stderr.write(message)
-    except:
-        pass
+    except Exception as e:
+        # try to write it to stderr, if possible
+        sys.stderr.write(message + "\nException while logging...\n%s" % e)
 
 
 def sighook(what, where):