{"record":{"id":"48a2a378ec5414f6","repo":"MyCATApache/Mycat-Server","slug":"mysql-replication-stream-is-already-open","errorCode":null,"errorMessage":"MySQL replication stream is already open","messagePattern":"MySQL replication stream is already open","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/migrate/BinlogStream.java","lineNumber":130,"sourceCode":"    }\n\n    private void initTaskDate() {\n        Date curDate = new Date();\n        for (MigrateTask migrateTask : migrateTaskList) {\n            migrateTask.setLastBinlogDate(curDate);\n        }\n    }\n\n    public void connect(long timeoutInMilliseconds) throws IOException, TimeoutException {\n        initTaskDate();\n        scheduler.scheduleAtFixedRate(new BinlogIdleCheck(this), 5, 15, TimeUnit.SECONDS);\n        allocateBinaryLogClient().connect(timeoutInMilliseconds);\n\n    }\n\n    private synchronized BinaryLogClient allocateBinaryLogClient() {\n        if (isConnected()) {\n            throw new IllegalStateException(\"MySQL replication stream is already open\");\n        }\n        binaryLogClient = new BinaryLogClient(hostname, port, username, password);\n        binaryLogClient.setBinlogFilename(getBinglogFile());\n        binaryLogClient.setBinlogPosition(getBinlogPos());\n        binaryLogClient.setServerId(getSlaveID());\n        binaryLogClient.registerEventListener(new DelegatingEventListener());\n        return binaryLogClient;\n    }\n\n\n    public synchronized boolean isConnected() {\n        return binaryLogClient != null && binaryLogClient.isConnected();\n    }\n\n\n    public synchronized void disconnect() throws IOException {\n        if (binaryLogClient != null) {\n            binaryLogClient.disconnect();","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/migrate/BinlogStream.java#L112-L148","documentation":"BinlogStream.allocateBinaryLogClient is synchronized and throws IllegalStateException(\"MySQL replication stream is already open\") if isConnected() is true when connect() is requested. Each BinlogStream instance manages a single BinaryLogClient, so a second concurrent connect on the same open stream is rejected.","triggerScenarios":"Calling connect() twice on the same BinlogStream without closing the previous connection, or two threads racing to connect — one enters the synchronized method while the underlying client is already connected.","commonSituations":"Reconnect logic that does not check isConnected() or close the old client first; application restart/retry logic re-invoking connect after a previous successful open; migration jobs triggered twice (e.g. scheduler overlap) sharing a BinlogStream instance.","solutions":["Guard calls with binlogStream.isConnected() before connect(), or track connection state in application code.","Close/disconnect the existing BinaryLogClient before reconnecting on the same stream.","Create a new BinlogStream instance for each connection lifecycle instead of reconnecting a shared one.","Serialize connect attempts behind an application-level lock/flag to avoid double-triggered jobs.","Catch IllegalStateException and treat it as a no-op if a connection is already active."],"exampleFix":"// before\nbinlogStream.connect(); // may be called repeatedly\n// after\nif (!binlogStream.isConnected()) {\n  binlogStream.connect();\n}","handlingStrategy":"try-catch","validationCode":"if (binlogStream.isConnected()) { return; } // already open, skip connect\nbinlogStream.connect();","typeGuard":null,"tryCatchPattern":"try { binlogStream.connect(); } catch (IllegalStateException e) { if (e.getMessage().contains(\"already open\")) { log.info(\"binlog stream already connected; ignoring duplicate connect\"); } else throw e; }","preventionTips":["Track connection lifecycle in one owner component","Check isConnected() before every connect","Close the old client before reconnecting","Prevent overlapping scheduler triggers for migration jobs"],"tags":["mysql","binlog","replication","illegal-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}