Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix launch port already in use #32892

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions python/paddle/distributed/fleet/launch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import logging
import socket
import time
import os
import signal
Expand All @@ -27,6 +25,7 @@
import socket
import warnings
import six
import struct

import paddle
import paddle.fluid as fluid
Expand Down Expand Up @@ -362,6 +361,10 @@ def add_arguments(argname, type, default, help, argparser, **kwargs):
def find_free_ports(num):
def __free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
# Note(wangxi): Close the connection with a TCP RST instead
# of a TCP FIN, to avoid time_wait state.
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', 1, 0))
s.bind(('', 0))
return s.getsockname()[1]

Expand All @@ -376,7 +379,7 @@ def __free_port():
return port_set

step += 1
if step > 100:
if step > 400:
print(
"can't find avilable port and use the specified static port now!"
)
Expand Down
4 changes: 4 additions & 0 deletions python/paddle/fluid/tests/unittests/test_launch_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from argparse import ArgumentParser, REMAINDER
from paddle.distributed.utils import _print_arguments, get_gpus, get_cluster_from_args
from paddle.distributed.fleet.launch_utils import find_free_ports


def _parse_args():
Expand Down Expand Up @@ -115,6 +116,9 @@ def test_gpus(self):
args.use_paddlecloud = True
cluster, pod = get_cluster_from_args(args, "0")

def test_find_free_ports(self):
find_free_ports(2)


if __name__ == '__main__':
unittest.main()