# Buggy code
def _send_packet(self, packet: dict):
    for k, v in packet.items():
        if isinstance(v, bytes):
            # Make sure we don't have any garbage utf-8 from e.g. weird compilers
            # *cough* fpc *cough* that could cause this routine to crash
            # We cannot use utf8text because it may not be text.
            packet[k] = v.decode("utf-8", "replace")

    raw = zlib.compress(utf8bytes(json.dumps(packet)))
    with self._lock:
        self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))


# 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 _send_packet(self, packet: dict):
    for k, v in packet.items():
        if isinstance(v, bytes):
            # Make sure we don't have any garbage utf-8 from e.g. weird compilers
            # *cough* fpc *cough* that could cause this routine to crash
            # We cannot use utf8text because it may not be text.
            packet[k] = v.decode("utf-8", "replace")

    raw = zlib.compress(utf8bytes(json.dumps(packet)))
    with self._lock:
        try:
            self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))
        except Exception:  # connection reset by peer
            log.exception(
                "Exception while sending packet to site, will not attempt to reconnect! Quitting judge."
            )
            raise SystemExit(1)