{"record":{"id":"85d2e2a9fd46fd1e","repo":"MyCATApache/Mycat-Server","slug":"log-already-in-use-filename-in-dir","errorCode":null,"errorMessage":"Log already in use? ${fileName} in ${dir}","messagePattern":"Log already in use\\? (.+?) in (.+?)","errorType":"exception","errorClass":"LogException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/mysql/xa/LogFileLock.java","lineNumber":55,"sourceCode":"        try {\n            File parent = new File(dir);\n            if(!parent.exists()) {\n                parent.mkdir();\n            }\n            lockfileToPreventDoubleStartup_ = new File(dir, fileName + \".lck\");\n            lockfilestream_ = new FileOutputStream(lockfileToPreventDoubleStartup_);\n            lock_ = lockfilestream_.getChannel().tryLock();\n            lockfileToPreventDoubleStartup_.deleteOnExit();\n        } catch (OverlappingFileLockException failedToGetLock) {\n            // happens on windows\n            lock_ = null;\n        } catch (IOException failedToGetLock) {\n            // happens on windows\n            lock_ = null;\n        }\n        if (lock_ == null) {\n            logger.error(\"ERROR: the specified log seems to be in use already: \" + fileName + \" in \" + dir + \". Make sure that no other instance is running, or kill any pending process if needed.\");\n            throw new LogException(\"Log already in use? \" + fileName + \" in \"+ dir);\n        }\n    }\n\n    public void releaseLock() {\n        try {\n            if (lock_ != null) {\n                lock_.release();\n            }\n            if (lockfilestream_ != null)\n                lockfilestream_.close();\n        } catch (IOException e) {\n            logger.warn(\"Error releasing file lock: \" + e.getMessage());\n        } finally {\n            lock_ = null;\n        }\n\n        if (lockfileToPreventDoubleStartup_ != null) {\n            lockfileToPreventDoubleStartup_.delete();","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/mysql/xa/LogFileLock.java#L37-L73","documentation":"LogFileLock.acquireLock() takes an OS-level file lock (FileLock) on the XA transaction log file. If the lock cannot be obtained (lock_ stays null) — the file is already locked by another MyCat instance or a zombie process — it logs an error and throws LogException(\"Log already in use? <fileName> in <dir>\").","triggerScenarios":"Starting a second MyCat instance against the same data dir while another instance holds the lock on the XA log file; a crashed/killed MyCat whose OS lock is still held on Windows, or a stale lock file that some filesystems fail to release.","commonSituations":"Accidentally running two MyCat nodes sharing one data directory; failover scripts starting a standby before the primary fully died; NFS-mounted dirs where file locks behave unreliably; leftover processes after an unclean kill on Windows.","solutions":["Find and stop the process holding the lock (lsof / handle.exe on the log file in the tmlog directory) before restarting MyCat","Ensure only one MyCat instance points at this data dir; fix deployment/failover scripts to guarantee mutual exclusion","Remove stale lock artifacts after confirming no process holds the lock, then restart","Move XA logs off NFS to a local filesystem where file locking is reliable"],"exampleFix":"# before (fails)\n./mycat start   # on same data dir while old instance runs\n# after\nps aux | grep mycat && kill <old-pid>; rm -f $MYCAT_HOME/logs/...stale-lock; ./mycat start","handlingStrategy":"retry","validationCode":"// before starting the second instance\nProcess proc = Runtime.getRuntime().exec(new String[]{\"fuser\", dir + \"/tmlog/LogFile\"});\n// exit code 0 => lock held; abort startup or wait for the holder to exit","typeGuard":null,"tryCatchPattern":"try { xaLog.acquireLock(); } catch (LogException e) { if (e.getMessage().startsWith(\"Log already in use?\")) { /* wait for other instance or kill stale process, then retry */ } else { throw e; } }","preventionTips":["Run exactly one MyCat per data dir; enforce via process manager","Use local filesystems (not NFS) for XA logs","After crashes, verify no zombie holder before restart"],"tags":["xa","file-lock","startup","concurrency"],"backgroundTag":"file-already-exists","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"}