# Buggy code
def close(self):
    if self.conn and not self._closed:
        self.conn.shutdown(socket.SHUT_RDWR)
    self._closed = True


# 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 close(self):
    if self.conn and not self._closed:
        try:
            # May already be closed despite self._closed == False if a network error occurred and `close` is being
            # called as part of cleanup.
            self.conn.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass
    self._closed = True