[SERVER-78384] mongos should allow --port 0 Created: 23/Jun/23  Updated: 06/Nov/23  Resolved: 30/Jun/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.1.0-rc0, 5.0.22, 7.0.3, 4.4.26, 6.0.12

Type: Bug Priority: Minor - P4
Reporter: Anna Henningsen Assignee: Amirsaman Memaripour
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Documented
is documented by DOCS-16233 Investigate changes in SERVER-78384: ... Closed
Related
related to SERVER-78520 Make mongo servers use a file handle ... Backlog
Assigned Teams:
Service Arch
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v7.0, v6.0, v5.0, v4.4
Steps To Reproduce:

mongos --port 0

Sprint: Service Arch 2023-07-10
Participants:

 Description   

mongos, unlike mongod, does not allow listening on an arbitrary OS-assigned port by specifying --port 0.

In ancient times (2010), validation for --port was added to the server.

Then, in slightly less ancient times (2013), this validation was split into separate validation code for mongod and mongos.

And then, in not-even-all-that-long-ago times (2017), the validation for mongod was updated to allow port 0, but no such update was done for the corresponding mongos argument validation.

We should apply that update there as well.



 Comments   
Comment by Githook User [ 06/Nov/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)

GitOrigin-RevId: 1336e91500cf661a4a63a7c388bd7f38e75658ad
Branch: v4.4
https://github.com/mongodb/mongo/commit/fc98983f4054cb1258ca4d2708b4fc3d8e20cc52

Comment by Githook User [ 05/Nov/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)

GitOrigin-RevId: f4b6a1339727ca3aee6fc7cbe33fbbb74167d9c5
Branch: v4.4
https://github.com/mongodb/mongo/commit/1336e91500cf661a4a63a7c388bd7f38e75658ad

Comment by Githook User [ 05/Nov/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)

GitOrigin-RevId: 239b9638dba2209e18c8ea91e3567f190b3781cc
Branch: v4.4
https://github.com/mongodb/mongo/commit/f4b6a1339727ca3aee6fc7cbe33fbbb74167d9c5

Comment by Githook User [ 04/Nov/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)

GitOrigin-RevId: 38713499c32d4d937b5085183824491e83223c82
Branch: v4.4
https://github.com/mongodb/mongo/commit/239b9638dba2209e18c8ea91e3567f190b3781cc

Comment by Githook User [ 14/Oct/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)

GitOrigin-RevId: c01215d47aa8566f0da914203659d57c17d4212f
Branch: v4.4
https://github.com/mongodb/mongo/commit/38713499c32d4d937b5085183824491e83223c82

Comment by Githook User [ 13/Oct/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)
Branch: v6.0
https://github.com/mongodb/mongo/commit/9b35daae3f5fd3a32aa9d22c6ce65724f681cf3c

Comment by Githook User [ 13/Oct/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)
Branch: v5.0
https://github.com/mongodb/mongo/commit/d1040316ed5483fc21ab5f766496a3a4ae1b802e

Comment by Githook User [ 10/Oct/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`

(cherry picked from commit 7e059e2b54cbab335d4d5d3d7b66ced74c4424f5)
Branch: v7.0
https://github.com/mongodb/mongo/commit/2be3147ca448e6ad50930eec9c9100bb0b770d48

Comment by Githook User [ 03/Jul/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`
Branch: EVG-17874-taskgen-test
https://github.com/mongodb/mongo/commit/7e059e2b54cbab335d4d5d3d7b66ced74c4424f5

Comment by Githook User [ 28/Jun/23 ]

Author:

{'name': 'Saman Memaripour', 'email': 'amirsaman.memaripour@mongodb.com', 'username': 'samanca'}

Message: SERVER-78384 Refactor validation logic for `net.port`
Branch: master
https://github.com/mongodb/mongo/commit/7e059e2b54cbab335d4d5d3d7b66ced74c4424f5

Comment by Felipe Gasper [ 28/Jun/23 ]

How possible would it be to allow mongod & mongos to accept a pre-created socket, e.g. `--port=fd5`? That’d look like:

  1. Create the socket, with its cloexec flag off.
  2. Bind it to port 0.
  3. Grab the bound port via getsockname().
  4. exec(mongod, ... "--port=fd" . fileno(socket))

… then mongod/mongos would just take that fd rather than creating/binding their own.

That’s a much cleaner way of achieving the same result that I, at least, am looking for.

Comment by Eric Milkie [ 28/Jun/23 ]

That last commit from 2017 that started to allow port 0 almost seems like a mistake, since the rest of the changes are only tangentially related and no mention of the new behavior was made in either the Jira ticket or the commit description.

Comment by Felipe Gasper [ 28/Jun/23 ]

(Really, the best for testing would be if mongod & mongos could accept a socket on a file descriptor, but fishing the port out of the log works for now.)

Comment by Felipe Gasper [ 28/Jun/23 ]

While working on REP-2831 I ran across this problem independently of this ticket.

`--port 0` is useful for reliably starting a mongos without needing to pre-allocate a port. It’s possible, of course, to pre-bind to port 0, read that socket’s actual port, close the socket, then reuse the port, but that’s not race-safe.

Comment by Alex Neben [ 26/Jun/23 ]

Moving to service arch since this is C++ startup params.

Generated at Thu Feb 08 06:38:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.