Skip to content

Conversation

ShaneHarvey
Copy link
Collaborator

@ShaneHarvey ShaneHarvey commented Aug 27, 2025

DRIVERS-3256 Make Singleton thread safe to fix PortPool uniqueness

Note I could not reproduce the error reported in the ticket but my theory is that Singleton was not thread safe so it was possible that 2 servers started in 2 threads could create 2 different PortPool instances and therefor both yield the same port. A lock fixes this problem.

Update: I was able to reproduce the bug locally by using python3.13t and increasing the number of servers:

$ curl -XPOST --data "{\"id\": \"rs0\", \"members\": [{}, {}, {}, {}, {}, {}]}" "http://localhost:8889/v1/replica_sets"
...
pymongo.errors.OperationFailure: Found two member configurations with same host field, members.0.host == members.1.host == localhost:1025, full error: {'ok': 0.0, 'errmsg': 'Found two member configurations with same host field, members.0.host == members.1.host == localhost:1025', 'code': 93, 'codeName': 'InvalidReplicaSetConfig'}

PortPool itself is also not thread safe so I had to utilize a lock there as well.

@ShaneHarvey ShaneHarvey requested a review from blink1073 August 27, 2025 18:36
Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@blink1073
Copy link
Member

Actually, can you update the changelog readme and the version so I can tag and release?

@ShaneHarvey
Copy link
Collaborator Author

ShaneHarvey commented Aug 27, 2025

Update: I was able to reproduce the bug locally by using python3.13t and increasing the number of servers:

curl -XPOST --data "{\"id\": \"rs0\", \"members\": [{}, {}, {}, {}, {}, {}]}" "http://localhost:8889/v1/replica_sets"
Traceback (most recent call last):
  File "/Users/shane/git/mongo-orchestration/mongo_orchestration/apps/__init__.py", line 66, in wrap
    return f(*arg, **kwd)
  File "/Users/shane/git/mongo-orchestration/mongo_orchestration/apps/replica_sets.py", line 80, in rs_create
    result = _rs_create(data)
  File "/Users/shane/git/mongo-orchestration/mongo_orchestration/apps/replica_sets.py", line 37, in _rs_create
    rs_id = ReplicaSets().create(params)
  File "/Users/shane/git/mongo-orchestration/mongo_orchestration/replica_sets.py", line 657, in create
    repl = ReplicaSet(rs_params)
  File "/Users/shane/git/mongo-orchestration/mongo_orchestration/replica_sets.py", line 95, in __init__
    if not self.repl_init(config):
           ~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/shane/git/mongo-orchestration/mongo_orchestration/replica_sets.py", line 209, in repl_init
    result = self.connection(init_server).admin.command("replSetInitiate", config)
...
pymongo.errors.OperationFailure: Found two member configurations with same host field, members.0.host == members.1.host == localhost:1025, full error: {'ok': 0.0, 'errmsg': 'Found two member configurations with same host field, members.0.host == members.1.host == localhost:1025', 'code': 93, 'codeName': 'InvalidReplicaSetConfig'}

Spoiler is this bugfix doesn't resolve the issue. PortPool itself is not thread safe so we have to introduce a lock there as well.

@ShaneHarvey ShaneHarvey marked this pull request as draft August 27, 2025 20:56
@ShaneHarvey ShaneHarvey marked this pull request as ready for review August 27, 2025 21:12
@ShaneHarvey
Copy link
Collaborator Author

Resolved the race:

$ curl -XPOST --data "{\"id\": \"rs0\", \"members\": [{}, {}, {}, {}, {}, {}, {}]}" "http://localhost:8889/v1/replica_sets"
{"id": "rs0", "auth_key": null, "members": [{"_id": 0, ...}

Also updated the readme and version to 0.11.1.

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@ShaneHarvey ShaneHarvey changed the title DRIVERS-3256 Make Singleton thread safe to fix PortPool uniqueness DRIVERS-3256 Fix PortPool uniqueness when used in multiple threads Aug 27, 2025
@ShaneHarvey ShaneHarvey merged commit 44ac4f3 into mongodb-labs:master Aug 27, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants