{"record":{"id":"6de6f255eb7eddb8","repo":"XX-net/XX-Net","slug":"connectionpipe-remove-sock-e-r","errorCode":null,"errorMessage":"ConnectionPipe remove sock e:%r","messagePattern":"ConnectionPipe remove sock e:%r","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"code/default/x_tunnel/local/base_container.py","lineNumber":474,"sourceCode":"                if not res:\n                    # self.xlog.debug(\"remove_sock_event %s conn:%d event:%s removed all\", sock, conn.conn_id, event)\n                    del self.sock_conn_map[sock]\n            except Exception as e:\n                self.xlog.exception(\"remove_sock_event %s event:%s e:%r\", sock, event, e)\n\n    def remove_sock(self, sock):\n        with self._lock:\n            if sock not in self.sock_conn_map:\n                return\n\n            try:\n                conn = self.sock_conn_map[sock]\n                self._debug_log(\"remove_sock all events conn:%d\", conn.conn_id)\n                del self.sock_conn_map[sock]\n                self.select2.unregister(sock)\n            except Exception as e:\n                # error will happen when sock closed\n                self.xlog.warn(\"ConnectionPipe remove sock e:%r\", e)\n\n    def close_sock(self, sock, reason):\n        if sock not in self.sock_conn_map:\n            return\n\n        try:\n            conn = self.sock_conn_map[sock]\n            # self.xlog.info(\"close conn:%d\", conn.conn_id)\n            self.remove_sock(sock)\n\n            conn.transfer_peer_close(reason)\n            conn.do_stop(reason=reason)\n        except Exception as e:\n            self.xlog.exception(\"close_sock %s e:%r\", sock, e)\n\n    def reset_all_connections(self):\n        for sock, conn in dict(self.sock_conn_map).items():\n            self.close_sock(sock, \"reset_all\")","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/XX-net/XX-Net/blob/cfa5bc17b67676e467f37ec50766127e0ab5f0aa/code/default/x_tunnel/local/base_container.py#L456-L492","documentation":"ConnectionPipe.remove_sock logs this warning when cleaning up a socket fails unexpectedly — typically because the socket was already closed/removed, so the sock_conn_map lookup or select2.unregister throws. It is a benign race in teardown: close_sock and do_stop can both try to remove the same sock.","triggerScenarios":"A connection ends while both the read loop and shutdown path call close_sock/remove_sock for the same socket; second removal hits KeyError or an unregistered-fd error from select2.","commonSituations":"High connection churn (many simultaneous disconnects), server shutdown while connections are active — the warning is noise rather than a fault.","solutions":["No action usually needed — the socket is already removed/closed","If the log is noisy, guard remove_sock with a `if sock not in self.sock_conn_map: return` check or catch KeyError silently","Reduce double-closing by centralizing teardown through one path"],"exampleFix":"# before\nconn = self.sock_conn_map[sock]\n\ndef remove_sock(self, sock):\n    if sock not in self.sock_conn_map:\n        return\n    ...","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    pipe.remove_sock(sock)\nexcept Exception:\n    pass  # already cleaned up; benign teardown race","preventionTips":["Guard remove_sock with a map membership check first","Route all teardown through a single close path to avoid double removal"],"tags":["socket-cleanup","race-condition","x-tunnel","shutdown"],"backgroundTag":"double-cleanup-race","analyzedSha":"cfa5bc17b67676e467f37ec50766127e0ab5f0aa","analyzedAt":"2026-08-27T19:28:28.225Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}