reduce ephemeral port range to avoid flaky test.

Many Linux kernels use the port range 32768 to 60999

PiperOrigin-RevId: 353901866
Change-Id: I4dfde7189b5cc44b06ed5e807114a87f8a1d3344
This commit is contained in:
A. Unique TensorFlower 2021-01-26 10:52:59 -08:00 committed by TensorFlower Gardener
parent 2833b3d945
commit c0948d3b29

View File

@ -28,6 +28,10 @@ limitations under the License.
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/strcat.h"
// https://en.wikipedia.org/wiki/Ephemeral_port
#define MAX_EPHEMERAL_PORT 60999
#define MIN_EPHEMERAL_PORT 32768
namespace tensorflow {
namespace internal {
@ -41,7 +45,7 @@ bool IsPortAvailable(int* port, bool is_tcp) {
int actual_port;
CHECK_GE(*port, 0);
CHECK_LE(*port, 65535);
CHECK_LE(*port, MAX_EPHEMERAL_PORT);
if (fd < 0) {
LOG(ERROR) << "socket() failed: " << strerror(errno);
return false;
@ -109,9 +113,11 @@ int PickUnusedPortOrDie() {
CHECK_LE(trial, kMaximumTrials)
<< "Failed to pick an unused port for testing.";
if (trial == 1) {
port = getpid() % (65536 - 30000) + 30000;
port = getpid() % (MAX_EPHEMERAL_PORT - MIN_EPHEMERAL_PORT) +
MIN_EPHEMERAL_PORT;
} else if (trial <= kNumRandomPortsToPick) {
port = rand() % (65536 - 30000) + 30000;
port = rand() % (MAX_EPHEMERAL_PORT - MIN_EPHEMERAL_PORT) +
MIN_EPHEMERAL_PORT;
} else {
port = 0;
}