Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Fix bug in IP detection (#3952)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhe-lz authored Jul 21, 2021
1 parent d8127e0 commit 442342c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
50 changes: 14 additions & 36 deletions ts/nni_manager/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import * as fs from 'fs';
import * as net from 'net';
import * as os from 'os';
import * as path from 'path';
import * as timersPromises from 'timers/promises';
import * as lockfile from 'lockfile';
import { Deferred } from 'ts-deferred';
import { Container } from 'typescript-ioc';
import * as util from 'util';
import * as glob from 'glob';

import { Database, DataStore } from './datastore';
Expand Down Expand Up @@ -49,39 +49,15 @@ function getExperimentsInfoPath(): string {
return path.join(os.homedir(), 'nni-experiments', '.experiment');
}

function mkDirP(dirPath: string): Promise<void> {
const deferred: Deferred<void> = new Deferred<void>();
fs.exists(dirPath, (exists: boolean) => {
if (exists) {
deferred.resolve();
} else {
const parent: string = path.dirname(dirPath);
mkDirP(parent).then(() => {
fs.mkdir(dirPath, (err: Error | null) => {
if (err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});
}).catch((err: Error) => {
deferred.reject(err);
});
}
});

return deferred.promise;
async function mkDirP(dirPath: string): Promise<void> {
await fs.promises.mkdir(dirPath, { recursive: true });
}

function mkDirPSync(dirPath: string): void {
if (fs.existsSync(dirPath)) {
return;
}
mkDirPSync(path.dirname(dirPath));
fs.mkdirSync(dirPath);
fs.mkdirSync(dirPath, { recursive: true });
}

const delay: (ms: number) => Promise<void> = util.promisify(setTimeout);
const delay = timersPromises.setTimeout;

/**
* Convert index to character
Expand Down Expand Up @@ -233,19 +209,21 @@ async function getIPV4Address(): Promise<string> {
const socket = dgram.createSocket('udp4');
socket.connect(1, '192.0.2.0');
for (let i = 0; i < 10; i++) { // wait the system to initialize "connection"
await yield_();
try { cachedIpv4Address = socket.address().address; } catch (error) { /* retry */ }
await timersPromises.setTimeout(1);
try {
cachedIpv4Address = socket.address().address;
socket.close();
return cachedIpv4Address;
} catch (error) {
/* retry */
}
}

cachedIpv4Address = socket.address().address; // if it still fails, throw the error
socket.close();

return cachedIpv4Address;
}

async function yield_(): Promise<void> {
/* trigger the scheduler, do nothing */
}

/**
* Get the status of canceled jobs according to the hint isEarlyStopped
*/
Expand Down
12 changes: 12 additions & 0 deletions ts/nni_manager/test/common/getIpv4Address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as assert from 'assert';
import { getIPV4Address } from '../../common/utils';

it('getIpv4Address', async () => {
const ip1 = await getIPV4Address();
const ip2 = await getIPV4Address();
assert.match(ip1, /^\d+\.\d+\.\d+\.\d+$/);
assert.equal(ip1, ip2);
});

0 comments on commit 442342c

Please sign in to comment.