MDEV-5479: Prevent mysqld from stealing Unix socket of another active instance#4874
Open
FaramosCZ wants to merge 1 commit intoMariaDB:mainfrom
Open
MDEV-5479: Prevent mysqld from stealing Unix socket of another active instance#4874FaramosCZ wants to merge 1 commit intoMariaDB:mainfrom
FaramosCZ wants to merge 1 commit intoMariaDB:mainfrom
Conversation
80aea74 to
d9ec59e
Compare
gkodinov
requested changes
Mar 31, 2026
Member
gkodinov
left a comment
There was a problem hiding this comment.
Thank you for your contribution. This is a preliminary review. Frankly, this is a borderline bugfix IMHO. I'd consider doing it in 10.11. But let's leave that for the final reviewer.
Some portability suggestions below.
gkodinov
approved these changes
Apr 2, 2026
Member
gkodinov
left a comment
There was a problem hiding this comment.
LGTM. Please stand by for the final review.
When mysqld starts up and the configured Unix socket path is already in use by another process, network_init() unconditionally unlink()s the active socket file. This silently breaks the other process's ability to accept local connections. The typical scenario is two MariaDB instances configured with the same socket path but different TCP ports, but any process listening on that path is affected. Root cause: the call (void) unlink(mysqld_unix_port) in network_init() removes an existing socket file without checking whether another process is actively listening on it. Notably, the bind() error handler immediately following the unlink already warns about another running server -- but this message could never trigger because the unconditional unlink() removed the socket before bind() had a chance to fail. Fix: add handle_stale_unix_socket() that, before unlinking an existing socket file, attempts to connect() to it: - If connect() succeeds, another process is actively using the socket. Abort startup with an error message. - If connect() fails with EACCES, the socket is active but owned by another user. Abort -- we must not unlink a socket we cannot even verify. - If connect() fails with ECONNREFUSED or similar, the socket is stale from a previous unclean shutdown. Proceed with unlink() as before. - If the file exists but is not a socket (S_ISSOCK check), remove it to preserve previous behavior. connect() was chosen over flock()-based advisory locking because flock() requires managing a separate lock file alongside the socket (creation, cleanup, and handling of orphaned lock files), and does not work reliably on network filesystems. connect() probes the socket directly with no extra files and no NFS dependency. This is the same approach PostgreSQL uses for socket conflict prevention. Co-Authored-By: Claude AI <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When two MariaDB server instances are configured with the same Unix socket path but different TCP ports, the second instance starting up would unconditionally unlink() the first instance's active socket file in network_init(). This silently broke the first instance's ability to accept local socket connections, with no error or warning.
Root cause:
The call
(void) unlink(mysqld_unix_port)in network_init() (sql/mysqld.cc) removed any existing socket file without checking whether another running server process was actively listening on it. Notably, the bind() error handler immediately following the unlink already prints "Do you already have another server running on socket: %s ?" -- but this message could never trigger, because the unconditional unlink() removed the socket before bind() had a chance to fail.Fix:
Before unlinking an existing socket file, attempt to connect() to it:
Before this patch:
$ mysqld --port=13306 --socket=/tmp/mysql.sock # starts fine
$ mysqld --port=13307 --socket=/tmp/mysql.sock
starts, steals socket
First instance can no longer accept socket connections
After this patch:
$ mysqld --port=13306 --socket=/tmp/mysql.sock # starts fine
$ mysqld --port=13307 --socket=/tmp/mysql.sock # refuses to start:
"Another server process is already using the socket file
'/tmp/mysql.sock'. Aborting."
First instance continues to operate normally
Test plan: