# Buggy code
def log_internal_error(self, exc: BaseException = None, message: str = None) -> None:
    if not message:
        # If exc exists, raise it so that sys.exc_info() is populated with its data.
        if exc:
            try:
                raise exc
            except KeyboardInterrupt:
                # Let KeyboardInterrupt bubble up.
                raise
            except:  # noqa: E722, we want to catch everything
                pass

        message = "".join(traceback.format_exception(*sys.exc_info()))

    # Strip ANSI from the message, since this might be a checker's CompileError ...we don't want to see the raw ANSI
    # codes from GCC/Clang on the site. We could use format_ansi and send HTML to the site, but the site doesn't
    # presently support HTML internal error formatting.
    self.packet_manager.internal_error_packet(strip_ansi(message))

    # Logs can contain ANSI, and it'll display fine
    print(message, file=sys.stderr)


# Traceback (BrokenPipeError)
INFO 2020-05-31 22:42:05,322 56 packet Received abortion request for 2124570
ERROR: ld.so: object '/libjemalloc.so.' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object '/libjemalloc.so.' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR 2020-05-31 22:42:10,327 56 judge Judge IPC process is still alive, sending SIGKILL!
Traceback (most recent call last):
File "/judge/dmoj/packet.py", line 151, in _read_forever
self._receive_packet(self._read_single())
File "/judge/dmoj/packet.py", line 240, in _receive_packet
self.ping_packet(packet['when'])
File "/judge/dmoj/packet.py", line 364, in ping_packet
self._send_packet(data)
File "/judge/dmoj/packet.py", line 235, in _send_packet
self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))
File "/usr/lib/python3.8/socket.py", line 687, in write
return self._sock.send(b)
File "/usr/lib/python3.8/ssl.py", line 1173, in send
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe
Exception in thread Thread-936:
Traceback (most recent call last):
File "/judge/dmoj/judge.py", line 137, in _grading_thread_main
handler_func(report, *data)
File "/judge/dmoj/judge.py", line 161, in _ipc_compile_message
self.packet_manager.compile_message_packet(compile_message)
File "/judge/dmoj/packet.py", line 316, in compile_message_packet
self._send_packet(
File "/judge/dmoj/packet.py", line 235, in _send_packet
self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))
File "/usr/lib/python3.8/socket.py", line 687, in write
return self._sock.send(b)
File "/usr/lib/python3.8/ssl.py", line 1173, in send
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/judge/dmoj/judge.py", line 145, in _grading_thread_main
self.log_internal_error()
File "/judge/dmoj/judge.py", line 250, in log_internal_error
self.packet_manager.internal_error_packet(strip_ansi(message))
File "/judge/dmoj/packet.py", line 323, in internal_error_packet
self._send_packet(
File "/judge/dmoj/packet.py", line 235, in _send_packet
self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))
File "/usr/lib/python3.8/socket.py", line 687, in write
return self._sock.send(b)
File "/usr/lib/python3.8/ssl.py", line 1173, in send
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe
Traceback (most recent call last):
File "/judge/dmoj/packet.py", line 151, in _read_forever
self._receive_packet(self._read_single())
File "/judge/dmoj/packet.py", line 240, in _receive_packet
self.ping_packet(packet['when'])
File "/judge/dmoj/packet.py", line 364, in ping_packet
self._send_packet(data)
File "/judge/dmoj/packet.py", line 235, in _send_packet
self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))
File "/usr/lib/python3.8/socket.py", line 687, in write
return self._sock.send(b)
File "/usr/lib/python3.8/ssl.py", line 1173, in send
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/judge/dmoj/judge.py", line 564, in main
judge.listen()
File "/judge/dmoj/judge.py", line 219, in listen
self.packet_manager.run()
File "/judge/dmoj/packet.py", line 178, in run
self._read_forever()
File "/judge/dmoj/packet.py", line 156, in _read_forever
raise SystemExit(1)
SystemExit: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/dmoj", line 11, in <module>
load_entry_point('dmoj', 'console_scripts', 'dmoj')()
File "/judge/dmoj/judge.py", line 570, in main
judge.murder()
File "/judge/dmoj/judge.py", line 229, in murder
self.packet_manager.close()
File "/judge/dmoj/packet.py", line 145, in close
self.conn.shutdown(socket.SHUT_RDWR)
File "/usr/lib/python3.8/ssl.py", line 1280, in shutdown
super().shutdown(how)
OSError: [Errno 107] Transport endpoint is not connected

# Fixed code
def log_internal_error(self, exc: BaseException = None, message: str = None) -> None:
    if not message:
        # If exc exists, raise it so that sys.exc_info() is populated with its data.
        if exc:
            try:
                raise exc
            except KeyboardInterrupt:
                # Let KeyboardInterrupt bubble up.
                raise
            except:  # noqa: E722, we want to catch everything
                pass

        message = "".join(traceback.format_exception(*sys.exc_info()))

    logger.error(message)

    try:
        # Strip ANSI from the message, since this might be a checker's CompileError ...we don't want to see the raw
        # ANSI codes from GCC/Clang on the site. We could use format_ansi and send HTML to the site, but the site
        # doesn't presently support HTML internal error formatting.
        self.packet_manager.internal_error_packet(strip_ansi(message))
    except Exception:  # noqa E722: don't want `log_internal_error` to trigger `log_internal_error`, ever
        logger.exception("Error encountered while reporting error to site!")