{"record":{"id":"d02fdf50aac027ec","repo":"XX-net/XX-Net","slug":"add-sock-event-s-conn-d-e-r","errorCode":null,"errorMessage":"add_sock_event %s conn:%d e:%r","messagePattern":"add_sock_event (.+?) conn:(.+?) e:%r","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"code/default/x_tunnel/local/base_container.py","lineNumber":431,"sourceCode":"        self._debug_log(\"ConnectionPipe stop\")\n\n    def _debug_log(self, fmt, *args, **kwargs):\n        if not self.session or not self.session.config.show_debug:\n            return\n        self.xlog.debug(fmt, *args, **kwargs)\n\n    def add_sock_event(self, sock, conn, event):\n        # this function can repeat without through an error.\n\n        if not sock:\n            return\n\n        with self._lock:\n            self._debug_log(\"add_sock_event conn:%d event:%s\", conn.conn_id, event)\n            try:\n                self.select2.register_event(sock, event, conn)\n            except Exception as e:\n                self.xlog.warn(\"add_sock_event %s conn:%d e:%r\", sock, conn.conn_id, e)\n                self.close_sock(sock, str(e) + \"_when_add_sock_event\")\n                return\n\n            # if sys.platform == \"win32\" and (sock not in self.sock_conn_map or event == selectors.EVENT_WRITE):\n            #     self.notice_select()\n\n            self.sock_conn_map[sock] = conn\n\n            if not self.th:\n                self.th = threading.Thread(target=self.pipe_worker, name=\"x_tunnel_pipe_worker\")\n                self.th.start()\n                self._debug_log(\"ConnectionPipe start\")\n\n    def remove_sock_event(self, sock, event):\n        # this function can repeat without through an error.\n\n        with self._lock:\n            if sock not in self.sock_conn_map:","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/XX-net/XX-Net/blob/cfa5bc17b67676e467f37ec50766127e0ab5f0aa/code/default/x_tunnel/local/base_container.py#L413-L449","documentation":"Registering a connection's socket with the selector (select2.register_event) raised an exception; the connection is then closed as a safety measure. Typically a closed/invalid fd ('EBADF' / 'bad file descriptor') or closed selector.","triggerScenarios":"add_sock_event called from start, put_cmd_data, process_cmd or send_to_sock after the socket was already closed, or during ConnectionPipe shutdown.","commonSituations":"Race between socket close and event registration; shutting down the tunnel while data still flows.","solutions":["Check if the pipe/worker is shutting down before issuing commands","Ensure sockets are unregistered before closing","Upgrade/patch race conditions around close_sock and register_event"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if not pipe.running:\n    return  # skip issuing commands while shutting down","typeGuard":null,"tryCatchPattern":"try:\n    pipe.add_sock_event(sock, event, conn)\nexcept Exception:\n    # already handled internally; conn is closed","preventionTips":["Check running flag before sending commands to a stopping pipe","Ensure single ownership of socket close/unregister"],"tags":["x-tunnel","selector","socket","race-condition"],"backgroundTag":"socket-registration-failed","analyzedSha":"cfa5bc17b67676e467f37ec50766127e0ab5f0aa","analyzedAt":"2026-08-27T19:28:28.225Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}