]> git.the-white-hart.net Git - gemini/cbs-server.git/commitdiff
Catch exception during handshake with client
authorrs <>
Sat, 20 Dec 2025 01:45:57 +0000 (19:45 -0600)
committerrs <>
Sat, 20 Dec 2025 01:45:57 +0000 (19:45 -0600)
When Lagrange doesn't yet trust the server's cert, it hangs up the
connection during the handshake phase, which causes OpenSSL to raise a
SysCallError.

Catch this, log it, and continue to serve normally afterwards.

cbs-srv.py

index 2778b37de892b103e8a13984494d6e3c6e292d09..e252ad9e348df0195cc00bb719ad1e9d89b508c9 100755 (executable)
@@ -221,20 +221,27 @@ def main():
         ssock = SSL.Connection(ctxt, sock)
         ssock.set_accept_state()
         while True:
-            conn, addr = ssock.accept()
-            conn.do_handshake()
-            logging.info('Connection from {}'.format(addr))
             try:
+                conn, addr = ssock.accept()
+                conn.do_handshake()
+                logging.info('Connection from {}'.format(addr))
                 req = recv_req(conn)
                 serve_req(conn, addr, req, conf)
+                conn.shutdown()
+                conn.sock_shutdown(socket.SHUT_RDWR)
+            except SSL.SysCallError as x:
+                logging.error('{}'.format(x))
+                # Don't call conn.shutdown or sock_shutdown
             except CBSException as x:
                 logging.error('{} {} {}'.format(x.code, x.meta, x.logdata))
                 conn.sendall('{} {}\r\n'.format(x.code, x.meta).encode('utf-8'))
+                conn.shutdown()
+                conn.sock_shutdown(socket.SHUT_RDWR)
             except Exception as x:
                 logging.error('Exception: {}'.format(x))
                 conn.sendall('40 Server error\r\n'.encode('utf-8'))
-            conn.shutdown()
-            conn.sock_shutdown(socket.SHUT_RDWR)
+                conn.shutdown()
+                conn.sock_shutdown(socket.SHUT_RDWR)
 
 
 if __name__ == '__main__':