Skip to content
/ server Public

MDEV-5479: Prevent mysqld from stealing Unix socket of another active instance#4874

Draft
FaramosCZ wants to merge 1 commit intoMariaDB:mainfrom
FaramosCZ:MDEV-5479
Draft

MDEV-5479: Prevent mysqld from stealing Unix socket of another active instance#4874
FaramosCZ wants to merge 1 commit intoMariaDB:mainfrom
FaramosCZ:MDEV-5479

Conversation

@FaramosCZ
Copy link
Copy Markdown
Contributor

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:

  • If connect() succeeds, another server is actively using the socket. Print an error message and abort startup via unireg_abort(1).
  • If connect() fails (ECONNREFUSED or similar), the socket is stale from a previous unclean shutdown. Proceed to unlink() as before.
  • If the socket file does not exist at all, skip directly to bind().
  • If the file exists but is not a socket (S_ISSOCK check), treat it as removable to preserve the previous behavior for non-socket files.

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:

  1. Start first MariaDB instance with --socket=/tmp/test.sock --port=13306
  2. Verify it is listening: mysql --socket=/tmp/test.sock -e "SELECT 1"
  3. Attempt to start second instance: --socket=/tmp/test.sock --port=13307
  4. Verify second instance aborts with the new error message in error log
  5. Verify first instance still accepts connections via the socket
  6. Stop the first instance, verify a new instance can start (stale socket cleanup still works)

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 27, 2026
@FaramosCZ FaramosCZ force-pushed the MDEV-5479 branch 2 times, most recently from 1775bd6 to 0eea40c Compare March 28, 2026 08:57
… instance

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:
- If connect() succeeds, another server is actively using the socket.
  Print an error message and abort startup via unireg_abort(1).
- If connect() fails (ECONNREFUSED or similar), the socket is stale
  from a previous unclean shutdown. Proceed to unlink() as before.
- If the socket file does not exist at all, skip directly to bind().
- If the file exists but is not a socket (S_ISSOCK check), treat it
  as removable to preserve the previous behavior for non-socket files.

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:
1. Start first MariaDB instance with
   --socket=/tmp/test.sock --port=13306
2. Verify it is listening: mysql --socket=/tmp/test.sock -e "SELECT 1"
3. Attempt to start second instance:
   --socket=/tmp/test.sock --port=13307
4. Verify second instance aborts with the new error message in error log
5. Verify first instance still accepts connections via the socket
6. Stop the first instance, verify a new instance
   can start (stale socket cleanup still works)

Co-Authored-By: Claude AI <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

2 participants