[SERVER-26600] mongod should fail to start on macOS when it can't listen port on all interfaces Created: 12/Oct/16  Updated: 04/Nov/16  Resolved: 31/Oct/16

Status: Closed
Project: Core Server
Component/s: Networking
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Andrey Brindeyev Assignee: Mira Carey
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: OS X
Sprint: Platforms 2016-10-31
Participants:

 Description   

On macOS you can have two different mongod's listening on same port: one with a specific IP addresses to bind to and second one with an empty list. Can't reproduce this behavior on Linux.

$ uname -mprs
Darwin 16.0.0 x86_64 i386
$ ifconfig|grep 'inet '
	inet 127.0.0.1 netmask 0xff000000
	inet 10.5.17.205 netmask 0xffff0000 broadcast 10.5.255.255
	inet 10.211.55.2 netmask 0xffffff00 broadcast 10.211.55.255
	inet 10.37.129.2 netmask 0xffffff00 broadcast 10.37.129.255
$ ps -p $(pgrep mongod)
  PID TTY           TIME CMD
90084 ??         0:04.49 /usr/local/opt/mongodb/bin/mongod --config /usr/local/etc/mongod.conf
90613 ttys006    0:03.70 /usr/local/m/versions/3.4.0-rc0/bin/mongod
$ lsof -i :27017 -a -sTCP:LISTEN
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mongod  90084  abr    6u  IPv4 0x11850b417b6b6f43      0t0  TCP localhost:27017 (LISTEN)
mongod  90084  abr    8u  IPv4 0x11850b4177f2b64b      0t0  TCP 10.211.55.2:27017 (LISTEN)
mongod  90613  abr    6u  IPv4 0x11850b417dcb864b      0t0  TCP *:27017 (LISTEN)
$ grep bindIp /usr/local/etc/mongod.conf
  bindIp: 127.0.0.1,10.211.55.2

There are no warnings or errors for mongod running with default configuration (PID 90613):

2016-10-12T08:38:33.061-0700 I NETWORK  [thread1] waiting for connections on port 27017

$ mongo --port 27017
MongoDB shell version v3.4.0-rc0
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.2.10
WARNING: shell and server versions do not match
>
bye
$ mongo --port 27017 --host 10.37.129.2
MongoDB shell version v3.4.0-rc0
connecting to: mongodb://10.37.129.2:27017/
MongoDB server version: 3.4.0-rc0
Server has startup warnings:
2016-10-12T08:38:32.868-0700 I CONTROL  [initandlisten]
2016-10-12T08:38:32.868-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2016-10-12T08:38:32.868-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2016-10-12T08:38:32.869-0700 I CONTROL  [initandlisten]
> 



 Comments   
Comment by Mira Carey [ 26/Oct/16 ]

You're observing a difference in the effect that SO_REUSADDR has on Linux versus BSD.

In the original Berkeley Sockets implementation (which OSX inherits) SO_REUSEADDR allows for binding a socket to an address unless there is an exact collision of source address and port. Specifically, it allows for binds against INADDR_ANY (0.0.0.0) that resolve to a particular physical interface as well as binds to that interface specifically.

In comparison, linux diverges and considers INADDR_ANY and the interface it resolves to, to be in collision.

On all platforms, the reason we use SO_REUSEADDR is so that quickly after a restart of a mongod, the next process will be able to bind to the port, rather than having to wait for all the tcp sessions to fully time out (leave TIME_WAIT). The downstream effect on other concurrent binding processes is fallout from how the OS implements that socket option.

Comment by Vick Mena (Inactive) [ 12/Oct/16 ]

For completeness I'm adding the following which shows you can do the same thing with netcat on OSX. So now I'm not sure if this is an application issue or an OSX hidden feature.

nc -v -l 12999&
nc -v -l 127.0.0.1 12999&
nc -v -l 192.168.1.41 12999&

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nc 26211 vmena 3u IPv4 0xab0f013253116459 0t0 TCP *:12999 (LISTEN)
nc 26212 vmena 3u IPv4 0xab0f01325718af41 0t0 TCP 127.0.0.1:12999 (LISTEN)
nc 26269 vmena 3u IPv4 0xab0f01325718d459 0t0 TCP 192.168.1.41:12999 (LISTEN)


And on Linux the same application also behaves differently:
vagrant@vagrant:~$ nc -v -l 12999&
[1] 2189
vagrant@vagrant:~$ Listening on [0.0.0.0] (family 0, port 12999)
vagrant@vagrant:~$ nc -v -l 127.0.0.1 12999
nc: Address already in use
vagrant@vagrant:~$ nc -v -l 10.0.2.15 12999
nc: Address already in use
vagrant@vagrant:~$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:12999 0.0.0.0:* LISTEN 2189/nc
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1979/sshd
tcp6 0 0 :::22 :::* LISTEN 1979/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 1922/dhclient

Comment by Jorge Imperial-Sosa [ 12/Oct/16 ]

I can confirm the behavior is the same in OS X 15.6.0.
Used community releases 3.2.10 and 3.4.0-rc0 (both with SSL).

 
Jorges-MacBook:bin jorgeimperial$ lsof -i :27017 -a -sTCP:LISTEN
COMMAND  PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mongod  9238 jorgeimperial    6u  IPv4 0xb11e043cbbd867a1      0t0  TCP localhost:27017 (LISTEN)
mongod  9238 jorgeimperial    8u  IPv4 0xb11e043cd9da0289      0t0  TCP 192.168.1.48:27017 (LISTEN)
mongod  9253 jorgeimperial    6u  IPv4 0xb11e043cbbcfa289      0t0  TCP *:27017 (LISTEN)
Jorges-MacBook:bin jorgeimperial$
Jorges-MacBook:bin jorgeimperial$ uname -msrp
Darwin 15.6.0 x86_64 i386

Generated at Thu Feb 08 04:12:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.